1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See https://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 -- )
20 { CHAR: \" "__quo__" }
21 { CHAR: * "__star__" }
22 { CHAR: : "__colon__" }
26 { CHAR: \\ "__back__" }
27 { CHAR: | "__pipe__" }
28 { CHAR: / "__slash__" }
29 { CHAR: , "__comma__" }
31 { CHAR: # "__hash__" }
32 { CHAR: % "__percent__" }
35 ] [ number>string "__" "__" surround % ] if ;
37 : escape-filename ( string -- filename )
38 [ [ escape-char ] each ] "" make ;
40 GENERIC: topic>filename* ( topic -- name prefix )
42 M: word topic>filename*
44 [ name>> ] [ vocabulary>> ] bi 2array "word"
47 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
48 M: word-link topic>filename* name>> topic>filename* ;
49 M: vocab-spec topic>filename* vocab-name "vocab" ;
50 M: vocab-tag topic>filename* name>> "tag" ;
51 M: vocab-author topic>filename* name>> "author" ;
52 M: f topic>filename* drop \ f topic>filename* ;
54 : topic>filename ( topic -- filename )
59 [ [ escape-filename ] map "," join ]
65 M: topic url-of topic>filename ;
68 string>> "resource:" ?head [
69 "https://github.com/factor/factor/blob/master/"
73 : help-stylesheet ( stylesheet -- xml )
74 "vocab:help/html/stylesheet.css" ascii file-contents
75 swap "\n" glue [XML <style><-></style> XML] ;
77 : help-meta ( -- xml )
81 content="width=device-width, initial-scale=1"
87 media="(prefers-color-scheme: light)"
92 media="(prefers-color-scheme: dark)"
96 : help-script ( -- xml )
98 <script type="text/javascript">
99 document.addEventListener('keydown', function (event) {
100 if (event.code == 'Slash') {
101 let input = document.getElementById('search');
103 if (input !== document.activeElement) {
104 event.preventDefault();
105 setTimeout(function() {
115 : help-header ( stylesheet -- xml )
116 help-stylesheet help-meta swap help-script 3append ;
118 : help-nav ( -- xml )
119 "conventions" >link topic>filename
122 <form method="get" action="/search" style="float: right;">
123 <input placeholder="Search" id="search" name="search" type="text" tabindex="1" />
124 <input type="submit" value="Go" tabindex="1" />
126 <a href="https://factorcode.org">
127 <img src="favicon.ico" width="24" height="24" />
129 <a href="/">Handbook</a>
130 <a href=<->>Glossary</a>
134 : help-footer ( -- xml )
135 version-info "\n" split1 drop
139 This documentation was generated offline from a
140 <code>load-all</code> image. If you want, you can also
141 browse the documentation from within the <a
142 href="article-ui-tools.html">UI developer tools</a>. See
143 the <a href="https://factorcode.org">Factor website</a>
144 for more information.
150 : bijective-base26 ( n -- name )
151 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
153 : css-class ( style classes -- name )
154 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
156 : fix-css-style ( style -- style' )
157 R/ font-size: \d+pt;/ [
158 "font-size: " ?head drop "pt;" ?tail drop
160 "font-size: %dpt;" sprintf
163 R/ padding: \d+px;/ [
164 "padding: " ?head drop "px;" ?tail drop
165 string>number 2 * number>string
166 "padding: " "px;" surround
173 R/ font-family: monospace;/ [
174 " margin-top: 0.5em; width: fit-content; white-space: pre-wrap; line-height: 125%;" append
177 : fix-help-header ( classes -- classes )
179 [ ".a" head? ] [ "#f4efd9;" subseq-of? ] bi and
181 "padding: 10px;" "" replace
182 "background-color: #f4efd9;" "" replace
184 " border-bottom: 1px dashed #d5d5d5; width: 100%; padding-top: 10px; padding-bottom: 10px; }"
185 append swap pick set-nth {
186 ".a a { color: black; font-size: 24pt; line-height: 100%; }"
187 ".a * a { color: #2a5db0; font-size: 12pt; }"
188 ".a td { border: none; }"
189 ".a tr:hover { background-color: transparent }"
193 : dark-mode-css ( classes -- classes' )
194 { "" "/* Dark mode */" "@media (prefers-color-scheme:dark) {" }
197 "{" ?head drop "}" ?tail drop ";" split
198 [ [ blank? ] trim ] map harvest [ ";" append ] map
199 [ R/ (#[0-9a-fA-F]+|white|black);/ re-contains? ] filter
201 R/ (#[0-9a-fA-F]+|white|black);/ [
203 { "#000000;" "#bdc1c6;" }
204 { "#2a5db0;" "#8ab4f8;" }
205 { "#333333;" "#d5d5d5;" }
206 { "#373e48;" "#ffffff;" }
207 { "#8b4500;" "orange;" }
208 { "#d5d5d5;" "#666;" }
209 { "#e3e2db;" "#444444;" }
210 { "white;" "#202124;" }
211 { "black;" "white;" }
212 { "transparent;" "transparent;" }
214 but-last parse-color inverse-color color>hex ";" append
217 ] map " " join "{ " " }" surround
218 ] re-replace-with " " prepend
219 dup "{ }" subseq-of? [ drop f ] when
220 ] map harvest append "}" suffix ;
222 : css-classes ( classes -- stylesheet )
224 [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
225 ] { } assoc>map fix-help-header dup dark-mode-css append join-lines ;
227 :: css-styles-to-classes ( body -- stylesheet body )
228 H{ } clone :> classes
237 [ clone [ V{ } like ] change-alist ] change-attrs
238 "style" over delete-at* drop classes css-class
243 ] each classes sort-values css-classes body ;
245 : retina-image ( path -- path' )
246 dup "@2x" subseq-of? [ "." split1-last "@2x." glue ] unless ;
248 : ?copy-file ( from to -- )
249 dup file-exists? [ 2drop ] [ copy-file ] if ;
251 : cache-images ( body -- body' )
255 T{ name { main "img" } } over tag-named? [
257 retina-image dup file-name
265 : help>html ( topic -- xml )
266 [ article-title " - Factor Documentation" append ]
268 [ print-topic ] with-html-writer
269 css-styles-to-classes cache-images
270 "resource:extra/websites/factorcode/favicon.ico" dup file-name ?copy-file
271 [ help-header help-nav ] dip help-footer
272 [XML <-><div class="page"><-><-></div> XML]
275 : generate-help-file ( topic -- )
276 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
278 : all-vocabs-really ( -- seq )
279 all-disk-vocabs-recursive filter-vocabs
280 [ vocab-name "scratchpad" = ] reject ;
282 : all-topics ( -- topics )
284 articles get keys [ >link ] map %
285 all-words [ >link ] map %
286 all-authors [ <vocab-author> ] map %
287 all-tags [ <vocab-tag> ] map %
291 : serialize-index ( index file -- )
293 [ [ topic>filename ] dip ] { } assoc-map-as serialize
296 : generate-article-index ( -- )
297 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
298 "articles.idx" serialize-index ;
300 : generate-word-index ( -- )
301 all-words [ dup name>> ] { } map>assoc
302 "words.idx" serialize-index ;
304 : generate-vocab-index ( -- )
305 all-vocabs-really [ dup vocab-name ] { } map>assoc
306 "vocabs.idx" serialize-index ;
308 : generate-indices ( -- )
309 generate-article-index
311 generate-vocab-index ;
313 : generate-help-files ( -- )
315 { recent-searches f }
317 { recent-articles f }
320 all-topics [ '[ _ generate-help-file ] try ] each
323 : generate-help ( -- )
333 MEMO: load-index ( name -- index )
334 binary file-contents bytes>object ;
336 : offline-apropos ( string index -- results )
337 load-index completions ;
339 : article-apropos ( string -- results )
340 "articles.idx" offline-apropos ;
342 : vocab-apropos ( string -- results )
343 "vocabs.idx" offline-apropos ;
345 : generate-qualified-index ( index -- )
348 over "," split1 nip ".html" ?tail drop
349 [ swap ":" glue 2array ] [ _ push-at ] bi
351 ] keep [ swap ] { } assoc-map-as
352 "qualified.idx" binary [ serialize ] with-file-writer ;
354 : qualified-index ( str index -- str index' )
357 dup file-exists? [ pick generate-qualified-index ] unless
358 load-index completions keys concat
359 ] [ drop f ] if [ append ] unless-empty ;
361 : word-apropos ( string -- results )
362 "words.idx" load-index qualified-index completions ;