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 formatting fry help help.home help.topics help.vocabs
5 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.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-meta ( -- xml )
77 content="width=device-width, initial-scale=1"
80 : help-navbar ( -- xml )
81 "conventions" >link topic>filename
84 <a href="/">Handbook</a>
85 <a href=<->>Glossary</a>
86 <form method="get" action="/search" style="float: right;">
87 <input placeholder="Search" name="search" type="text"/>
88 <input type="submit" value="Go"/>
89 <a href="//factorcode.org">factorcode.org</a>
94 : bijective-base26 ( n -- name )
95 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
97 : css-class ( style classes -- name )
98 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
100 : css-style ( style -- style' )
101 R/ font-size: \d+pt;/ [
102 "font-size: " ?head drop "pt;" ?tail drop
104 "font-size: %dpt;" sprintf
107 R/ padding: \d+px;/ [
108 "padding: " ?head drop "px;" ?tail drop
109 string>number dup even? [ 2 * 1 + ] [ 2 * ] if
110 number>string "padding: " "px;" surround
117 R/ font-family: monospace;/ [
118 " white-space: pre-wrap; line-height: 125%;" append
121 : css-classes ( classes -- stylesheet )
123 [ css-style " { " "}" surround ] [ "." prepend ] bi* prepend
124 ] { } assoc>map "\n" join ;
126 :: css-styles-to-classes ( body -- stylesheet body )
127 H{ } clone :> classes
136 [ clone [ V{ } like ] change-alist ] change-attrs
137 "style" over delete-at* drop classes css-class
142 ] each classes sort-values css-classes body ;
144 : retina-image ( path -- path' )
145 "@2x" over subseq? [ "." split1-last "@2x." glue ] unless ;
147 : ?copy-file ( from to -- )
148 dup exists? [ 2drop ] [ copy-file ] if ;
150 : cache-images ( body -- body' )
154 T{ name { main "img" } } over tag-named? [
156 retina-image dup file-name
164 : help>html ( topic -- xml )
165 [ article-title " - Factor Documentation" append ]
167 [ print-topic ] with-html-writer
168 css-styles-to-classes cache-images
169 [ help-stylesheet help-meta prepend help-navbar ] dip
170 [XML <div id="container"><-><div class="page"><-></div></div> XML]
173 : generate-help-file ( topic -- )
174 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
176 : all-vocabs-really ( -- seq )
177 all-disk-vocabs-recursive filter-vocabs
178 [ vocab-name "scratchpad" = ] reject ;
180 : all-topics ( -- topics )
182 articles get keys [ >link ] map %
183 all-words [ >link ] map %
184 all-authors [ <vocab-author> ] map %
185 all-tags [ <vocab-tag> ] map %
189 : serialize-index ( index file -- )
191 [ [ topic>filename ] dip ] { } assoc-map-as serialize
194 : generate-article-index ( -- )
195 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
196 "articles.idx" serialize-index ;
198 : generate-word-index ( -- )
199 all-words [ dup name>> ] { } map>assoc
200 "words.idx" serialize-index ;
202 : generate-vocab-index ( -- )
203 all-vocabs-really [ dup vocab-name ] { } map>assoc
204 "vocabs.idx" serialize-index ;
206 : generate-indices ( -- )
207 generate-article-index
209 generate-vocab-index ;
211 : generate-help-files ( -- )
213 { recent-searches f }
215 { recent-articles f }
218 all-topics [ '[ _ generate-help-file ] try ] each
221 : generate-help ( -- )
231 MEMO: load-index ( name -- index )
232 binary file-contents bytes>object ;
234 : offline-apropos ( string index -- results )
235 load-index completions ;
237 : article-apropos ( string -- results )
238 "articles.idx" offline-apropos ;
240 : word-apropos ( string -- results )
241 "words.idx" offline-apropos ;
243 : vocab-apropos ( string -- results )
244 "vocabs.idx" offline-apropos ;