1 ! Copyright (C) 2011 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
4 USING: accessors colors formatting http http.client images.gif
5 images.http io io.styles kernel namespaces sequences splitting
6 ui urls.encoding xml xml.data xml.traversal ;
10 SYMBOL: wolfram-api-id
12 ! "XXXXXX-XXXXXXXXXX" wolfram-api-id set-global
16 : query ( query -- xml )
17 url-encode wolfram-api-id get-global
18 "http://api.wolframalpha.com/v2/query?input=%s&appid=%s"
19 sprintf http-get nip string>xml
20 dup "error" tag-named [
21 "msg" tag-named children>string throw
26 : wolfram-image. ( query -- )
27 query "pod" tags-named [
29 "title" attr "%s:\n" sprintf H{
30 { foreground COLOR: slate-gray }
31 { font-name "sans-serif" }
35 "img" deep-tags-named [
36 "src" attr " " write http-image.
41 : wolfram-text. ( query -- )
42 query "pod" tags-named [
43 [ "title" attr "%s:\n" printf ]
45 "plaintext" deep-tags-named [
46 children>string split-lines
47 [ " %s\n" printf ] each
52 : wolfram. ( query -- )
53 ui-running? [ wolfram-image. ] [ wolfram-text. ] if ;