1 ! Copyright (C) 2008 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 html.streams html.elements html.components 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 ;
10 : escape-char ( ch -- )
12 { CHAR: " "__quote__" }
13 { CHAR: * "__star__" }
14 { CHAR: : "__colon__" }
17 { CHAR: ? "__question__" }
18 { CHAR: \\ "__backslash__" }
19 { CHAR: | "__pipe__" }
20 { CHAR: _ "__underscore__" }
21 { CHAR: / "__slash__" }
22 { CHAR: \\ "__backslash__" }
23 { 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* [ name>> ] [ vocabulary>> ] bi 2array "word" ;
32 M: link topic>filename* name>> "article" ;
33 M: word-link topic>filename* name>> topic>filename* ;
34 M: vocab-spec topic>filename* vocab-name "vocab" ;
35 M: vocab-tag topic>filename* name>> "tag" ;
36 M: vocab-author topic>filename* name>> "author" ;
38 : topic>filename ( topic -- filename )
40 topic>filename* % "-" %
42 [ [ escape-filename ] map "," join ]
47 M: topic browser-link-href topic>filename ;
49 : help-stylesheet ( -- )
50 "resource:basis/help/html/stylesheet.css" ascii file-contents write ;
52 : help>html ( topic -- )
53 dup topic>filename utf8 [
55 [ <style> help-stylesheet </style> ]
56 [ [ help ] with-html-writer ] simple-page
59 : all-vocabs-really ( -- seq )
61 all-vocabs values concat
62 vocabs [ find-vocab-root not ] filter [ vocab ] map append ;
64 : all-topics ( -- topics )
66 ! articles get keys [ >link ] map %
67 ! all-words [ >link ] map %
68 ! all-authors [ <vocab-author> ] map %
69 all-tags [ <vocab-tag> ] map %
73 : serialize-index ( index file -- )
74 [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
75 binary set-file-contents ;
77 : generate-indices ( -- )
78 articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
79 all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
80 all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
82 : generate-help ( -- )
83 all-topics [ help>html ] each ;
85 MEMO: load-index ( name -- index )
86 binary file-contents bytes>object ;
88 TUPLE: result title href ;
90 M: result link-title title>> ;
92 M: result link-href href>> ;
94 : offline-apropos ( string index -- results )
95 load-index swap >lower
96 '[ [ drop _ ] dip >lower subseq? ] assoc-filter
97 [ swap result boa ] { } assoc>map ;
99 : article-apropos ( string -- results )
100 "articles.idx" offline-apropos ;
102 : word-apropos ( string -- results )
103 "words.idx" offline-apropos ;
105 : vocab-apropos ( string -- results )
106 "vocabs.idx" offline-apropos ;