1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.encodings.utf8 io.encodings.binary io.files
4 io.files.temp io.directories html.streams help help.home 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__" }
29 { CHAR: % "__percent__" }
31 ] [ number>string "__" "__" surround % ] if ;
33 : escape-filename ( string -- filename )
34 [ [ escape-char ] each ] "" make ;
36 GENERIC: topic>filename* ( topic -- name prefix )
38 M: word topic>filename*
40 [ name>> ] [ vocabulary>> ] bi 2array "word"
43 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
44 M: word-link topic>filename* name>> topic>filename* ;
45 M: vocab-spec topic>filename* vocab-name "vocab" ;
46 M: vocab-tag topic>filename* name>> "tag" ;
47 M: vocab-author topic>filename* name>> "author" ;
48 M: f topic>filename* drop \ f topic>filename* ;
50 : topic>filename ( topic -- filename )
55 [ [ escape-filename ] map "," join ]
61 M: topic url-of topic>filename ;
63 : help-stylesheet ( -- xml )
64 "vocab:help/html/stylesheet.css" ascii file-contents
65 [XML <style><-></style> XML] ;
67 : help-navbar ( -- xml )
70 <b> Factor Documentation </b> |
71 <a href="/">Home</a> |
72 <a href="article-conventions.html">Glossary</a> |
73 <form method="post" action="/search" style="display:inline;">
74 <input name="search" type="text"/>
75 <button type="submit">Search</button>
77 <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
81 : help>html ( topic -- xml )
82 [ article-title " - Factor Documentation" append ]
83 [ drop help-stylesheet ]
84 [ [ print-topic ] with-html-writer help-navbar prepend ]
87 : generate-help-file ( topic -- )
88 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
90 : all-vocabs-really ( -- seq )
91 all-vocabs-recursive >hashtable no-roots remove-redundant-prefixes
92 [ vocab-name "scratchpad" = not ] filter ;
94 : all-topics ( -- topics )
96 articles get keys [ >link ] map %
97 all-words [ >link ] map %
98 all-authors [ <vocab-author> ] map %
99 all-tags [ <vocab-tag> ] map %
103 : serialize-index ( index file -- )
104 [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
105 binary set-file-contents ;
107 : generate-indices ( -- )
108 articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
109 all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
110 all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
112 : (generate-help-files) ( -- )
113 all-topics [ '[ _ generate-help-file ] try ] each ;
115 : generate-help-files ( -- )
121 (generate-help-files)
124 : generate-help ( -- )
134 MEMO: load-index ( name -- index )
135 binary file-contents bytes>object ;
137 TUPLE: result title href ;
139 : partition-exact ( string results -- results' )
140 [ title>> = ] with partition append ;
142 : offline-apropos ( string index -- results )
143 load-index over >lower
144 '[ [ drop _ ] dip >lower subseq? ] assoc-filter
145 [ swap result boa ] { } assoc>map
146 [ title>> ] sort-with
149 : article-apropos ( string -- results )
150 "articles.idx" offline-apropos ;
152 : word-apropos ( string -- results )
153 "words.idx" offline-apropos ;
155 : vocab-apropos ( string -- results )
156 "vocabs.idx" offline-apropos ;