<commit_before>! Copyright (C) 2011 <NAME>
! See http://factorcode.org/license.txt for BSD license

USING: accessors formatting html.entities html.parser
html.parser.analyzer html.parser.printer http.client images.http
images.viewer images.viewer.prettyprint io kernel parser
prettyprint.custom prettyprint.sections regexp sequences strings
ui wrap.strings ;

IN: xkcd

<PRIVATE

: comic-image ( url -- image )
    http-get nip
    R" http://imgs\.xkcd\.com/comics/[^\.]+\.(png|jpg)"
    first-match >string load-http-image ;

: comic-image. ( url -- )
    comic-image image. ;

: comic-text ( url -- string )
    http-get nip parse-html
    "transcript" find-by-id-between
    html-text html-unescape ;

: comic-text. ( url -- )
    comic-text 80 wrap-string print ;

: comic. ( url -- )
    ui-running? [ comic-image. ] [ comic-text. ] if ;

PRIVATE>

: xkcd-url ( n -- url )
    "http://xkcd.com/%s/" sprintf ;

: xkcd-image ( n -- image )
    xkcd-url comic-image ;

: xkcd. ( n -- )
    xkcd-url comic. ;

: random-xkcd. ( -- )
    "http://dynamic.xkcd.com/random/comic/" comic. ;

: latest-xkcd. ( -- )
    "http://xkcd.com" comic. ;

TUPLE: xkcd image ;

C: <xkcd> xkcd

SYNTAX: XKCD: scan-number xkcd-image <xkcd> suffix! ;

M: xkcd pprint* image>> <image-section> add-section ;<commit_msg>xkcd: save the comic number with XKCD: syntax.<commit_after>! Copyright (C) 2011 <NAME>
! See http://factorcode.org/license.txt for BSD license

USING: accessors formatting html.entities html.parser
html.parser.analyzer html.parser.printer http.client images.http
images.viewer images.viewer.prettyprint io kernel parser
prettyprint.custom prettyprint.sections regexp sequences strings
ui wrap.strings ;

IN: xkcd

<PRIVATE

: comic-image ( url -- image )
    http-get nip
    R" http://imgs\.xkcd\.com/comics/[^\.]+\.(png|jpg)"
    first-match >string load-http-image ;

: comic-image. ( url -- )
    comic-image image. ;

: comic-text ( url -- string )
    http-get nip parse-html
    "transcript" find-by-id-between
    html-text html-unescape ;

: comic-text. ( url -- )
    comic-text 80 wrap-string print ;

: comic. ( url -- )
    ui-running? [ comic-image. ] [ comic-text. ] if ;

PRIVATE>

: xkcd-url ( n -- url )
    "http://xkcd.com/%s/" sprintf ;

: xkcd-image ( n -- image )
    xkcd-url comic-image ;

: xkcd. ( n -- )
    xkcd-url comic. ;

: random-xkcd. ( -- )
    "http://dynamic.xkcd.com/random/comic/" comic. ;

: latest-xkcd. ( -- )
    "http://xkcd.com" comic. ;

TUPLE: xkcd number image ;

C: <xkcd> xkcd

SYNTAX: XKCD: scan-number dup xkcd-image <xkcd> suffix! ;

M: xkcd pprint* image>> <image-section> add-section ;
{'fraction_non_alphanumeric': 0.16098484848484848, 'fraction_numerical': 0.004545454545454545, 'mean_word_length': 2.7040673211781208, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5965671', 'n_tokens_mistral': 1022, 'n_tokens_neox': 1003, 'n_words': 227}