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 : $navigation-row ( content element label -- )
89 [ prefix 1array ] dip prefix , ;
91 : ($navigation-links) ( topic -- )
94 [ prev-article [ 1array \ $long-link "Prev:" $navigation-row ] when* ]
95 [ next-article [ 1array \ $long-link "Next:" $navigation-row ] when* ]
97 ] { } make [ ($navigation-table) ] unless-empty
100 : $title ( topic -- )
102 { { page-color COLOR: FactorLightTan } } assoc-union dup
106 [ ($navigation-path) ]
107 [ ($navigation-links) ] tri
111 : help>html ( topic -- xml )
112 [ article-title " - Factor Documentation" append ]
113 [ drop help-stylesheet ]
116 [ [ [ $title ($blank-line) ] [ print-topic ] bi ] with-html-writer ]
121 : generate-help-file ( topic -- )
122 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
124 : all-vocabs-really ( -- seq )
125 all-disk-vocabs-recursive no-roots remove-redundant-prefixes
126 [ vocab-name "scratchpad" = ] reject ;
128 : all-topics ( -- topics )
130 articles get keys [ >link ] map %
131 all-words [ >link ] map %
132 all-authors [ <vocab-author> ] map %
133 all-tags [ <vocab-tag> ] map %
137 : serialize-index ( index file -- )
139 [ [ topic>filename ] dip ] { } assoc-map-as serialize
142 : generate-article-index ( -- )
143 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
144 "articles.idx" serialize-index ;
146 : generate-word-index ( -- )
147 all-words [ dup name>> ] { } map>assoc
148 "words.idx" serialize-index ;
150 : generate-vocab-index ( -- )
151 all-vocabs-really [ dup vocab-name ] { } map>assoc
152 "vocabs.idx" serialize-index ;
154 : generate-indices ( -- )
155 generate-article-index
157 generate-vocab-index ;
159 : generate-help-files ( -- )
161 { recent-searches f }
163 { recent-articles f }
166 all-topics [ '[ _ generate-help-file ] try ] each
169 : generate-help ( -- )
179 MEMO: load-index ( name -- index )
180 binary file-contents bytes>object ;
182 : offline-apropos ( string index -- results )
183 load-index completions ;
185 : article-apropos ( string -- results )
186 "articles.idx" offline-apropos ;
188 : word-apropos ( string -- results )
189 "words.idx" offline-apropos ;
191 : vocab-apropos ( string -- results )
192 "vocabs.idx" offline-apropos ;