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');
101 if (input !== document.activeElement) {
102 event.preventDefault();
103 setTimeout(function() {
104 input.focus().select()
113 : help-header ( stylesheet -- xml )
114 help-stylesheet help-meta swap help-script 3append ;
116 : help-nav ( -- xml )
117 "conventions" >link topic>filename
120 <form method="get" action="/search" style="float: right;">
121 <input placeholder="Search" id="search" name="search" type="text" tabindex="1" />
122 <input type="submit" value="Go" tabindex="1" />
124 <a href="https://factorcode.org">
125 <img src="favicon.ico" width="24" height="24" />
127 <a href="/">Handbook</a>
128 <a href=<->>Glossary</a>
132 : help-footer ( -- xml )
133 version-info "\n" split1 drop
137 This documentation was generated offline from a
138 <code>load-all</code> image. If you want, you can also
139 browse the documentation from within the <a
140 href="article-ui-tools.html">UI developer tools</a>. See
141 the <a href="https://factorcode.org">Factor website</a>
142 for more information.
148 : bijective-base26 ( n -- name )
149 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
151 : css-class ( style classes -- name )
152 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
154 : fix-css-style ( style -- style' )
155 R/ font-size: \d+pt;/ [
156 "font-size: " ?head drop "pt;" ?tail drop
158 "font-size: %dpt;" sprintf
161 R/ padding: \d+px;/ [
162 "padding: " ?head drop "px;" ?tail drop
163 string>number dup even? [ 2 * 1 + ] [ 2 * ] if
164 number>string "padding: " "px;" surround
171 R/ font-family: monospace;/ [
172 " white-space: pre-wrap; line-height: 125%;" append
175 dup { "font-family: monospace;" "background-color:" } [ subseq-of? ] with all? [
176 " margin: 10px 0px;" append
179 dup { "border:" "background-color:" } [ subseq-of? ] with all? [
180 " border-radius: 5px;" append
183 : fix-help-header ( classes -- classes )
185 [ ".a" head? ] [ "#f4efd9;" subseq-of? ] bi and
187 "padding: 10px;" "padding: 0px;" replace
188 "background-color: #f4efd9;" "background-color: white;" replace
190 " border-bottom: 1px dashed #d5d5d5 width: 100%; padding-top: 15px; padding-bottom: 10px; }"
191 append swap pick set-nth {
192 ".a a { color: black; font-size: 24pt; line-height: 100%; }"
193 ".a * a { color: #2a5db0; font-size: 12pt; }"
194 ".a td { border: none; }"
195 ".a tr:hover { background-color: white; }"
199 : dark-mode-css ( classes -- classes' )
200 { "" "/* Dark mode */" "@media (prefers-color-scheme:dark) {" }
203 "{" ?head drop "}" ?tail drop ";" split
204 [ [ blank? ] trim ] map harvest [ ";" append ] map
205 [ R/ (#[0-9a-fA-F]+|white|black);/ re-contains? ] filter
207 R/ (#[0-9a-fA-F]+|white|black);/ [
209 { "#000000;" "#bdc1c6;" }
210 { "#2a5db0;" "#8ab4f8;" }
211 { "#333333;" "#d5d5d5;" }
212 { "#373e48;" "#ffffff;" }
213 { "#8b4500;" "orange;" }
214 { "#e3e2db;" "#444444;" }
215 { "white;" "#202124;" }
216 { "black;" "white;" }
218 but-last parse-color inverse-color color>hex ";" append
221 ] map " " join "{ " " }" surround
222 ] re-replace-with " " prepend
223 dup "{ }" subseq-of? [ drop f ] when
224 ] map harvest append "}" suffix ;
226 : css-classes ( classes -- stylesheet )
228 [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
229 ] { } assoc>map fix-help-header dup dark-mode-css append join-lines ;
231 :: css-styles-to-classes ( body -- stylesheet body )
232 H{ } clone :> classes
241 [ clone [ V{ } like ] change-alist ] change-attrs
242 "style" over delete-at* drop classes css-class
247 ] each classes sort-values css-classes body ;
249 : retina-image ( path -- path' )
250 dup "@2x" subseq-of? [ "." split1-last "@2x." glue ] unless ;
252 : ?copy-file ( from to -- )
253 dup file-exists? [ 2drop ] [ copy-file ] if ;
255 : cache-images ( body -- body' )
259 T{ name { main "img" } } over tag-named? [
261 retina-image dup file-name
269 : help>html ( topic -- xml )
270 [ article-title " - Factor Documentation" append ]
272 [ print-topic ] with-html-writer
273 css-styles-to-classes cache-images
274 "resource:extra/websites/factorcode/favicon.ico" dup file-name ?copy-file
275 [ help-header help-nav ] dip help-footer
276 [XML <-><div class="page"><-><-></div> XML]
279 : generate-help-file ( topic -- )
280 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
282 : all-vocabs-really ( -- seq )
283 all-disk-vocabs-recursive filter-vocabs
284 [ vocab-name "scratchpad" = ] reject ;
286 : all-topics ( -- topics )
288 articles get keys [ >link ] map %
289 all-words [ >link ] map %
290 all-authors [ <vocab-author> ] map %
291 all-tags [ <vocab-tag> ] map %
295 : serialize-index ( index file -- )
297 [ [ topic>filename ] dip ] { } assoc-map-as serialize
300 : generate-article-index ( -- )
301 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
302 "articles.idx" serialize-index ;
304 : generate-word-index ( -- )
305 all-words [ dup name>> ] { } map>assoc
306 "words.idx" serialize-index ;
308 : generate-vocab-index ( -- )
309 all-vocabs-really [ dup vocab-name ] { } map>assoc
310 "vocabs.idx" serialize-index ;
312 : generate-indices ( -- )
313 generate-article-index
315 generate-vocab-index ;
317 : generate-help-files ( -- )
319 { recent-searches f }
321 { recent-articles f }
324 all-topics [ '[ _ generate-help-file ] try ] each
327 : generate-help ( -- )
337 MEMO: load-index ( name -- index )
338 binary file-contents bytes>object ;
340 : offline-apropos ( string index -- results )
341 load-index completions ;
343 : article-apropos ( string -- results )
344 "articles.idx" offline-apropos ;
346 : vocab-apropos ( string -- results )
347 "vocabs.idx" offline-apropos ;
349 : generate-qualified-index ( index -- )
352 over "," split1 nip ".html" ?tail drop
353 [ swap ":" glue 2array ] [ _ push-at ] bi
355 ] keep [ swap ] { } assoc-map-as
356 "qualified.idx" binary [ serialize ] with-file-writer ;
358 : qualified-index ( str index -- str index' )
361 dup file-exists? [ pick generate-qualified-index ] unless
362 load-index completions keys concat
363 ] [ drop f ] if [ append ] unless-empty ;
365 : word-apropos ( string -- results )
366 "words.idx" load-index qualified-index completions ;