1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays ascii assocs colors
4 combinators.short-circuit debugger formatting help help.home
5 help.topics help.vocabs html html.streams io.directories
6 io.encodings.ascii io.encodings.binary io.encodings.utf8
7 io.files io.files.temp io.pathnames kernel make math math.parser
8 namespaces regexp sequences sequences.deep serialize sets
9 sorting splitting strings system tools.completion vocabs
10 vocabs.hierarchy words xml.data xml.syntax xml.traversal
12 FROM: io.encodings.ascii => ascii ;
13 FROM: ascii => ascii? ;
16 : escape-char ( ch -- )
19 { CHAR: \" "__quo__" }
20 { CHAR: * "__star__" }
21 { CHAR: : "__colon__" }
25 { CHAR: \\ "__back__" }
26 { CHAR: | "__pipe__" }
27 { CHAR: / "__slash__" }
28 { CHAR: , "__comma__" }
30 { CHAR: # "__hash__" }
31 { CHAR: % "__percent__" }
33 ] [ number>string "__" "__" surround % ] if ;
35 : escape-filename ( string -- filename )
36 [ [ escape-char ] each ] "" make ;
38 GENERIC: topic>filename* ( topic -- name prefix )
40 M: word topic>filename*
42 [ name>> ] [ vocabulary>> ] bi 2array "word"
45 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
46 M: word-link topic>filename* name>> topic>filename* ;
47 M: vocab-spec topic>filename* vocab-name "vocab" ;
48 M: vocab-tag topic>filename* name>> "tag" ;
49 M: vocab-author topic>filename* name>> "author" ;
50 M: f topic>filename* drop \ f topic>filename* ;
52 : topic>filename ( topic -- filename )
57 [ [ escape-filename ] map "," join ]
63 M: topic url-of topic>filename ;
66 string>> "resource:" ?head [
67 "https://github.com/factor/factor/blob/master/"
71 : help-stylesheet ( stylesheet -- xml )
72 "vocab:help/html/stylesheet.css" ascii file-contents
73 swap "\n" glue [XML <style><-></style> XML] ;
75 : help-meta ( -- xml )
79 content="width=device-width, initial-scale=1"
85 media="(prefers-color-scheme: light)"
90 media="(prefers-color-scheme: dark)"
95 "conventions" >link topic>filename
98 <form method="get" action="/search" style="float: right;">
99 <input placeholder="Search" name="search" type="text"/>
100 <input type="submit" value="Go"/>
102 <a href="https://factorcode.org">
103 <img src="favicon.ico" width="24" height="24" />
105 <a href="/">Handbook</a>
106 <a href=<->>Glossary</a>
110 : help-footer ( -- xml )
111 version-info "\n" split1 drop
115 This documentation was generated offline from a
116 <code>load-all</code> image. If you want, you can also
117 browse the documentation from within the <a
118 href="article-ui-tools.html">UI developer tools</a>. See
119 the <a href="https://factorcode.org">Factor website</a>
120 for more information.
126 : bijective-base26 ( n -- name )
127 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
129 : css-class ( style classes -- name )
130 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
132 : fix-css-style ( style -- style' )
133 R/ font-size: \d+pt;/ [
134 "font-size: " ?head drop "pt;" ?tail drop
136 "font-size: %dpt;" sprintf
139 R/ padding: \d+px;/ [
140 "padding: " ?head drop "px;" ?tail drop
141 string>number dup even? [ 2 * 1 + ] [ 2 * ] if
142 number>string "padding: " "px;" surround
149 R/ font-family: monospace;/ [
150 " white-space: pre-wrap; line-height: 125%;" append
153 dup { "font-family: monospace;" "background-color:" } [ subseq-of? ] with all? [
154 " margin: 10px 0px;" append
157 dup { "border:" "background-color:" } [ subseq-of? ] with all? [
158 " border-radius: 5px;" append
161 : fix-help-header ( classes -- classes )
163 [ ".a" head? ] [ "#f4efd9;" subseq-of? ] bi and
165 "padding: 10px;" "padding: 0px;" replace
166 "background-color: #f4efd9;" "background-color: white;" replace
168 " border-bottom: 1px dashed #d5d5d5 width: 100%; padding-top: 15px; padding-bottom: 10px; }"
169 append swap pick set-nth {
170 ".a a { color: black; font-size: 24pt; line-height: 100%; }"
171 ".a * a { color: #2a5db0; font-size: 12pt; }"
172 ".a td { border: none; }"
173 ".a tr:hover { background-color: white; }"
177 : dark-mode-css ( classes -- classes' )
178 { "" "/* Dark mode */" "@media (prefers-color-scheme:dark) {" }
181 "{" ?head drop "}" ?tail drop ";" split
182 [ [ blank? ] trim ] map harvest [ ";" append ] map
183 [ R/ (#[0-9a-fA-F]+|white|black);/ re-contains? ] filter
185 R/ (#[0-9a-fA-F]+|white|black);/ [
187 { "#000000;" "#bdc1c6;" }
188 { "#2a5db0;" "#8ab4f8;" }
189 { "#333333;" "#d5d5d5;" }
190 { "#373e48;" "#ffffff;" }
191 { "#8b4500;" "orange;" }
192 { "#e3e2db;" "#444444;" }
193 { "white;" "#202124;" }
194 { "black;" "white;" }
196 but-last parse-color inverse-color color>hex ";" append
199 ] map " " join "{ " " }" surround
200 ] re-replace-with " " prepend
201 dup "{ }" subseq-of? [ drop f ] when
202 ] map harvest append "}" suffix ;
204 : css-classes ( classes -- stylesheet )
206 [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
207 ] { } assoc>map fix-help-header dup dark-mode-css append join-lines ;
209 :: css-styles-to-classes ( body -- stylesheet body )
210 H{ } clone :> classes
219 [ clone [ V{ } like ] change-alist ] change-attrs
220 "style" over delete-at* drop classes css-class
225 ] each classes sort-values css-classes body ;
227 : retina-image ( path -- path' )
228 dup "@2x" subseq-of? [ "." split1-last "@2x." glue ] unless ;
230 : ?copy-file ( from to -- )
231 dup file-exists? [ 2drop ] [ copy-file ] if ;
233 : cache-images ( body -- body' )
237 T{ name { main "img" } } over tag-named? [
239 retina-image dup file-name
247 : help>html ( topic -- xml )
248 [ article-title " - Factor Documentation" append ]
250 [ print-topic ] with-html-writer
251 css-styles-to-classes cache-images
252 "resource:extra/websites/factorcode/favicon.ico" dup file-name ?copy-file
253 [ help-stylesheet help-meta prepend help-nav ] dip help-footer
254 [XML <-><div class="page"><-><-></div> XML]
257 : generate-help-file ( topic -- )
258 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
260 : all-vocabs-really ( -- seq )
261 all-disk-vocabs-recursive filter-vocabs
262 [ vocab-name "scratchpad" = ] reject ;
264 : all-topics ( -- topics )
266 articles get keys [ >link ] map %
267 all-words [ >link ] map %
268 all-authors [ <vocab-author> ] map %
269 all-tags [ <vocab-tag> ] map %
273 : serialize-index ( index file -- )
275 [ [ topic>filename ] dip ] { } assoc-map-as serialize
278 : generate-article-index ( -- )
279 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
280 "articles.idx" serialize-index ;
282 : generate-word-index ( -- )
283 all-words [ dup name>> ] { } map>assoc
284 "words.idx" serialize-index ;
286 : generate-vocab-index ( -- )
287 all-vocabs-really [ dup vocab-name ] { } map>assoc
288 "vocabs.idx" serialize-index ;
290 : generate-indices ( -- )
291 generate-article-index
293 generate-vocab-index ;
295 : generate-help-files ( -- )
297 { recent-searches f }
299 { recent-articles f }
302 all-topics [ '[ _ generate-help-file ] try ] each
305 : generate-help ( -- )
315 MEMO: load-index ( name -- index )
316 binary file-contents bytes>object ;
318 : offline-apropos ( string index -- results )
319 load-index completions ;
321 : article-apropos ( string -- results )
322 "articles.idx" offline-apropos ;
324 : vocab-apropos ( string -- results )
325 "vocabs.idx" offline-apropos ;
327 : generate-qualified-index ( index -- )
330 over "," split1 nip ".html" ?tail drop
331 [ swap ":" glue 2array ] [ _ push-at ] bi
333 ] keep [ swap ] { } assoc-map-as
334 "qualified.idx" binary [ serialize ] with-file-writer ;
336 : qualified-index ( str index -- str index' )
339 dup file-exists? [ pick generate-qualified-index ] unless
340 load-index completions keys concat
341 ] [ drop f ] if [ append ] unless-empty ;
343 : word-apropos ( string -- results )
344 "words.idx" load-index qualified-index completions ;