]> gitweb.factorcode.org Git - factor.git/blob - extra/html/parser/analyzer/analyzer.factor
Simplify combinator with joe's suggestion, unit test
[factor.git] / extra / html / parser / analyzer / analyzer.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs combinators combinators.short-circuit
4 fry html.parser http.client io kernel locals math sequences
5 sets splitting unicode.case unicode.categories urls
6 urls.encoding shuffle ;
7 IN: html.parser.analyzer
8
9 : scrape-html ( url -- headers vector )
10     http-get parse-html ;
11
12 : attribute ( tag string -- obj/f )
13     swap attributes>> [ at ] [ drop f ] if* ;
14
15 : attribute* ( tag string -- obj ? )
16     swap attributes>> [ at* ] [ drop f f ] if* ;
17
18 : attribute? ( tag string -- obj )
19     swap attributes>> [ key? ] [ drop f ] if* ;
20
21 : find-all ( seq quot -- alist )
22    [ <enum> >alist ] [ '[ second @ ] ] bi* filter ; inline
23
24 : loopn-index ( n quot -- )
25     [ iota ] [ '[ @ not ] ] bi* find 2drop ; inline
26
27 : loopn ( n quot -- )
28     [ drop ] prepose loopn-index ; inline
29
30 ERROR: undefined-find-nth m n seq quot ;
31
32 : check-trivial-find ( m n seq quot -- m n seq quot )
33     pick 0 = [ undefined-find-nth ] when ; inline
34
35 : find-nth-from ( m n seq quot -- i/f elt/f )
36     check-trivial-find [ f ] 3dip '[
37         drop _ _ find-from [ dup [ 1 + ] when ] dip over
38     ] loopn [ dup [ 1 - ] when ] dip ; inline
39
40 : find-nth ( n seq quot -- i/f elt/f )
41     [ 0 ] 3dip find-nth-from ; inline
42
43 : find-last-nth-from ( m n seq quot -- i/f elt/f )
44     check-trivial-find [ f ] 3dip '[
45         drop _ _ find-last-from [ dup [ 1 - ] when ] dip over
46     ] loopn [ dup [ 1 + ] when ] dip ; inline
47
48 : find-last-nth ( n seq quot -- i/f elt/f )
49     [ [ nip length 1 - ] [ ] 2bi ] dip find-last-nth-from ; inline
50
51 : find-first-name ( vector string -- i/f tag/f )
52     >lower '[ name>> _ = ] find ; inline
53
54 : find-matching-close ( vector string -- i/f tag/f )
55     >lower
56     '[ [ name>> _ = ] [ closing?>> ] bi and ] find ; inline
57
58 : find-between* ( vector i/f tag/f -- vector )
59     over integer? [
60         [ tail-slice ] [ name>> ] bi*
61         dupd find-matching-close drop [ 1 + ] [ 1 ] if*
62         head
63     ] [
64         3drop V{ } clone
65     ] if ; inline
66
67 : find-between ( vector i/f tag/f -- vector )
68     find-between* dup length 3 >= [
69         [ rest-slice but-last-slice ] keep like
70     ] when ; inline
71
72 : find-between-first ( vector string -- vector' )
73     dupd find-first-name find-between ; inline
74
75 : find-between-all ( vector quot -- seq )
76     dupd
77     '[ _ [ closing?>> not ] bi and ] find-all
78     [ first2 find-between* ] with map ; inline
79
80 : remove-blank-text ( vector -- vector' )
81     [
82         dup name>> text =
83         [ text>> [ blank? ] all? not ] [ drop t ] if
84     ] filter ;
85
86 : trim-text ( vector -- vector' )
87     [
88         dup name>> text =
89         [ [ [ blank? ] trim ] change-text ] when
90     ] map ;
91
92 : find-by-id ( vector id -- vector' elt/f )
93     '[ "id" attribute _ = ] find ;
94     
95 : find-by-class ( vector id -- vector' elt/f )
96     '[ "class" attribute _ = ] find ;
97
98 : find-by-name ( vector string -- vector elt/f )
99     >lower '[ name>> _ = ] find ;
100
101 : find-by-id-between ( vector string -- vector' )
102     dupd
103     '[ "id" attribute _ = ] find find-between* ;
104     
105 : find-by-class-between ( vector string -- vector' )
106     dupd
107     '[ "class" attribute _ = ] find find-between* ;
108     
109 : find-by-class-id-between ( vector class id -- vector' )
110     [
111         '[
112             [ "class" attribute _ = ]
113             [ "id" attribute _ = ] bi and
114         ] find
115     ] [
116         2drop find-between*
117     ] 3bi ;
118
119 : find-by-attribute-key ( vector key -- vector' elt/? )
120     >lower
121     [ attributes>> at _ = ] filter sift ;
122
123 : find-by-attribute-key-value ( vector value key -- vector' )
124     >lower
125     [ attributes>> at over = ] with filter nip sift ;
126
127 : find-first-attribute-key-value ( vector value key -- i/f tag/f )
128     >lower
129     [ attributes>> at over = ] with find rot drop ;
130
131 : tag-link ( tag -- link/f ) "href" attribute ;
132
133 : find-links ( vector -- vector' )
134     [ { [ name>> "a" = ] [ "href" attribute ] } 1&& ]
135     find-between-all ;
136
137 : find-images ( vector -- vector' )
138     [
139         {
140             [ name>> "img" = ]
141             [ "src" attribute ]
142         } 1&&
143     ] find-all
144     values [ "src" attribute ] map ;
145
146 : find-by-text ( seq quot -- tag )
147     [ dup name>> text = ] prepose find drop ; inline
148
149 : find-opening-tags-by-name ( name seq -- seq )
150     [ { [ name>> = ] [ closing?>> not ] } 1&& ] with find-all ;
151
152 : href-contains? ( str tag -- ? )
153     "href" attribute* [ subseq? ] [ 2drop f ] if ;
154
155 : find-hrefs ( vector -- vector' )
156     find-links
157     [ [ { [ name>> "a" = ] [ "href" attribute? ] } 1&& ] filter ] map sift
158     [ [ "href" attribute ] map ] map concat [ >url ] map ;
159
160 : find-frame-links ( vector -- vector' )
161     [ name>> "frame" = ] find-between-all
162     [ [ "src" attribute ] map sift ] map concat sift
163     [ >url ] map ;
164
165 : find-all-links ( vector -- vector' )
166     [ find-hrefs ] [ find-frame-links ] bi union ;
167
168 : find-forms ( vector -- vector' )
169     "form" over find-opening-tags-by-name
170     swap [ [ first2 ] dip find-between* ] curry map
171     [ [ name>> { "form" "input" } member? ] filter ] map ;
172
173 : find-html-objects ( vector string -- vector' )
174     dupd find-opening-tags-by-name
175     [ first2 find-between* ] curry map ;
176
177 : form-action ( vector -- string )
178     [ name>> "form" = ] find nip "action" attribute ;
179
180 : hidden-form-values ( vector -- strings )
181     [ "type" attribute "hidden" = ] filter ;
182
183 : input. ( tag -- )
184     dup name>> print
185     attributes>>
186     [ bl bl bl bl [ write "=" write ] [ write bl ] bi* nl ] assoc-each ;
187
188 : form. ( vector -- )
189     [ closing?>> not ] filter
190     [
191         {
192             { [ dup name>> "form" = ]
193                 [ "form action: " write "action" attribute print ] }
194             { [ dup name>> "input" = ] [ input. ] }
195             [ drop ]
196         } cond
197     ] each ;
198
199 : query>assoc* ( str -- hash )
200     "?" split1 nip query>assoc ;
201     
202 : html-class? ( tag string -- ? )
203     swap "class" attribute = ;
204     
205 : html-id? ( tag string -- ? )
206     swap "id" attribute = ;
207
208 : opening-tag? ( tag -- ? )
209     closing?>> not ;
210
211 TUPLE: link attributes clickable ;
212
213 : <link> ( vector -- link )
214     [ first attributes>> ]
215     [ [ name>> { text "img" } member? ] filter ] bi
216     link boa ;
217
218 : link. ( vector -- )
219     [ "href" attribute write nl ]
220     [ clickable>> [ bl bl text>> print ] each nl ] bi ;