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
7 IN: html.parser.analyzer
9 : scrape-html ( url -- headers vector )
12 : attribute ( tag string -- obj/f )
13 swap attributes>> [ at ] [ drop f ] if* ;
15 : attribute* ( tag string -- obj ? )
16 swap attributes>> [ at* ] [ drop f f ] if* ;
18 : attribute? ( tag string -- obj )
19 swap attributes>> [ key? ] [ drop f ] if* ;
21 : find-all ( seq quot -- alist )
22 [ <enum> >alist ] [ '[ second @ ] ] bi* filter ; inline
24 : loopn-index ( ... pred: ( ... n -- ... ? ) n -- ... )
26 [ swap call ] [ 1 - ] 2bi
27 [ loopn-index ] 2curry when
30 ] if ; inline recursive
32 : loopn ( ... pred: ( ... -- ... ? ) n -- ... )
33 [ [ drop ] prepose ] dip loopn-index ; inline
35 :: find-nth ( n seq quot -- i/f elt/f )
37 [ drop seq quot find-from ] dip 1 = [
38 over [ [ 1 + ] dip ] when
39 ] unless over >boolean
40 ] n loopn-index ; inline
42 : find-first-name ( vector string -- i/f tag/f )
43 >lower '[ name>> _ = ] find ; inline
45 : find-matching-close ( vector string -- i/f tag/f )
47 '[ [ name>> _ = ] [ closing?>> ] bi and ] find ; inline
49 : find-between* ( vector i/f tag/f -- vector )
51 [ tail-slice ] [ name>> ] bi*
52 dupd find-matching-close drop [ 1 + ] [ 1 ] if*
58 : find-between ( vector i/f tag/f -- vector )
59 find-between* dup length 3 >= [
60 [ rest-slice but-last-slice ] keep like
63 : find-between-first ( vector string -- vector' )
64 dupd find-first-name find-between ; inline
66 : find-between-all ( vector quot -- seq )
68 '[ _ [ closing?>> not ] bi and ] find-all
69 [ first2 find-between* ] with map ; inline
71 : remove-blank-text ( vector -- vector' )
74 [ text>> [ blank? ] all? not ] [ drop t ] if
77 : trim-text ( vector -- vector' )
80 [ [ [ blank? ] trim ] change-text ] when
83 : find-by-id ( vector id -- vector' elt/f )
84 '[ "id" attribute _ = ] find ;
86 : find-by-class ( vector id -- vector' elt/f )
87 '[ "class" attribute _ = ] find ;
89 : find-by-name ( vector string -- vector elt/f )
90 >lower '[ name>> _ = ] find ;
92 : find-by-id-between ( vector string -- vector' )
94 '[ "id" attribute _ = ] find find-between* ;
96 : find-by-class-between ( vector string -- vector' )
98 '[ "class" attribute _ = ] find find-between* ;
100 : find-by-class-id-between ( vector class id -- vector' )
103 [ "class" attribute _ = ]
104 [ "id" attribute _ = ] bi and
110 : find-by-attribute-key ( vector key -- vector' elt/? )
112 [ attributes>> at _ = ] filter sift ;
114 : find-by-attribute-key-value ( vector value key -- vector' )
116 [ attributes>> at over = ] with filter nip sift ;
118 : find-first-attribute-key-value ( vector value key -- i/f tag/f )
120 [ attributes>> at over = ] with find rot drop ;
122 : tag-link ( tag -- link/f ) "href" attribute ;
124 : find-links ( vector -- vector' )
125 [ { [ name>> "a" = ] [ "href" attribute ] } 1&& ]
128 : find-images ( vector -- vector' )
135 values [ "src" attribute ] map ;
137 : find-by-text ( seq quot -- tag )
138 [ dup name>> text = ] prepose find drop ; inline
140 : find-opening-tags-by-name ( name seq -- seq )
141 [ { [ name>> = ] [ closing?>> not ] } 1&& ] with find-all ;
143 : href-contains? ( str tag -- ? )
144 "href" attribute* [ subseq? ] [ 2drop f ] if ;
146 : find-hrefs ( vector -- vector' )
148 [ [ { [ name>> "a" = ] [ "href" attribute? ] } 1&& ] filter ] map sift
149 [ [ "href" attribute ] map ] map concat [ >url ] map ;
151 : find-frame-links ( vector -- vector' )
152 [ name>> "frame" = ] find-between-all
153 [ [ "src" attribute ] map sift ] map concat sift
156 : find-all-links ( vector -- vector' )
157 [ find-hrefs ] [ find-frame-links ] bi union ;
159 : find-forms ( vector -- vector' )
160 "form" over find-opening-tags-by-name
161 swap [ [ first2 ] dip find-between* ] curry map
162 [ [ name>> { "form" "input" } member? ] filter ] map ;
164 : find-html-objects ( vector string -- vector' )
165 dupd find-opening-tags-by-name
166 [ first2 find-between* ] curry map ;
168 : form-action ( vector -- string )
169 [ name>> "form" = ] find nip "action" attribute ;
171 : hidden-form-values ( vector -- strings )
172 [ "type" attribute "hidden" = ] filter ;
177 [ bl bl bl bl [ write "=" write ] [ write bl ] bi* nl ] assoc-each ;
179 : form. ( vector -- )
180 [ closing?>> not ] filter
183 { [ dup name>> "form" = ]
184 [ "form action: " write "action" attribute print ] }
185 { [ dup name>> "input" = ] [ input. ] }
190 : query>assoc* ( str -- hash )
191 "?" split1 nip query>assoc ;
193 : html-class? ( tag string -- ? )
194 swap "class" attribute = ;
196 : html-id? ( tag string -- ? )
197 swap "id" attribute = ;
199 : opening-tag? ( tag -- ? )
202 TUPLE: link attributes clickable ;
204 : <link> ( vector -- link )
205 [ first attributes>> ]
206 [ [ name>> { text "img" } member? ] filter ] bi
209 : link. ( vector -- )
210 [ "href" attribute write nl ]
211 [ clickable>> [ bl bl text>> print ] each nl ] bi ;