1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.encodings.utf8 io.encodings.binary
4 io.files io.files.temp io.directories html.streams help kernel
5 assocs sequences make words accessors arrays help.topics vocabs
6 vocabs.hierarchy help.vocabs namespaces prettyprint io
7 vocabs.loader serialize fry memoize unicode.case math.order
8 sorting debugger html xml.syntax xml.writer math.parser
10 FROM: io.encodings.ascii => ascii ;
11 FROM: ascii => ascii? ;
14 : escape-char ( ch -- )
18 { CHAR: * "__star__" }
19 { CHAR: : "__colon__" }
23 { CHAR: \\ "__back__" }
24 { CHAR: | "__pipe__" }
25 { CHAR: / "__slash__" }
26 { CHAR: , "__comma__" }
28 { CHAR: # "__hash__" }
30 ] [ number>string "__" "__" surround % ] if ;
32 : escape-filename ( string -- filename )
33 [ [ escape-char ] each ] "" make ;
35 GENERIC: topic>filename* ( topic -- name prefix )
37 M: word topic>filename*
39 [ name>> ] [ vocabulary>> ] bi 2array "word"
42 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
43 M: word-link topic>filename* name>> topic>filename* ;
44 M: vocab-spec topic>filename* vocab-name "vocab" ;
45 M: vocab-tag topic>filename* name>> "tag" ;
46 M: vocab-author topic>filename* name>> "author" ;
47 M: f topic>filename* drop \ f topic>filename* ;
49 : topic>filename ( topic -- filename )
54 [ [ escape-filename ] map "," join ]
60 M: topic url-of topic>filename ;
62 : help-stylesheet ( -- string )
63 "vocab:help/html/stylesheet.css" ascii file-contents
64 [XML <style><-></style> XML] ;
66 : help>html ( topic -- xml )
68 [ drop help-stylesheet ]
69 [ [ print-topic ] with-html-writer ]
72 : generate-help-file ( topic -- )
73 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
75 : all-vocabs-really ( -- seq )
76 all-vocabs-recursive >hashtable no-roots remove-redundant-prefixes
77 [ vocab-name "scratchpad" = not ] filter ;
79 : all-topics ( -- topics )
81 articles get keys [ >link ] map %
82 all-words [ >link ] map %
83 all-authors [ <vocab-author> ] map %
84 all-tags [ <vocab-tag> ] map %
88 : serialize-index ( index file -- )
89 [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
90 binary set-file-contents ;
92 : generate-indices ( -- )
93 articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
94 all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
95 all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
97 : generate-help-files ( -- )
98 all-topics [ '[ _ generate-help-file ] try ] each ;
100 : generate-help ( -- )
110 MEMO: load-index ( name -- index )
111 binary file-contents bytes>object ;
113 TUPLE: result title href ;
115 : offline-apropos ( string index -- results )
116 load-index swap >lower
117 '[ [ drop _ ] dip >lower subseq? ] assoc-filter
118 [ swap result boa ] { } assoc>map
119 [ title>> ] sort-with ;
121 : article-apropos ( string -- results )
122 "articles.idx" offline-apropos ;
124 : word-apropos ( string -- results )
125 "words.idx" offline-apropos ;
127 : vocab-apropos ( string -- results )
128 "vocabs.idx" offline-apropos ;