]> gitweb.factorcode.org Git - factor.git/blob - extra/html/parser/analyzer/analyzer.factor
html.parser.analyzer: when matching on classname, check that the tag has the given...
[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 -- response 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 : html-class? ( tag string -- ? )
31     swap "class" attribute [ blank? ] split-when member? ;
32
33 : html-id? ( tag string -- ? )
34     swap "id" attribute = ;
35
36 ERROR: undefined-find-nth m n seq quot ;
37
38 : check-trivial-find ( m n seq quot -- m n seq quot )
39     pick 0 = [ undefined-find-nth ] when ; inline
40
41 : find-nth-from ( m n seq quot -- i/f elt/f )
42     check-trivial-find [ f ] 3dip '[
43         drop _ _ find-from [ dup [ 1 + ] when ] dip over
44     ] loopn [ dup [ 1 - ] when ] dip ; inline
45
46 : find-nth ( n seq quot -- i/f elt/f )
47     [ 0 ] 3dip find-nth-from ; inline
48
49 : find-last-nth-from ( m n seq quot -- i/f elt/f )
50     check-trivial-find [ f ] 3dip '[
51         drop _ _ find-last-from [ dup [ 1 - ] when ] dip over
52     ] loopn [ dup [ 1 + ] when ] dip ; inline
53
54 : find-last-nth ( n seq quot -- i/f elt/f )
55     [ [ nip length 1 - ] [ ] 2bi ] dip find-last-nth-from ; inline
56
57 : find-first-name ( vector string -- i/f tag/f )
58     >lower '[ name>> _ = ] find ; inline
59
60 : stack-find ( seq quot: ( elt -- 1/0/-1 ) -- i/f )
61     map cum-sum [ 0 = ] find drop ; inline
62
63 : tag-classifier ( string -- quot )
64     >lower
65     '[ dup name>> _ = [ closing?>> -1 1  ? ] [ drop 0 ] if ] ; inline
66
67 : find-between* ( vector i/f tag/f -- vector )
68     over integer? [
69         [ tail-slice ] [ name>> ] bi*
70         dupd tag-classifier stack-find [ 1 + ] [ 1 ] if*
71         head
72     ] [
73         3drop V{ } clone
74     ] if ; inline
75
76 : find-between ( vector i/f tag/f -- vector )
77     find-between* dup length 3 >= [
78         [ rest-slice but-last-slice ] keep like
79     ] when ; inline
80
81 : find-between-first ( vector string -- vector' )
82     dupd find-first-name find-between ; inline
83
84 : find-between-all ( vector quot -- seq )
85     dupd
86     '[ _ [ closing?>> not ] bi and ] find-all
87     [ first2 find-between* ] with map ; inline
88
89 : remove-blank-text ( vector -- vector' )
90     [
91         dup name>> text =
92         [ text>> [ blank? ] all? not ] [ drop t ] if
93     ] filter ;
94
95 : trim-text ( vector -- vector' )
96     [
97         dup name>> text =
98         [ [ [ blank? ] trim ] change-text ] when
99     ] map ;
100
101 : find-by-id ( vector id -- vector' elt/f )
102     '[ "id" attribute _ = ] find ;
103
104 : find-by-class ( vector id -- vector' elt/f )
105     '[ _ html-class? ] find ;
106
107 : find-by-name ( vector string -- vector elt/f )
108     >lower '[ name>> _ = ] find ;
109
110 : find-by-id-between ( vector string -- vector' )
111     dupd
112     '[ "id" attribute _ = ] find find-between* ;
113
114 : find-by-class-between ( vector string -- vector' )
115     dupd
116     '[ _ html-class? ] find find-between* ;
117
118 : find-by-class-id-between ( vector class id -- vector' )
119     [
120         '[
121             [ _ html-class? ]
122             [ "id" attribute _ = ] bi and
123         ] find
124     ] [
125         2drop find-between*
126     ] 3bi ;
127
128 : find-by-attribute-key ( vector key -- vector' elt/? )
129     >lower
130     [ attributes>> at _ = ] filter sift ;
131
132 : find-by-attribute-key-value ( vector value key -- vector' )
133     >lower
134     [ attributes>> at over = ] with filter nip sift ;
135
136 : find-first-attribute-key-value ( vector value key -- i/f tag/f )
137     >lower
138     [ attributes>> at over = ] with find rot drop ;
139
140 : tag-link ( tag -- link/f ) "href" attribute ;
141
142 : find-links ( vector -- vector' )
143     [ { [ name>> "a" = ] [ "href" attribute ] } 1&& ]
144     find-between-all ;
145
146 : find-images ( vector -- vector' )
147     [
148         {
149             [ name>> "img" = ]
150             [ "src" attribute ]
151         } 1&&
152     ] find-all
153     values [ "src" attribute ] map ;
154
155 : find-by-text ( seq quot -- tag )
156     [ dup name>> text = ] prepose find drop ; inline
157
158 : find-opening-tags-by-name ( name seq -- seq )
159     [ { [ name>> = ] [ closing?>> not ] } 1&& ] with find-all ;
160
161 : href-contains? ( str tag -- ? )
162     "href" attribute* [ subseq? ] [ 2drop f ] if ;
163
164 : find-hrefs ( vector -- vector' )
165     [ { [ name>> "a" = ] [ "href" attribute? ] } 1&& ] filter sift
166     [ "href" attribute >url ] map ;
167
168 : find-frame-links ( vector -- vector' )
169     [ { [ name>> "frame" = ] [ "src" attribute? ] } 1&& ] filter sift
170     [ "src" attribute >url ] map ;
171
172 : find-script-links ( vector -- vector' )
173     [ { [ name>> "script" = ] [ "src" attribute? ] } 1&& ] filter sift
174     [ "src" attribute >url ] map ;
175
176 : find-all-links ( vector -- vector' )
177     [ find-hrefs ] [ find-frame-links ] [ find-script-links ] tri union union ;
178
179 : find-forms ( vector -- vector' )
180     "form" over find-opening-tags-by-name
181     swap [ [ first2 ] dip find-between* ] curry map
182     [ [ name>> { "form" "input" } member? ] filter ] map ;
183
184 : find-html-objects ( vector string -- vector' )
185     over find-opening-tags-by-name
186     [ first2 find-between* ] with map ;
187
188 : form-action ( vector -- string )
189     [ name>> "form" = ] find nip "action" attribute ;
190
191 : hidden-form-values ( vector -- strings )
192     [ "type" attribute "hidden" = ] filter ;
193
194 : input. ( tag -- )
195     dup name>> print
196     attributes>>
197     [ bl bl bl bl [ write "=" write ] [ write bl ] bi* nl ] assoc-each ;
198
199 : form. ( vector -- )
200     [ closing?>> not ] filter
201     [
202         {
203             { [ dup name>> "form" = ]
204                 [ "form action: " write "action" attribute print ] }
205             { [ dup name>> "input" = ] [ input. ] }
206             [ drop ]
207         } cond
208     ] each ;
209
210 : query>assoc* ( str -- hash )
211     "?" split1 nip query>assoc ;
212
213 : opening-tag? ( tag -- ? )
214     closing?>> not ;
215
216 TUPLE: link attributes clickable ;
217
218 : <link> ( vector -- link )
219     [ first attributes>> ]
220     [ [ name>> { text "img" } member? ] filter ] bi
221     link boa ;
222
223 : link. ( vector -- )
224     [ "href" attribute write nl ]
225     [ clickable>> [ bl bl text>> print ] each nl ] bi ;