factor/extra/html/parser/parser.factor

128 lines
3.3 KiB
Factor
Raw Normal View History

2008-09-25 03:02:47 -04:00
! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays hashtables html.parser.state
2009-04-01 12:43:30 -04:00
html.parser.utils kernel namespaces sequences
unicode.case unicode.categories combinators.short-circuit
2009-04-01 12:48:44 -04:00
quoting fry ;
2007-12-04 15:14:33 -05:00
IN: html.parser
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
TUPLE: tag name attributes text closing? ;
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
SINGLETON: text
SINGLETON: dtd
SINGLETON: comment
2009-04-01 13:48:51 -04:00
<PRIVATE
2007-09-20 18:09:08 -04:00
SYMBOL: tagstack
: push-tag ( tag -- )
tagstack get push ;
: closing-tag? ( string -- ? )
2008-08-17 11:38:34 -04:00
[ f ]
2009-04-01 12:48:44 -04:00
[ { [ first CHAR: / = ] [ peek CHAR: / = ] } 1|| ] if-empty ;
2007-09-20 18:09:08 -04:00
: <tag> ( name attributes closing? -- tag )
2008-08-14 00:09:43 -04:00
tag new
swap >>closing?
swap >>attributes
swap >>name ;
2007-09-20 18:09:08 -04:00
2008-08-17 11:38:34 -04:00
: make-tag ( string attribs -- tag )
2008-11-29 13:18:28 -05:00
[ [ closing-tag? ] keep "/" trim1 ] dip rot <tag> ;
2007-09-20 18:09:08 -04:00
2009-04-01 12:43:30 -04:00
: new-tag ( text name -- tag )
2008-08-17 11:38:34 -04:00
tag new
swap >>name
swap >>text ; inline
2007-09-20 18:09:08 -04:00
2009-04-01 12:48:44 -04:00
: (read-quote) ( state-parser ch -- string )
'[ [ current _ = ] take-until ] [ next drop ] bi ;
: read-single-quote ( state-parser -- string )
2009-04-01 12:48:44 -04:00
CHAR: ' (read-quote) ;
2007-09-20 18:09:08 -04:00
: read-double-quote ( state-parser -- string )
2009-04-01 12:48:44 -04:00
CHAR: " (read-quote) ;
2007-09-20 18:09:08 -04:00
: read-quote ( state-parser -- string )
dup get+increment CHAR: ' =
[ read-single-quote ] [ read-double-quote ] if ;
2007-09-20 18:09:08 -04:00
: read-key ( state-parser -- string )
skip-whitespace
[ current { [ CHAR: = = ] [ blank? ] } 1|| ] take-until ;
2007-09-20 18:09:08 -04:00
: read-token ( state-parser -- string )
[ current blank? ] take-until ;
2007-09-20 18:09:08 -04:00
: read-value ( state-parser -- string )
skip-whitespace
2009-04-01 03:33:38 -04:00
dup current quote? [ read-quote ] [ read-token ] if
2008-08-17 11:38:34 -04:00
[ blank? ] trim ;
2007-09-20 18:09:08 -04:00
: read-comment ( state-parser -- )
2009-04-01 12:43:30 -04:00
"-->" take-until-sequence comment new-tag push-tag ;
2007-09-20 18:09:08 -04:00
: read-dtd ( state-parser -- )
2009-04-01 12:43:30 -04:00
">" take-until-sequence dtd new-tag push-tag ;
2007-09-20 18:09:08 -04:00
: read-bang ( state-parser -- )
next dup { [ current CHAR: - = ] [ peek-next CHAR: - = ] } 1&&
[ next next read-comment ] [ read-dtd ] if ;
2007-09-20 18:09:08 -04:00
: read-tag ( state-parser -- string )
[ [ current "><" member? ] take-until ]
2009-04-01 03:33:38 -04:00
[ dup current CHAR: < = [ next ] unless drop ] bi ;
2007-09-20 18:09:08 -04:00
: read-until-< ( state-parser -- string )
[ current CHAR: < = ] take-until ;
2007-09-20 18:09:08 -04:00
: parse-text ( state-parser -- )
2009-04-01 12:43:30 -04:00
read-until-< [ text new-tag push-tag ] unless-empty ;
2007-09-20 18:09:08 -04:00
: parse-key/value ( state-parser -- key value )
[ read-key >lower ]
[ skip-whitespace "=" take-sequence ]
[ swap [ read-value ] [ drop f ] if ] tri ;
: (parse-attributes) ( state-parser -- )
skip-whitespace
dup state-parse-end? [
drop
] [
[ parse-key/value swap set ] [ (parse-attributes) ] bi
] if ;
2007-09-20 18:09:08 -04:00
: parse-attributes ( state-parser -- hashtable )
2009-04-01 12:43:30 -04:00
[ (parse-attributes) ] H{ } make-assoc ;
2007-09-20 18:09:08 -04:00
2008-06-14 01:41:48 -04:00
: (parse-tag) ( string -- string' hashtable )
2007-09-20 18:09:08 -04:00
[
[ read-token >lower ] [ parse-attributes ] bi
] state-parse ;
2007-09-20 18:09:08 -04:00
: read-< ( state-parser -- string/f )
2009-04-01 03:33:38 -04:00
next dup current [
CHAR: ! = [ read-bang f ] [ read-tag ] if
] [
drop f
] if* ;
: parse-tag ( state-parser -- )
read-< [ (parse-tag) make-tag push-tag ] unless-empty ;
: (parse-html) ( state-parser -- )
2009-04-01 03:33:38 -04:00
dup peek-next [
[ parse-text ] [ parse-tag ] [ (parse-html) ] tri
] [ drop ] if ;
2007-09-20 18:09:08 -04:00
: tag-parse ( quot -- vector )
V{ } clone tagstack [ state-parse ] with-variable ; inline
2007-09-20 18:09:08 -04:00
2009-04-01 13:48:51 -04:00
PRIVATE>
2007-09-20 18:09:08 -04:00
: parse-html ( string -- vector )
2008-08-17 11:38:34 -04:00
[ (parse-html) tagstack get ] tag-parse ;