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)"
94 : help-script ( -- xml )
96 <script type="text/javascript">
97 document.addEventListener('keydown', function (event) {
98 if (event.code == 'Slash') {
99 let input = document.getElementById('search');
100 if (input !== document.activeElement) {
101 setTimeout(function() {
102 input.focus().select()
110 : help-header ( stylesheet -- xml )
111 help-stylesheet help-meta swap help-script 3append ;
113 : help-nav ( -- xml )
114 "conventions" >link topic>filename
117 <form method="get" action="/search" style="float: right;">
118 <input placeholder="Search" id="search" name="search" type="text" tabindex="1" />
119 <input type="submit" value="Go" tabindex="1" />
121 <a href="https://factorcode.org">
122 <img src="favicon.ico" width="24" height="24" />
124 <a href="/">Handbook</a>
125 <a href=<->>Glossary</a>
129 : help-footer ( -- xml )
130 version-info "\n" split1 drop
134 This documentation was generated offline from a
135 <code>load-all</code> image. If you want, you can also
136 browse the documentation from within the <a
137 href="article-ui-tools.html">UI developer tools</a>. See
138 the <a href="https://factorcode.org">Factor website</a>
139 for more information.
145 : bijective-base26 ( n -- name )
146 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
148 : css-class ( style classes -- name )
149 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
151 : fix-css-style ( style -- style' )
152 R/ font-size: \d+pt;/ [
153 "font-size: " ?head drop "pt;" ?tail drop
155 "font-size: %dpt;" sprintf
158 R/ padding: \d+px;/ [
159 "padding: " ?head drop "px;" ?tail drop
160 string>number dup even? [ 2 * 1 + ] [ 2 * ] if
161 number>string "padding: " "px;" surround
168 R/ font-family: monospace;/ [
169 " white-space: pre-wrap; line-height: 125%;" append
172 dup { "font-family: monospace;" "background-color:" } [ subseq-of? ] with all? [
173 " margin: 10px 0px;" append
176 dup { "border:" "background-color:" } [ subseq-of? ] with all? [
177 " border-radius: 5px;" append
180 : fix-help-header ( classes -- classes )
182 [ ".a" head? ] [ "#f4efd9;" subseq-of? ] bi and
184 "padding: 10px;" "padding: 0px;" replace
185 "background-color: #f4efd9;" "background-color: white;" replace
187 " border-bottom: 1px dashed #d5d5d5 width: 100%; padding-top: 15px; padding-bottom: 10px; }"
188 append swap pick set-nth {
189 ".a a { color: black; font-size: 24pt; line-height: 100%; }"
190 ".a * a { color: #2a5db0; font-size: 12pt; }"
191 ".a td { border: none; }"
192 ".a tr:hover { background-color: white; }"
196 : dark-mode-css ( classes -- classes' )
197 { "" "/* Dark mode */" "@media (prefers-color-scheme:dark) {" }
200 "{" ?head drop "}" ?tail drop ";" split
201 [ [ blank? ] trim ] map harvest [ ";" append ] map
202 [ R/ (#[0-9a-fA-F]+|white|black);/ re-contains? ] filter
204 R/ (#[0-9a-fA-F]+|white|black);/ [
206 { "#000000;" "#bdc1c6;" }
207 { "#2a5db0;" "#8ab4f8;" }
208 { "#333333;" "#d5d5d5;" }
209 { "#373e48;" "#ffffff;" }
210 { "#8b4500;" "orange;" }
211 { "#e3e2db;" "#444444;" }
212 { "white;" "#202124;" }
213 { "black;" "white;" }
215 but-last parse-color inverse-color color>hex ";" append
218 ] map " " join "{ " " }" surround
219 ] re-replace-with " " prepend
220 dup "{ }" subseq-of? [ drop f ] when
221 ] map harvest append "}" suffix ;
223 : css-classes ( classes -- stylesheet )
225 [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
226 ] { } assoc>map fix-help-header dup dark-mode-css append join-lines ;
228 :: css-styles-to-classes ( body -- stylesheet body )
229 H{ } clone :> classes
238 [ clone [ V{ } like ] change-alist ] change-attrs
239 "style" over delete-at* drop classes css-class
244 ] each classes sort-values css-classes body ;
246 : retina-image ( path -- path' )
247 dup "@2x" subseq-of? [ "." split1-last "@2x." glue ] unless ;
249 : ?copy-file ( from to -- )
250 dup file-exists? [ 2drop ] [ copy-file ] if ;
252 : cache-images ( body -- body' )
256 T{ name { main "img" } } over tag-named? [
258 retina-image dup file-name
266 : help>html ( topic -- xml )
267 [ article-title " - Factor Documentation" append ]
269 [ print-topic ] with-html-writer
270 css-styles-to-classes cache-images
271 "resource:extra/websites/factorcode/favicon.ico" dup file-name ?copy-file
272 [ help-header help-nav ] dip help-footer
273 [XML <-><div class="page"><-><-></div> XML]
276 : generate-help-file ( topic -- )
277 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
279 : all-vocabs-really ( -- seq )
280 all-disk-vocabs-recursive filter-vocabs
281 [ vocab-name "scratchpad" = ] reject ;
283 : all-topics ( -- topics )
285 articles get keys [ >link ] map %
286 all-words [ >link ] map %
287 all-authors [ <vocab-author> ] map %
288 all-tags [ <vocab-tag> ] map %
292 : serialize-index ( index file -- )
294 [ [ topic>filename ] dip ] { } assoc-map-as serialize
297 : generate-article-index ( -- )
298 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
299 "articles.idx" serialize-index ;
301 : generate-word-index ( -- )
302 all-words [ dup name>> ] { } map>assoc
303 "words.idx" serialize-index ;
305 : generate-vocab-index ( -- )
306 all-vocabs-really [ dup vocab-name ] { } map>assoc
307 "vocabs.idx" serialize-index ;
309 : generate-indices ( -- )
310 generate-article-index
312 generate-vocab-index ;
314 : generate-help-files ( -- )
316 { recent-searches f }
318 { recent-articles f }
321 all-topics [ '[ _ generate-help-file ] try ] each
324 : generate-help ( -- )
334 MEMO: load-index ( name -- index )
335 binary file-contents bytes>object ;
337 : offline-apropos ( string index -- results )
338 load-index completions ;
340 : article-apropos ( string -- results )
341 "articles.idx" offline-apropos ;
343 : vocab-apropos ( string -- results )
344 "vocabs.idx" offline-apropos ;
346 : generate-qualified-index ( index -- )
349 over "," split1 nip ".html" ?tail drop
350 [ swap ":" glue 2array ] [ _ push-at ] bi
352 ] keep [ swap ] { } assoc-map-as
353 "qualified.idx" binary [ serialize ] with-file-writer ;
355 : qualified-index ( str index -- str index' )
358 dup file-exists? [ pick generate-qualified-index ] unless
359 load-index completions keys concat
360 ] [ drop f ] if [ append ] unless-empty ;
362 : word-apropos ( string -- results )
363 "words.idx" load-index qualified-index completions ;