1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs colors.constants debugger fry
4 help help.crossref help.home help.markup help.stylesheet
5 help.topics help.vocabs html html.streams io io.directories
6 io.encodings.binary io.encodings.utf8 io.files io.files.temp
7 io.pathnames io.styles kernel make math.parser memoize
8 namespaces sequences serialize splitting tools.completion vocabs
9 vocabs.hierarchy words xml.syntax xml.writer ;
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 ;
64 string>> "resource:" ?head [
65 "https://github.com/slavapestov/factor/blob/master/"
69 : help-stylesheet ( -- xml )
70 "vocab:help/html/stylesheet.css" ascii file-contents
71 [XML <style><-></style> XML] ;
73 : help-navbar ( -- xml )
74 "conventions" >link topic>filename
77 <b> Factor Documentation </b> |
78 <a href="/">Home</a> |
79 <a href=<->>Glossary</a> |
80 <form method="get" action="/search" style="display:inline;">
81 <input name="search" type="text"/>
82 <button type="submit">Search</button>
84 <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
88 : help>html ( topic -- xml )
89 [ article-title " - Factor Documentation" append ]
90 [ drop help-stylesheet ]
96 { { page-color COLOR: FactorLightTan } } assoc-union
97 title-style [ print-topic ] with-variable
103 : generate-help-file ( topic -- )
104 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
106 : all-vocabs-really ( -- seq )
107 all-disk-vocabs-recursive no-roots remove-redundant-prefixes
108 [ vocab-name "scratchpad" = ] reject ;
110 : all-topics ( -- topics )
112 articles get keys [ >link ] map %
113 all-words [ >link ] map %
114 all-authors [ <vocab-author> ] map %
115 all-tags [ <vocab-tag> ] map %
119 : serialize-index ( index file -- )
121 [ [ topic>filename ] dip ] { } assoc-map-as serialize
124 : generate-article-index ( -- )
125 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
126 "articles.idx" serialize-index ;
128 : generate-word-index ( -- )
129 all-words [ dup name>> ] { } map>assoc
130 "words.idx" serialize-index ;
132 : generate-vocab-index ( -- )
133 all-vocabs-really [ dup vocab-name ] { } map>assoc
134 "vocabs.idx" serialize-index ;
136 : generate-indices ( -- )
137 generate-article-index
139 generate-vocab-index ;
141 : generate-help-files ( -- )
143 { recent-searches f }
145 { recent-articles f }
148 all-topics [ '[ _ generate-help-file ] try ] each
151 : generate-help ( -- )
161 MEMO: load-index ( name -- index )
162 binary file-contents bytes>object ;
164 : offline-apropos ( string index -- results )
165 load-index completions ;
167 : article-apropos ( string -- results )
168 "articles.idx" offline-apropos ;
170 : word-apropos ( string -- results )
171 "words.idx" offline-apropos ;
173 : vocab-apropos ( string -- results )
174 "vocabs.idx" offline-apropos ;