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" http://imgs\.xkcd\.com/comics/[^\.]+\.(png|jpg)"
17 first-match >string load-http-image ;
19 : comic-image. ( url -- )
22 : comic-text ( url -- string )
23 http-get nip parse-html
24 "transcript" find-by-id-between
25 html-text html-unescape ;
27 : comic-text. ( url -- )
28 comic-text 80 wrap-string print ;
31 ui-running? [ comic-image. ] [ comic-text. ] if ;
35 : xkcd-url ( n -- url )
36 "http://xkcd.com/%s/" sprintf ;
38 : xkcd-image ( n -- image )
39 xkcd-url comic-image ;
45 "http://dynamic.xkcd.com/random/comic/" comic. ;
48 "http://xkcd.com" comic. ;
54 SYNTAX: XKCD: scan-number xkcd-image <xkcd> suffix! ;
56 M: xkcd pprint* image>> <image-section> add-section ;