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 fonts formatting fry help help.home help.topics
5 help.vocabs html 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 regexp sequences
8 sequences.deep serialize sorting splitting tools.completion
9 vocabs vocabs.hierarchy words xml.data xml.syntax xml.writer ;
10 FROM: io.encodings.ascii => ascii ;
11 FROM: ascii => ascii? ;
14 : escape-char ( ch -- )
17 { CHAR: \" "__quo__" }
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/factor/factor/blob/master/"
69 : help-stylesheet ( stylesheet -- xml )
70 "vocab:help/html/stylesheet.css" ascii file-contents
71 swap "\n" glue [XML <style><-></style> XML] ;
73 : help-navbar ( -- xml )
74 "conventions" >link topic>filename
77 <a href="/">Handbook</a>
78 <a href=<->>Glossary</a>
79 <form method="get" action="/search" style="display:inline;">
80 <input placeholder="Search" name="search" type="text"/>
81 <input type="submit" value="Go"/>
83 <div style="float: right;">
84 <a href="http://factorcode.org"><b>factorcode.org</b></a>
89 : bijective-base26 ( n -- name )
90 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
92 : css-class ( style classes -- name )
93 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
95 : css-style ( style -- style' )
96 R/ font-size: \d+pt;/ [
97 "font-size: " ?head drop "pt;" ?tail drop
98 string>number default-font-size /f
99 "font-size: %.3frem;" sprintf
102 R/ padding: \d+px;/ [
103 "padding: " ?head drop "px;" ?tail drop
104 string>number dup even? [ 2 * 1 + ] [ 2 * ] if
105 number>string "padding: " "px;" surround
108 : css-classes ( classes -- stylesheet )
110 [ css-style " { " "}" surround ] [ "." prepend ] bi* prepend
111 ] { } assoc>map "\n" join ;
113 :: css-styles-to-classes ( body -- stylesheet body )
114 H{ } clone :> classes
123 [ clone [ V{ } like ] change-alist ] change-attrs
124 "style" over delete-at* drop classes css-class
129 ] each classes sort-values css-classes body ;
131 : help>html ( topic -- xml )
132 [ article-title " - Factor Documentation" append ]
134 [ print-topic ] with-html-writer css-styles-to-classes
135 [ help-stylesheet help-navbar ] dip
136 [XML <div id="container"><-><div class="page"><-></div></div> XML]
139 : generate-help-file ( topic -- )
140 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
142 : all-vocabs-really ( -- seq )
143 all-disk-vocabs-recursive filter-vocabs
144 [ vocab-name "scratchpad" = ] reject ;
146 : all-topics ( -- topics )
148 articles get keys [ >link ] map %
149 all-words [ >link ] map %
150 all-authors [ <vocab-author> ] map %
151 all-tags [ <vocab-tag> ] map %
155 : serialize-index ( index file -- )
157 [ [ topic>filename ] dip ] { } assoc-map-as serialize
160 : generate-article-index ( -- )
161 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
162 "articles.idx" serialize-index ;
164 : generate-word-index ( -- )
165 all-words [ dup name>> ] { } map>assoc
166 "words.idx" serialize-index ;
168 : generate-vocab-index ( -- )
169 all-vocabs-really [ dup vocab-name ] { } map>assoc
170 "vocabs.idx" serialize-index ;
172 : generate-indices ( -- )
173 generate-article-index
175 generate-vocab-index ;
177 : generate-help-files ( -- )
179 { recent-searches f }
181 { recent-articles f }
184 all-topics [ '[ _ generate-help-file ] try ] each
187 : generate-help ( -- )
197 MEMO: load-index ( name -- index )
198 binary file-contents bytes>object ;
200 : offline-apropos ( string index -- results )
201 load-index completions ;
203 : article-apropos ( string -- results )
204 "articles.idx" offline-apropos ;
206 : word-apropos ( string -- results )
207 "words.idx" offline-apropos ;
209 : vocab-apropos ( string -- results )
210 "vocabs.idx" offline-apropos ;