1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs debugger fry help help.home
4 help.topics help.vocabs html html.streams io.directories
5 io.encodings.binary io.encodings.utf8 io.files io.files.temp
6 io.pathnames kernel make math.parser memoize namespaces
7 sequences serialize splitting tools.completion vocabs
8 vocabs.hierarchy words xml.syntax xml.writer ;
9 FROM: io.encodings.ascii => ascii ;
10 FROM: ascii => ascii? ;
13 : escape-char ( ch -- )
17 { CHAR: * "__star__" }
18 { CHAR: : "__colon__" }
22 { CHAR: \\ "__back__" }
23 { CHAR: | "__pipe__" }
24 { CHAR: / "__slash__" }
25 { CHAR: , "__comma__" }
27 { CHAR: # "__hash__" }
28 { CHAR: % "__percent__" }
30 ] [ number>string "__" "__" surround % ] if ;
32 : escape-filename ( string -- filename )
33 [ [ escape-char ] each ] "" make ;
35 GENERIC: topic>filename* ( topic -- name prefix )
37 M: word topic>filename*
39 [ name>> ] [ vocabulary>> ] bi 2array "word"
42 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
43 M: word-link topic>filename* name>> topic>filename* ;
44 M: vocab-spec topic>filename* vocab-name "vocab" ;
45 M: vocab-tag topic>filename* name>> "tag" ;
46 M: vocab-author topic>filename* name>> "author" ;
47 M: f topic>filename* drop \ f topic>filename* ;
49 : topic>filename ( topic -- filename )
54 [ [ escape-filename ] map "," join ]
60 M: topic url-of topic>filename ;
63 string>> "resource:" ?head [
64 "https://github.com/slavapestov/factor/blob/master/"
68 : help-stylesheet ( -- xml )
69 "vocab:help/html/stylesheet.css" ascii file-contents
70 [XML <style><-></style> XML] ;
72 : help-navbar ( -- xml )
73 "conventions" >link topic>filename
76 <b> Factor Documentation </b> |
77 <a href="/">Home</a> |
78 <a href=<->>Glossary</a> |
79 <form method="get" action="/search" style="display:inline;">
80 <input name="search" type="text"/>
81 <button type="submit">Search</button>
83 <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
87 : help>html ( topic -- xml )
88 [ article-title " - Factor Documentation" append ]
89 [ drop help-stylesheet ]
92 [ [ print-topic ] with-html-writer ]
97 : generate-help-file ( topic -- )
98 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
100 : all-vocabs-really ( -- seq )
101 all-vocabs-recursive no-roots remove-redundant-prefixes
102 [ vocab-name "scratchpad" = ] reject ;
104 : all-topics ( -- topics )
106 articles get keys [ >link ] map %
107 all-words [ >link ] map %
108 all-authors [ <vocab-author> ] map %
109 all-tags [ <vocab-tag> ] map %
113 : serialize-index ( index file -- )
115 [ [ topic>filename ] dip ] { } assoc-map-as serialize
118 : generate-article-index ( -- )
119 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
120 "articles.idx" serialize-index ;
122 : generate-word-index ( -- )
123 all-words [ dup name>> ] { } map>assoc
124 "words.idx" serialize-index ;
126 : generate-vocab-index ( -- )
127 all-vocabs-really [ dup vocab-name ] { } map>assoc
128 "vocabs.idx" serialize-index ;
130 : generate-indices ( -- )
131 generate-article-index
133 generate-vocab-index ;
135 : generate-help-files ( -- )
137 { recent-searches f }
139 { recent-articles f }
142 all-topics [ '[ _ generate-help-file ] try ] each
145 : generate-help ( -- )
155 MEMO: load-index ( name -- index )
156 binary file-contents bytes>object ;
158 : offline-apropos ( string index -- results )
159 load-index completions ;
161 : article-apropos ( string -- results )
162 "articles.idx" offline-apropos ;
164 : word-apropos ( string -- results )
165 "words.idx" offline-apropos ;
167 : vocab-apropos ( string -- results )
168 "vocabs.idx" offline-apropos ;