1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.encodings.utf8 io.encodings.ascii 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 tools.vocabs tools.vocabs.browser namespaces prettyprint io
7 vocabs.loader serialize fry memoize unicode.case math.order
8 sorting debugger html xml.syntax xml.writer ;
11 : escape-char ( ch -- )
14 { CHAR: * "__star__" }
15 { CHAR: : "__colon__" }
19 { CHAR: \\ "__back__" }
20 { CHAR: | "__pipe__" }
21 { CHAR: / "__slash__" }
22 { CHAR: , "__comma__" }
24 } at [ % ] [ , ] ?if ;
26 : escape-filename ( string -- filename )
27 [ [ escape-char ] each ] "" make ;
29 GENERIC: topic>filename* ( topic -- name prefix )
31 M: word topic>filename*
33 [ name>> ] [ vocabulary>> ] bi 2array "word"
36 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
37 M: word-link topic>filename* name>> topic>filename* ;
38 M: vocab-spec topic>filename* vocab-name "vocab" ;
39 M: vocab-tag topic>filename* name>> "tag" ;
40 M: vocab-author topic>filename* name>> "author" ;
41 M: f topic>filename* drop \ f topic>filename* ;
43 : topic>filename ( topic -- filename )
48 [ [ escape-filename ] map "," join ]
54 M: topic url-of topic>filename ;
56 : help-stylesheet ( -- string )
57 "vocab:help/html/stylesheet.css" ascii file-contents
58 [XML <style><-></style> XML] ;
60 : help>html ( topic -- xml )
62 [ drop help-stylesheet ]
63 [ [ help ] with-html-writer ]
66 : generate-help-file ( topic -- )
67 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
69 : all-vocabs-really ( -- seq )
71 all-vocabs values concat
72 vocabs [ find-vocab-root not ] filter [ vocab ] map append ;
74 : all-topics ( -- topics )
76 articles get keys [ >link ] map %
77 all-words [ >link ] map %
78 all-authors [ <vocab-author> ] map %
79 all-tags [ <vocab-tag> ] map %
83 : serialize-index ( index file -- )
84 [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
85 binary set-file-contents ;
87 : generate-indices ( -- )
88 articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
89 all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
90 all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
92 : generate-help-files ( -- )
93 all-topics [ '[ _ generate-help-file ] try ] each ;
95 : generate-help ( -- )
105 MEMO: load-index ( name -- index )
106 binary file-contents bytes>object ;
108 TUPLE: result title href ;
110 : offline-apropos ( string index -- results )
111 load-index swap >lower
112 '[ [ drop _ ] dip >lower subseq? ] assoc-filter
113 [ swap result boa ] { } assoc>map
114 [ [ title>> ] compare ] sort ;
116 : article-apropos ( string -- results )
117 "articles.idx" offline-apropos ;
119 : word-apropos ( string -- results )
120 "words.idx" offline-apropos ;
122 : vocab-apropos ( string -- results )
123 "vocabs.idx" offline-apropos ;