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 system 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"
81 : help-navbar ( -- xml )
82 "conventions" >link topic>filename
86 <a href="https://factorcode.org">
87 <img src="favicon.ico" width="24" height="24" />
89 <a href="/">Handbook</a>
90 <a href=<->>Glossary</a>
91 <form method="get" action="/search" style="float: right;">
92 <input placeholder="Search" name="search" type="text"/>
93 <input type="submit" value="Go"/>
99 : help-footer ( -- xml )
100 version-info "\n" split1 drop
104 This documentation was generated offline from a
105 <code>load-all</code> image. If you want, you can also
106 browse the documentation from within the <a
107 href="article-ui-tools.html">UI developer tools</a>. See
108 the <a href="https://factorcode.org">Factor website</a>
109 for more information.
115 : bijective-base26 ( n -- name )
116 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
118 : css-class ( style classes -- name )
119 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
121 : fix-css-style ( style -- style' )
122 R/ font-size: \d+pt;/ [
123 "font-size: " ?head drop "pt;" ?tail drop
125 "font-size: %dpt;" sprintf
128 R/ padding: \d+px;/ [
129 "padding: " ?head drop "px;" ?tail drop
130 string>number dup even? [ 2 * 1 + ] [ 2 * ] if
131 number>string "padding: " "px;" surround
138 R/ font-family: monospace;/ [
139 " white-space: pre-wrap; line-height: 125%;" append
142 : fix-help-header ( classes -- classes )
144 [ ".a" head? ] [ "#f4efd9;" swap subseq? ] bi and
146 "padding: 10px;" "padding: 0px;" replace
147 "background-color: #f4efd9;" "background-color: white;" replace
149 " border-bottom: 1px dashed #ccc; width: 100%; padding-top: 15px; padding-bottom: 10px; }"
150 append swap pick set-nth {
151 ".a a { color: black; font-size: 24pt; }"
152 ".a * a { color: #2A5DB0; font-size: 12pt; }"
153 ".a td { border: none; }"
154 ".a tr:hover { background-color: white; }"
158 : css-classes ( classes -- stylesheet )
160 [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
161 ] { } assoc>map fix-help-header join-lines ;
163 :: css-styles-to-classes ( body -- stylesheet body )
164 H{ } clone :> classes
173 [ clone [ V{ } like ] change-alist ] change-attrs
174 "style" over delete-at* drop classes css-class
179 ] each classes sort-values css-classes body ;
181 : retina-image ( path -- path' )
182 "@2x" over subseq? [ "." split1-last "@2x." glue ] unless ;
184 : ?copy-file ( from to -- )
185 dup file-exists? [ 2drop ] [ copy-file ] if ;
187 : cache-images ( body -- body' )
191 T{ name { main "img" } } over tag-named? [
193 retina-image dup file-name
201 : help>html ( topic -- xml )
202 [ article-title " - Factor Documentation" append ]
204 [ print-topic ] with-html-writer
205 css-styles-to-classes cache-images
206 "resource:extra/websites/factorcode/favicon.ico" dup file-name ?copy-file
207 [ help-stylesheet help-meta prepend help-navbar ] dip help-footer
208 [XML <-><div class="page"><-><-></div> XML]
211 : generate-help-file ( topic -- )
212 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
214 : all-vocabs-really ( -- seq )
215 all-disk-vocabs-recursive filter-vocabs
216 [ vocab-name "scratchpad" = ] reject ;
218 : all-topics ( -- topics )
220 articles get keys [ >link ] map %
221 all-words [ >link ] map %
222 all-authors [ <vocab-author> ] map %
223 all-tags [ <vocab-tag> ] map %
227 : serialize-index ( index file -- )
229 [ [ topic>filename ] dip ] { } assoc-map-as serialize
232 : generate-article-index ( -- )
233 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
234 "articles.idx" serialize-index ;
236 : generate-word-index ( -- )
237 all-words [ dup name>> ] { } map>assoc
238 "words.idx" serialize-index ;
240 : generate-vocab-index ( -- )
241 all-vocabs-really [ dup vocab-name ] { } map>assoc
242 "vocabs.idx" serialize-index ;
244 : generate-indices ( -- )
245 generate-article-index
247 generate-vocab-index ;
249 : generate-help-files ( -- )
251 { recent-searches f }
253 { recent-articles f }
256 all-topics [ '[ _ generate-help-file ] try ] each
259 : generate-help ( -- )
269 MEMO: load-index ( name -- index )
270 binary file-contents bytes>object ;
272 : offline-apropos ( string index -- results )
273 load-index completions ;
275 : article-apropos ( string -- results )
276 "articles.idx" offline-apropos ;
278 : word-apropos ( string -- results )
279 "words.idx" offline-apropos ;
281 : vocab-apropos ( string -- results )
282 "vocabs.idx" offline-apropos ;