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