]> gitweb.factorcode.org Git - factor.git/blob - extra/html/parser/parser.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / extra / html / parser / parser.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays hashtables sequence-parser
4 html.parser.utils kernel namespaces sequences
5 unicode.case unicode.categories combinators.short-circuit
6 quoting fry ;
7 IN: html.parser
8
9 TUPLE: tag name attributes text closing? ;
10
11 SINGLETON: text
12 SINGLETON: dtd
13 SINGLETON: comment
14
15 <PRIVATE
16
17 SYMBOL: tagstack
18
19 : push-tag ( tag -- )
20     tagstack get push ;
21
22 : closing-tag? ( string -- ? )
23     [ f ]
24     [ { [ first CHAR: / = ] [ peek CHAR: / = ] } 1|| ] if-empty ;
25
26 : <tag> ( name attributes closing? -- tag )
27     tag new
28         swap >>closing?
29         swap >>attributes
30         swap >>name ;
31
32 : make-tag ( string attribs -- tag )
33     [ [ closing-tag? ] keep "/" trim1 ] dip rot <tag> ;
34
35 : new-tag ( text name -- tag )
36     tag new
37         swap >>name
38         swap >>text ; inline
39
40 : (read-quote) ( state-parser ch -- string )
41     '[ [ current _ = ] take-until ] [ advance drop ] bi ;
42
43 : read-single-quote ( state-parser -- string )
44     CHAR: ' (read-quote) ;
45
46 : read-double-quote ( state-parser -- string )
47     CHAR: " (read-quote) ;
48
49 : read-quote ( state-parser -- string )
50     dup get+increment CHAR: ' =
51     [ read-single-quote ] [ read-double-quote ] if ;
52
53 : read-key ( state-parser -- string )
54     skip-whitespace
55     [ current { [ CHAR: = = ] [ blank? ] } 1|| ] take-until ;
56
57 : read-token ( state-parser -- string )
58     [ current blank? ] take-until ;
59
60 : read-value ( state-parser -- string )
61     skip-whitespace
62     dup current quote? [ read-quote ] [ read-token ] if
63     [ blank? ] trim ;
64
65 : read-comment ( state-parser -- )
66     "-->" take-until-sequence comment new-tag push-tag ;
67
68 : read-dtd ( state-parser -- )
69     ">" take-until-sequence dtd new-tag push-tag ;
70
71 : read-bang ( state-parser -- )
72     advance dup { [ current CHAR: - = ] [ peek-next CHAR: - = ] } 1&&
73     [ advance advance read-comment ] [ read-dtd ] if ;
74
75 : read-tag ( state-parser -- string )
76     [ [ current "><" member? ] take-until ]
77     [ dup current CHAR: < = [ advance ] unless drop ] bi ;
78
79 : read-until-< ( state-parser -- string )
80     [ current CHAR: < = ] take-until ;
81
82 : parse-text ( state-parser -- )
83     read-until-< [ text new-tag push-tag ] unless-empty ;
84
85 : parse-key/value ( state-parser -- key value )
86     [ read-key >lower ]
87     [ skip-whitespace "=" take-sequence ]
88     [ swap [ read-value ] [ drop dup ] if ] tri ;
89
90 : (parse-attributes) ( state-parser -- )
91     skip-whitespace
92     dup state-parse-end? [
93         drop
94     ] [
95         [ parse-key/value swap set ] [ (parse-attributes) ] bi
96     ] if ;
97
98 : parse-attributes ( state-parser -- hashtable )
99     [ (parse-attributes) ] H{ } make-assoc ;
100
101 : (parse-tag) ( string -- string' hashtable )
102     [
103         [ read-token >lower ] [ parse-attributes ] bi
104     ] state-parse ;
105
106 : read-< ( state-parser -- string/f )
107     advance dup current [
108         CHAR: ! = [ read-bang f ] [ read-tag ] if
109     ] [
110         drop f
111     ] if* ;
112
113 : parse-tag ( state-parser -- )
114     read-< [ (parse-tag) make-tag push-tag ] unless-empty ;
115
116 : (parse-html) ( state-parser -- )
117     dup peek-next [
118         [ parse-text ] [ parse-tag ] [ (parse-html) ] tri
119     ] [ drop ] if ;
120
121 : tag-parse ( quot -- vector )
122     V{ } clone tagstack [ state-parse ] with-variable ; inline
123
124 PRIVATE>
125
126 : parse-html ( string -- vector )
127     [ (parse-html) tagstack get ] tag-parse ;