1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: assocs html.parser kernel math sequences strings ascii
4 arrays generalizations shuffle unicode.case namespaces make
5 splitting http accessors io combinators http.client urls
6 urls.encoding fry prettyprint sets ;
7 IN: html.parser.analyzer
9 TUPLE: link attributes clickable ;
11 : scrape-html ( url -- headers vector )
14 : find-all ( seq quot -- alist )
15 [ <enum> >alist ] [ '[ second @ ] ] bi* filter ; inline
17 : find-nth ( seq quot n -- i elt )
18 [ <enum> >alist ] 2dip -rot
19 '[ _ [ second @ ] find-from rot drop swap 1+ ]
20 [ f 0 ] 2dip times drop first2 ; inline
22 : find-first-name ( vector string -- i/f tag/f )
23 >lower '[ name>> _ = ] find ; inline
25 : find-matching-close ( vector string -- i/f tag/f )
27 '[ [ name>> _ = ] [ closing?>> ] bi and ] find ; inline
29 : find-between* ( vector i/f tag/f -- vector )
31 [ tail-slice ] [ name>> ] bi*
32 dupd find-matching-close drop dup [ 1+ ] when
33 [ head ] [ first ] if*
38 : find-between ( vector i/f tag/f -- vector )
39 find-between* dup length 3 >= [
40 [ rest-slice but-last-slice ] keep like
43 : find-between-first ( vector string -- vector' )
44 dupd find-first-name find-between ; inline
46 : find-between-all ( vector quot -- seq )
48 '[ _ [ closing?>> not ] bi and ] find-all
49 [ first2 find-between* ] with map ; inline
51 : remove-blank-text ( vector -- vector' )
54 [ text>> [ blank? ] all? not ] [ drop t ] if
57 : trim-text ( vector -- vector' )
60 [ [ [ blank? ] trim ] change-text ] when
63 : find-by-id ( vector id -- vector' elt/f )
64 '[ attributes>> "id" at _ = ] find ;
66 : find-by-class ( vector id -- vector' elt/f )
67 '[ attributes>> "class" at _ = ] find ;
69 : find-by-name ( vector string -- vector elt/f )
70 >lower '[ name>> _ = ] find ;
72 : find-by-id-between ( vector string -- vector' )
74 '[ attributes>> "id" swap at _ = ] find find-between* ;
76 : find-by-class-between ( vector string -- vector' )
78 '[ attributes>> "class" swap at _ = ] find find-between* ;
80 : find-by-class-id-between ( vector class id -- vector' )
82 [ attributes>> "class" swap at _ = ]
83 [ attributes>> "id" swap at _ = ] bi and
84 ] dupd find find-between* ;
86 : find-by-attribute-key ( vector key -- vector' elt/? )
88 [ attributes>> at _ = ] filter sift ;
90 : find-by-attribute-key-value ( vector value key -- vector' )
92 [ attributes>> at over = ] with filter nip
95 : find-first-attribute-key-value ( vector value key -- i/f tag/f )
97 [ attributes>> at over = ] with find rot drop ;
99 : tag-link ( tag -- link/f )
100 attributes>> [ "href" swap at ] [ f ] if* ;
102 : find-links ( vector -- vector' )
103 [ [ name>> "a" = ] [ attributes>> "href" swap at ] bi and ]
106 : <link> ( vector -- link )
107 [ first attributes>> ]
108 [ [ name>> { text "img" } member? ] filter ] bi
111 : link. ( vector -- )
112 [ attributes>> "href" swap at write nl ]
113 [ clickable>> [ bl bl text>> print ] each nl ] bi ;
115 : find-by-text ( seq quot -- tag )
116 [ dup name>> text = ] prepose find drop ; inline
118 : find-opening-tags-by-name ( name seq -- seq )
119 [ [ name>> = ] [ closing?>> not ] bi and ] with find-all ;
121 : href-contains? ( str tag -- ? )
122 attributes>> "href" swap at* [ subseq? ] [ 2drop f ] if ;
124 : find-hrefs ( vector -- vector' )
128 [ attributes>> "href" swap key? ] bi and ] filter
130 [ [ attributes>> "href" swap at ] map ] map concat
133 : find-frame-links ( vector -- vector' )
134 [ name>> "frame" = ] find-between-all
135 [ [ attributes>> "src" swap at ] map sift ] map concat sift
138 : find-all-links ( vector -- vector' )
139 [ find-hrefs ] [ find-frame-links ] bi append prune ;
141 : find-forms ( vector -- vector' )
142 "form" over find-opening-tags-by-name
143 swap [ [ first2 ] dip find-between* ] curry map
144 [ [ name>> { "form" "input" } member? ] filter ] map ;
146 : find-html-objects ( vector string -- vector' )
147 dupd find-opening-tags-by-name
148 [ first2 find-between* ] curry map ;
150 : form-action ( vector -- string )
151 [ name>> "form" = ] find nip
152 attributes>> "action" swap at ;
154 : hidden-form-values ( vector -- strings )
155 [ attributes>> "type" swap at "hidden" = ] filter ;
160 [ bl bl bl bl [ write "=" write ] [ write bl ] bi* nl ] assoc-each ;
162 : form. ( vector -- )
163 [ closing?>> not ] filter
166 { [ dup name>> "form" = ]
167 [ "form action: " write attributes>> "action" swap at print ] }
168 { [ dup name>> "input" = ] [ input. ] }
173 : query>assoc* ( str -- hash )
174 "?" split1 nip query>assoc ;
176 : html-class? ( tag string -- ? )
177 swap attributes>> "class" swap at = ;
179 : html-id? ( tag string -- ? )
180 swap attributes>> "id" swap at = ;
182 : opening-tag? ( tag -- ? )