]> gitweb.factorcode.org Git - factor.git/blob - extra/html/parser/analyzer/analyzer.factor
html.parser.analyzer: make find-between* work on nested tags.
[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 math.statistics
5 sequences 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 ! Takes a sequence and a quotation expected to return -1 if the
55 ! element decrements the stack, 0 if it doesnt affect it and 1 if it
56 ! increments it. Then finds the matching element where the stack is
57 ! empty.
58 : stack-find ( seq quot -- i/f )
59     map cum-sum [ 0 = ] find drop ; inline
60
61 ! Produces a function which returns 1 if the input item is an opening
62 ! tag element with the specified name, -1 if it is a closing tag of
63 ! the same name and 0 otherwise.
64 : tag-classifier ( string -- quot )
65     >lower
66     '[ dup name>> _ = [ closing?>> [ -1 ] [ 1 ] if ] [ drop 0 ] if ] ; inline
67
68 : find-between* ( vector i/f tag/f -- vector )
69     over integer? [
70         [ tail-slice ] [ name>> ] bi*
71         dupd tag-classifier stack-find [ 1 + ] [ 1 ] if*
72         head
73     ] [
74         3drop V{ } clone
75     ] if ; inline
76
77 : find-between ( vector i/f tag/f -- vector )
78     find-between* dup length 3 >= [
79         [ rest-slice but-last-slice ] keep like
80     ] when ; inline
81
82 : find-between-first ( vector string -- vector' )
83     dupd find-first-name find-between ; inline
84
85 : find-between-all ( vector quot -- seq )
86     dupd
87     '[ _ [ closing?>> not ] bi and ] find-all
88     [ first2 find-between* ] with map ; inline
89
90 : remove-blank-text ( vector -- vector' )
91     [
92         dup name>> text =
93         [ text>> [ blank? ] all? not ] [ drop t ] if
94     ] filter ;
95
96 : trim-text ( vector -- vector' )
97     [
98         dup name>> text =
99         [ [ [ blank? ] trim ] change-text ] when
100     ] map ;
101
102 : find-by-id ( vector id -- vector' elt/f )
103     '[ "id" attribute _ = ] find ;
104     
105 : find-by-class ( vector id -- vector' elt/f )
106     '[ "class" attribute _ = ] find ;
107
108 : find-by-name ( vector string -- vector elt/f )
109     >lower '[ name>> _ = ] find ;
110
111 : find-by-id-between ( vector string -- vector' )
112     dupd
113     '[ "id" attribute _ = ] find find-between* ;
114     
115 : find-by-class-between ( vector string -- vector' )
116     dupd
117     '[ "class" attribute _ = ] find find-between* ;
118     
119 : find-by-class-id-between ( vector class id -- vector' )
120     [
121         '[
122             [ "class" attribute _ = ]
123             [ "id" attribute _ = ] bi and
124         ] find
125     ] [
126         2drop find-between*
127     ] 3bi ;
128
129 : find-by-attribute-key ( vector key -- vector' elt/? )
130     >lower
131     [ attributes>> at _ = ] filter sift ;
132
133 : find-by-attribute-key-value ( vector value key -- vector' )
134     >lower
135     [ attributes>> at over = ] with filter nip sift ;
136
137 : find-first-attribute-key-value ( vector value key -- i/f tag/f )
138     >lower
139     [ attributes>> at over = ] with find rot drop ;
140
141 : tag-link ( tag -- link/f ) "href" attribute ;
142
143 : find-links ( vector -- vector' )
144     [ { [ name>> "a" = ] [ "href" attribute ] } 1&& ]
145     find-between-all ;
146
147 : find-images ( vector -- vector' )
148     [
149         {
150             [ name>> "img" = ]
151             [ "src" attribute ]
152         } 1&&
153     ] find-all
154     values [ "src" attribute ] map ;
155
156 : find-by-text ( seq quot -- tag )
157     [ dup name>> text = ] prepose find drop ; inline
158
159 : find-opening-tags-by-name ( name seq -- seq )
160     [ { [ name>> = ] [ closing?>> not ] } 1&& ] with find-all ;
161
162 : href-contains? ( str tag -- ? )
163     "href" attribute* [ subseq? ] [ 2drop f ] if ;
164
165 : find-hrefs ( vector -- vector' )
166     find-links
167     [ [ { [ name>> "a" = ] [ "href" attribute? ] } 1&& ] filter ] map sift
168     [ [ "href" attribute ] map ] map concat [ >url ] map ;
169
170 : find-frame-links ( vector -- vector' )
171     [ name>> "frame" = ] find-between-all
172     [ [ "src" attribute ] map sift ] map concat sift
173     [ >url ] map ;
174
175 : find-all-links ( vector -- vector' )
176     [ find-hrefs ] [ find-frame-links ] bi union ;
177
178 : find-forms ( vector -- vector' )
179     "form" over find-opening-tags-by-name
180     swap [ [ first2 ] dip find-between* ] curry map
181     [ [ name>> { "form" "input" } member? ] filter ] map ;
182
183 : find-html-objects ( vector string -- vector' )
184     over find-opening-tags-by-name
185     [ first2 find-between* ] with map ;
186
187 : form-action ( vector -- string )
188     [ name>> "form" = ] find nip "action" attribute ;
189
190 : hidden-form-values ( vector -- strings )
191     [ "type" attribute "hidden" = ] filter ;
192
193 : input. ( tag -- )
194     dup name>> print
195     attributes>>
196     [ bl bl bl bl [ write "=" write ] [ write bl ] bi* nl ] assoc-each ;
197
198 : form. ( vector -- )
199     [ closing?>> not ] filter
200     [
201         {
202             { [ dup name>> "form" = ]
203                 [ "form action: " write "action" attribute print ] }
204             { [ dup name>> "input" = ] [ input. ] }
205             [ drop ]
206         } cond
207     ] each ;
208
209 : query>assoc* ( str -- hash )
210     "?" split1 nip query>assoc ;
211     
212 : html-class? ( tag string -- ? )
213     swap "class" attribute = ;
214     
215 : html-id? ( tag string -- ? )
216     swap "id" attribute = ;
217
218 : opening-tag? ( tag -- ? )
219     closing?>> not ;
220
221 TUPLE: link attributes clickable ;
222
223 : <link> ( vector -- link )
224     [ first attributes>> ]
225     [ [ name>> { text "img" } member? ] filter ] bi
226     link boa ;
227
228 : link. ( vector -- )
229     [ "href" attribute write nl ]
230     [ clickable>> [ bl bl text>> print ] each nl ] bi ;