]> gitweb.factorcode.org Git - factor.git/commitdiff
modern.html: Add walk-html helper function
authorDoug Coleman <doug.coleman@gmail.com>
Fri, 3 Jun 2022 03:33:26 +0000 (22:33 -0500)
committerDoug Coleman <doug.coleman@gmail.com>
Fri, 3 Jun 2022 03:45:10 +0000 (22:45 -0500)
Example:
USE: modern.html
"https://en.wikipedia.org/w/index.php?title=Factor_(programming_language)&offset=&limit=500&action=history" http-get
nip string>html
[
  [
    dup tag? [ props>> [ drop  >lower "href" = ] assoc-find [ nip , ] [ 2drop ] if ] [ drop ] if
  ] walk-html
] { } make [ payload>> ] map

extra/modern/html/html.factor

index 7c90db4384e34237d96f5b5f115bddd991601875..f91b7c5badb81b03675f40c8b690bbc3807d6dd5 100644 (file)
@@ -240,3 +240,13 @@ M: string write-html % ;
 
 : html>string ( sequence -- string )
     [ [ write-html ] each ] "" make ;
+
+GENERIC#: walk-html 1 ( seq/tag quot -- )
+
+M: sequence walk-html [ walk-html ] curry each ;
+M: string walk-html call( obj -- ) ;
+M: doctype walk-html call( obj -- ) ;
+M: processing-instruction walk-html call( obj -- ) ;
+M: open-tag walk-html [ call( obj -- ) ] 2keep [ children>> ] dip [ walk-html ] curry each ;
+M: self-close-tag walk-html [ call( obj -- ) ] 2keep [ children>> ] dip [ walk-html ] curry each ;
+M: comment walk-html call( obj -- ) ;