1 ! Copyright (C) 2011 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors formatting html.entities html.parser
5 html.parser.analyzer html.parser.printer http.client images.http
6 images.viewer images.viewer.prettyprint io kernel parser
7 prettyprint.custom prettyprint.sections regexp sequences strings
14 : comic-image ( url -- image )
16 R/ \/\/imgs\.xkcd\.com\\/comics\\/[^\.]+\.(png|jpg)/
17 first-match >string "http:" prepend load-http-image ;
19 : comic-image. ( url -- )
22 : comic-text ( url -- string )
23 scrape-html nip "transcript" find-by-id-between
24 html-text html-unescape ;
26 : comic-text. ( url -- )
27 comic-text 80 wrap-string print ;
30 ui-running? [ comic-image. ] [ comic-text. ] if ;
34 : xkcd-url ( n -- url )
35 "http://xkcd.com/%s/" sprintf ;
37 : xkcd-image ( n -- image )
38 xkcd-url comic-image ;
44 "http://dynamic.xkcd.com/random/comic/" comic. ;
47 "http://xkcd.com" comic. ;
49 TUPLE: xkcd number image ;
53 SYNTAX: XKCD: scan-number dup xkcd-image <xkcd> suffix! ;
55 M: xkcd pprint* image>> <image-section> add-section ;