]> gitweb.factorcode.org Git - factor.git/blob - extra/html/parser/analyzer/analyzer.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[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 ;
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 : <link> ( vector -- link )
107     [ first attributes>> ]
108     [ [ name>> { text "img" } member? ] filter ] bi
109     link boa ;
110
111 : link. ( vector -- )
112     [ attributes>> "href" swap at write nl ]
113     [ clickable>> [ bl bl text>> print ] each nl ] bi ;
114
115 : find-by-text ( seq quot -- tag )
116     [ dup name>> text = ] prepose find drop ; inline
117
118 : find-opening-tags-by-name ( name seq -- seq )
119     [ [ name>> = ] [ closing?>> not ] bi and ] with find-all ;
120
121 : href-contains? ( str tag -- ? )
122     attributes>> "href" swap at* [ subseq? ] [ 2drop f ] if ;
123
124 : find-hrefs ( vector -- vector' )
125     find-links
126     [ [
127         [ name>> "a" = ]
128         [ attributes>> "href" swap key? ] bi and ] filter
129     ] map sift
130     [ [ attributes>> "href" swap at ] map ] map concat
131     [ >url ] map ;
132
133 : find-frame-links ( vector -- vector' )
134     [ name>> "frame" = ] find-between-all
135     [ [ attributes>> "src" swap at ] map sift ] map concat sift
136     [ >url ] map ;
137
138 : find-all-links ( vector -- vector' )
139     [ find-hrefs ] [ find-frame-links ] bi append prune ;
140
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 ;
145
146 : find-html-objects ( vector string -- vector' )
147     dupd find-opening-tags-by-name
148     [ first2 find-between* ] curry map ;
149
150 : form-action ( vector -- string )
151     [ name>> "form" = ] find nip 
152     attributes>> "action" swap at ;
153
154 : hidden-form-values ( vector -- strings )
155     [ attributes>> "type" swap at "hidden" = ] filter ;
156
157 : input. ( tag -- )
158     dup name>> print
159     attributes>>
160     [ bl bl bl bl [ write "=" write ] [ write bl ] bi* nl ] assoc-each ;
161
162 : form. ( vector -- )
163     [ closing?>> not ] filter
164     [
165         {
166             { [ dup name>> "form" = ]
167                 [ "form action: " write attributes>> "action" swap at print ] }
168             { [ dup name>> "input" = ] [ input. ] }
169             [ drop ]
170         } cond
171     ] each ;
172
173 : query>assoc* ( str -- hash )
174     "?" split1 nip query>assoc ;
175     
176 : html-class? ( tag string -- ? )
177     swap attributes>> "class" swap at = ;
178     
179 : html-id? ( tag string -- ? )
180     swap attributes>> "id" swap at = ;
181
182 : opening-tag? ( tag -- ? )
183     closing?>> not ;