! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: assocs html.parser kernel math sequences strings ascii
-arrays generalizations shuffle unicode.case namespaces make
+arrays generalizations shuffle namespaces make
splitting http accessors io combinators http.client urls
-fry sequences.lib ;
+urls.encoding fry prettyprint sets ;
IN: html.parser.analyzer
TUPLE: link attributes clickable ;
-: scrape-html ( url -- vector )
- http-get nip parse-html ;
+: scrape-html ( url -- headers vector )
+ http-get parse-html ;
: find-all ( seq quot -- alist )
[ <enum> >alist ] [ '[ second @ ] ] bi* filter ; inline
: find-nth ( seq quot n -- i elt )
[ <enum> >alist ] 2dip -rot
- '[ _ [ second @ ] find-from rot drop swap 1+ ]
+ '[ _ [ second @ ] find-from rot drop swap 1 + ]
[ f 0 ] 2dip times drop first2 ; inline
-: find-first-name ( str vector -- i/f tag/f )
- [ >lower ] dip [ name>> = ] with find ; inline
+: find-first-name ( vector string -- i/f tag/f )
+ >lower '[ name>> _ = ] find ; inline
-: find-matching-close ( str vector -- i/f tag/f )
- [ >lower ] dip
- [ [ name>> = ] [ closing?>> ] bi and ] with find ; inline
+: find-matching-close ( vector string -- i/f tag/f )
+ >lower
+ '[ [ name>> _ = ] [ closing?>> ] bi and ] find ; inline
-: find-between* ( i/f tag/f vector -- vector )
- pick integer? [
- rot tail-slice
- >r name>> r>
- [ find-matching-close drop dup [ 1+ ] when ] keep
- swap [ head ] [ first ] if*
+: find-between* ( vector i/f tag/f -- vector )
+ over integer? [
+ [ tail-slice ] [ name>> ] bi*
+ dupd find-matching-close drop dup [ 1 + ] when
+ [ head ] [ first ] if*
] [
3drop V{ } clone
] if ; inline
-
-: find-between ( i/f tag/f vector -- vector )
+
+: find-between ( vector i/f tag/f -- vector )
find-between* dup length 3 >= [
[ rest-slice but-last-slice ] keep like
] when ; inline
-: find-between-first ( string vector -- vector' )
- [ find-first-name ] keep find-between ; inline
+: find-between-first ( vector string -- vector' )
+ dupd find-first-name find-between ; inline
: find-between-all ( vector quot -- seq )
- [ [ [ closing?>> not ] bi and ] curry find-all ] curry
- [ [ >r first2 r> find-between* ] curry map ] bi ; inline
-
+ dupd
+ '[ _ [ closing?>> not ] bi and ] find-all
+ [ first2 find-between* ] with map ; inline
: remove-blank-text ( vector -- vector' )
[
[ [ [ blank? ] trim ] change-text ] when
] map ;
-: find-by-id ( id vector -- vector )
- [ attributes>> "id" swap at = ] with filter ;
-
-: find-by-class ( id vector -- vector )
- [ attributes>> "class" swap at = ] with filter ;
-
-: find-by-name ( str vector -- vector )
- [ >lower ] dip [ name>> = ] with filter ;
+: find-by-id ( vector id -- vector' elt/f )
+ '[ attributes>> "id" at _ = ] find ;
+
+: find-by-class ( vector id -- vector' elt/f )
+ '[ attributes>> "class" at _ = ] find ;
-: find-by-attribute-key ( key vector -- vector )
- [ >lower ] dip
- [ attributes>> at ] with filter
- sift ;
+: find-by-name ( vector string -- vector elt/f )
+ >lower '[ name>> _ = ] find ;
-: find-by-attribute-key-value ( value key vector -- vector )
- [ >lower ] dip
+: find-by-id-between ( vector string -- vector' )
+ dupd
+ '[ attributes>> "id" swap at _ = ] find find-between* ;
+
+: find-by-class-between ( vector string -- vector' )
+ dupd
+ '[ attributes>> "class" swap at _ = ] find find-between* ;
+
+: find-by-class-id-between ( vector class id -- vector' )
+ '[
+ [ attributes>> "class" swap at _ = ]
+ [ attributes>> "id" swap at _ = ] bi and
+ ] dupd find find-between* ;
+
+: find-by-attribute-key ( vector key -- vector' elt/? )
+ >lower
+ [ attributes>> at _ = ] filter sift ;
+
+: find-by-attribute-key-value ( vector value key -- vector' )
+ >lower
[ attributes>> at over = ] with filter nip
sift ;
-: find-first-attribute-key-value ( value key vector -- i/f tag/f )
- [ >lower ] dip
+: find-first-attribute-key-value ( vector value key -- i/f tag/f )
+ >lower
[ attributes>> at over = ] with find rot drop ;
: tag-link ( tag -- link/f )
[ clickable>> [ bl bl text>> print ] each nl ] bi ;
: find-by-text ( seq quot -- tag )
- [ dup name>> text = ] prepose find drop ;
+ [ dup name>> text = ] prepose find drop ; inline
: find-opening-tags-by-name ( name seq -- seq )
[ [ name>> = ] [ closing?>> not ] bi and ] with find-all ;
[ [
[ name>> "a" = ]
[ attributes>> "href" swap key? ] bi and ] filter
- ] map sift [ [ attributes>> "href" swap at ] map ] map concat ;
+ ] map sift
+ [ [ attributes>> "href" swap at ] map ] map concat
+ [ >url ] map ;
+
+: find-frame-links ( vector -- vector' )
+ [ name>> "frame" = ] find-between-all
+ [ [ attributes>> "src" swap at ] map sift ] map concat sift
+ [ >url ] map ;
+
+: find-all-links ( vector -- vector' )
+ [ find-hrefs ] [ find-frame-links ] bi append prune ;
: find-forms ( vector -- vector' )
"form" over find-opening-tags-by-name
- swap [ >r first2 r> find-between* ] curry map
+ swap [ [ first2 ] dip find-between* ] curry map
[ [ name>> { "form" "input" } member? ] filter ] map ;
-: find-html-objects ( string vector -- vector' )
- [ find-opening-tags-by-name ] keep
- [ [ first2 ] dip find-between* ] curry map ;
+: find-html-objects ( vector string -- vector' )
+ dupd find-opening-tags-by-name
+ [ first2 find-between* ] curry map ;
: form-action ( vector -- string )
[ name>> "form" = ] find nip
: query>assoc* ( str -- hash )
"?" split1 nip query>assoc ;
+
+: html-class? ( tag string -- ? )
+ swap attributes>> "class" swap at = ;
+
+: html-id? ( tag string -- ? )
+ swap attributes>> "id" swap at = ;
+
+: opening-tag? ( tag -- ? )
+ closing?>> not ;