]> gitweb.factorcode.org Git - factor.git/blob - extra/html/parser/analyzer/analyzer.factor
10fcd9c449ade7c150ae2cb4469fa209cc13b645
[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: assocs html.parser kernel math sequences strings ascii
4 arrays generalizations shuffle namespaces make
5 splitting http accessors io combinators http.client urls
6 urls.encoding fry prettyprint sets combinators.short-circuit ;
7 IN: html.parser.analyzer
8
9 TUPLE: link attributes clickable ;
10
11 : scrape-html ( url -- headers vector )
12     http-get parse-html ;
13
14 : find-all ( seq quot -- alist )
15    [ <enum> >alist ] [ '[ second @ ] ] bi* filter ; inline
16
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
21
22 : find-first-name ( vector string -- i/f tag/f )
23     >lower '[ name>> _ = ] find ; inline
24
25 : find-matching-close ( vector string -- i/f tag/f )
26     >lower
27     '[ [ name>> _ = ] [ closing?>> ] bi and ] find ; inline
28
29 : find-between* ( vector i/f tag/f -- vector )
30     over integer? [
31         [ tail-slice ] [ name>> ] bi*
32         dupd find-matching-close drop dup [ 1 + ] when
33         [ head ] [ first ] if*
34     ] [
35         3drop V{ } clone
36     ] if ; inline
37
38 : find-between ( vector i/f tag/f -- vector )
39     find-between* dup length 3 >= [
40         [ rest-slice but-last-slice ] keep like
41     ] when ; inline
42
43 : find-between-first ( vector string -- vector' )
44     dupd find-first-name find-between ; inline
45
46 : find-between-all ( vector quot -- seq )
47     dupd
48     '[ _ [ closing?>> not ] bi and ] find-all
49     [ first2 find-between* ] with map ; inline
50
51 : remove-blank-text ( vector -- vector' )
52     [
53         dup name>> text =
54         [ text>> [ blank? ] all? not ] [ drop t ] if
55     ] filter ;
56
57 : trim-text ( vector -- vector' )
58     [
59         dup name>> text =
60         [ [ [ blank? ] trim ] change-text ] when
61     ] map ;
62
63 : find-by-id ( vector id -- vector' elt/f )
64     '[ attributes>> "id" at _ = ] find ;
65     
66 : find-by-class ( vector id -- vector' elt/f )
67     '[ attributes>> "class" at _ = ] find ;
68
69 : find-by-name ( vector string -- vector elt/f )
70     >lower '[ name>> _ = ] find ;
71
72 : find-by-id-between ( vector string -- vector' )
73     dupd
74     '[ attributes>> "id" swap at _ = ] find find-between* ;
75     
76 : find-by-class-between ( vector string -- vector' )
77     dupd
78     '[ attributes>> "class" swap at _ = ] find find-between* ;
79     
80 : find-by-class-id-between ( vector class id -- vector' )
81     '[
82         [ attributes>> "class" swap at _ = ]
83         [ attributes>> "id" swap at _ = ] bi and
84     ] dupd find find-between* ;
85
86 : find-by-attribute-key ( vector key -- vector' elt/? )
87     >lower
88     [ attributes>> at _ = ] filter sift ;
89
90 : find-by-attribute-key-value ( vector value key -- vector' )
91     >lower
92     [ attributes>> at over = ] with filter nip
93     sift ;
94
95 : find-first-attribute-key-value ( vector value key -- i/f tag/f )
96     >lower
97     [ attributes>> at over = ] with find rot drop ;
98
99 : tag-link ( tag -- link/f )
100     attributes>> [ "href" swap at ] [ f ] if* ;
101
102 : find-links ( vector -- vector' )
103     [ [ name>> "a" = ] [ attributes>> "href" swap at ] bi and ]
104     find-between-all ;
105
106 : find-images ( vector -- vector' )
107     [
108         {
109             [ name>> "img" = ]
110             [ attributes>> "src" swap at ]
111         } 1&&
112     ] find-all
113     values [ attributes>> "src" swap at ] map ;
114
115 : <link> ( vector -- link )
116     [ first attributes>> ]
117     [ [ name>> { text "img" } member? ] filter ] bi
118     link boa ;
119
120 : link. ( vector -- )
121     [ attributes>> "href" swap at write nl ]
122     [ clickable>> [ bl bl text>> print ] each nl ] bi ;
123
124 : find-by-text ( seq quot -- tag )
125     [ dup name>> text = ] prepose find drop ; inline
126
127 : find-opening-tags-by-name ( name seq -- seq )
128     [ [ name>> = ] [ closing?>> not ] bi and ] with find-all ;
129
130 : href-contains? ( str tag -- ? )
131     attributes>> "href" swap at* [ subseq? ] [ 2drop f ] if ;
132
133 : find-hrefs ( vector -- vector' )
134     find-links
135     [ [
136         [ name>> "a" = ]
137         [ attributes>> "href" swap key? ] bi and ] filter
138     ] map sift
139     [ [ attributes>> "href" swap at ] map ] map concat
140     [ >url ] map ;
141
142 : find-frame-links ( vector -- vector' )
143     [ name>> "frame" = ] find-between-all
144     [ [ attributes>> "src" swap at ] map sift ] map concat sift
145     [ >url ] map ;
146
147 : find-all-links ( vector -- vector' )
148     [ find-hrefs ] [ find-frame-links ] bi append prune ;
149
150 : find-forms ( vector -- vector' )
151     "form" over find-opening-tags-by-name
152     swap [ [ first2 ] dip find-between* ] curry map
153     [ [ name>> { "form" "input" } member? ] filter ] map ;
154
155 : find-html-objects ( vector string -- vector' )
156     dupd find-opening-tags-by-name
157     [ first2 find-between* ] curry map ;
158
159 : form-action ( vector -- string )
160     [ name>> "form" = ] find nip 
161     attributes>> "action" swap at ;
162
163 : hidden-form-values ( vector -- strings )
164     [ attributes>> "type" swap at "hidden" = ] filter ;
165
166 : input. ( tag -- )
167     dup name>> print
168     attributes>>
169     [ bl bl bl bl [ write "=" write ] [ write bl ] bi* nl ] assoc-each ;
170
171 : form. ( vector -- )
172     [ closing?>> not ] filter
173     [
174         {
175             { [ dup name>> "form" = ]
176                 [ "form action: " write attributes>> "action" swap at print ] }
177             { [ dup name>> "input" = ] [ input. ] }
178             [ drop ]
179         } cond
180     ] each ;
181
182 : query>assoc* ( str -- hash )
183     "?" split1 nip query>assoc ;
184     
185 : html-class? ( tag string -- ? )
186     swap attributes>> "class" swap at = ;
187     
188 : html-id? ( tag string -- ? )
189     swap attributes>> "id" swap at = ;
190
191 : opening-tag? ( tag -- ? )
192     closing?>> not ;