1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators.short-circuit
4 debugger fry help help.home help.topics help.vocabs html
5 html.streams io.directories io.encodings.binary
6 io.encodings.utf8 io.files io.files.temp io.pathnames kernel
7 locals make math math.parser memoize namespaces sequences
8 sequences.deep serialize sorting splitting tools.completion
9 vocabs vocabs.hierarchy words xml.data xml.syntax xml.traversal
11 FROM: io.encodings.ascii => ascii ;
12 FROM: ascii => ascii? ;
15 : escape-char ( ch -- )
18 { CHAR: \" "__quo__" }
19 { CHAR: * "__star__" }
20 { CHAR: : "__colon__" }
24 { CHAR: \\ "__back__" }
25 { CHAR: | "__pipe__" }
26 { CHAR: / "__slash__" }
27 { CHAR: , "__comma__" }
29 { CHAR: # "__hash__" }
30 { CHAR: % "__percent__" }
32 ] [ number>string "__" "__" surround % ] if ;
34 : escape-filename ( string -- filename )
35 [ [ escape-char ] each ] "" make ;
37 GENERIC: topic>filename* ( topic -- name prefix )
39 M: word topic>filename*
41 [ name>> ] [ vocabulary>> ] bi 2array "word"
44 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
45 M: word-link topic>filename* name>> topic>filename* ;
46 M: vocab-spec topic>filename* vocab-name "vocab" ;
47 M: vocab-tag topic>filename* name>> "tag" ;
48 M: vocab-author topic>filename* name>> "author" ;
49 M: f topic>filename* drop \ f topic>filename* ;
51 : topic>filename ( topic -- filename )
56 [ [ escape-filename ] map "," join ]
62 M: topic url-of topic>filename ;
65 string>> "resource:" ?head [
66 "https://github.com/factor/factor/blob/master/"
70 : help-stylesheet ( stylesheet -- xml )
71 "vocab:help/html/stylesheet.css" ascii file-contents
72 swap "\n" glue [XML <style><-></style> XML] ;
74 : help-navbar ( -- xml )
75 "conventions" >link topic>filename
78 <a href="http://factorcode.org">factorcode.org</a>
79 <a href="/">Handbook</a>
80 <a href=<->>Glossary</a>
81 <form method="get" action="/search" style="display:inline;">
82 <input placeholder="Search" name="search" type="text"/>
83 <input type="submit" value="Go"/>
88 : bijective-base26 ( n -- name )
89 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
91 : css-class ( style classes -- name )
92 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
94 : css-classes ( classes -- stylesheet )
96 [ " { " "}" surround ] [ "." prepend ] bi* prepend
97 ] { } assoc>map "\n" join ;
99 :: css-styles-to-classes ( body -- stylesheet body )
100 H{ } clone :> classes
109 attrs>> [ V{ } like ] change-alist
110 "style" over delete-at* drop classes css-class
115 ] each classes sort-values css-classes body ;
117 : help>html ( topic -- xml )
118 [ article-title " - Factor Documentation" append ]
120 [ print-topic ] with-html-writer css-styles-to-classes
121 [ help-stylesheet help-navbar ] dip
122 [XML <-><div class="page"><-></div> XML]
125 : generate-help-file ( topic -- )
126 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
128 : all-vocabs-really ( -- seq )
129 all-disk-vocabs-recursive filter-vocabs
130 [ vocab-name "scratchpad" = ] reject ;
132 : all-topics ( -- topics )
134 articles get keys [ >link ] map %
135 all-words [ >link ] map %
136 all-authors [ <vocab-author> ] map %
137 all-tags [ <vocab-tag> ] map %
141 : serialize-index ( index file -- )
143 [ [ topic>filename ] dip ] { } assoc-map-as serialize
146 : generate-article-index ( -- )
147 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
148 "articles.idx" serialize-index ;
150 : generate-word-index ( -- )
151 all-words [ dup name>> ] { } map>assoc
152 "words.idx" serialize-index ;
154 : generate-vocab-index ( -- )
155 all-vocabs-really [ dup vocab-name ] { } map>assoc
156 "vocabs.idx" serialize-index ;
158 : generate-indices ( -- )
159 generate-article-index
161 generate-vocab-index ;
163 : generate-help-files ( -- )
165 { recent-searches f }
167 { recent-articles f }
170 all-topics [ '[ _ generate-help-file ] try ] each
173 : generate-help ( -- )
183 MEMO: load-index ( name -- index )
184 binary file-contents bytes>object ;
186 : offline-apropos ( string index -- results )
187 load-index completions ;
189 : article-apropos ( string -- results )
190 "articles.idx" offline-apropos ;
192 : word-apropos ( string -- results )
193 "words.idx" offline-apropos ;
195 : vocab-apropos ( string -- results )
196 "vocabs.idx" offline-apropos ;