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 )
78 content="width=device-width, initial-scale=1"
82 : help-navbar ( -- xml )
83 "conventions" >link topic>filename
87 <a href="https://factorcode.org">
88 <img src="favicon.ico" width="24" height="24" />
90 <a href="/">Handbook</a>
91 <a href=<->>Glossary</a>
92 <form method="get" action="/search" style="float: right;">
93 <input placeholder="Search" name="search" type="text"/>
94 <input type="submit" value="Go"/>
100 : help-footer ( -- xml )
101 version-info "\n" split1 drop
105 This documentation was generated offline from a
106 <code>load-all</code> image. If you want, you can also
107 browse the documentation from within the <a
108 href="article-ui-tools.html">UI developer tools</a>. See
109 the <a href="https://factorcode.org">Factor website</a>
110 for more information.
116 : bijective-base26 ( n -- name )
117 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
119 : css-class ( style classes -- name )
120 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
122 : fix-css-style ( style -- style' )
123 R/ font-size: \d+pt;/ [
124 "font-size: " ?head drop "pt;" ?tail drop
126 "font-size: %dpt;" sprintf
129 R/ padding: \d+px;/ [
130 "padding: " ?head drop "px;" ?tail drop
131 string>number dup even? [ 2 * 1 + ] [ 2 * ] if
132 number>string "padding: " "px;" surround
139 R/ background-color: #f3f2ea;/ [
140 drop "background-color: #f7f7f7;"
143 R/ font-family: monospace;/ [
144 " white-space: pre-wrap; line-height: 125%;" append
147 { "font-family: monospace;" "background-color:" } [ over subseq? ] all? [
148 " border: 1px solid #dbdbdb; border-radius: 5px; margin: 15px; width: calc(100% - 30px);" append
151 : fix-help-header ( classes -- classes )
153 [ ".a" head? ] [ "#f4efd9;" swap subseq? ] bi and
155 "padding: 10px;" "padding: 0px;" replace
156 "background-color: #f4efd9;" "background-color: white;" replace
158 " border-bottom: 1px dashed #ccc; width: 100%; padding-top: 15px; padding-bottom: 10px; }"
159 append swap pick set-nth {
160 ".a a { color: black; font-size: 24pt; line-height: 100%; }"
161 ".a * a { color: #2a5db0; font-size: 12pt; }"
162 ".a td { border: none; }"
163 ".a tr:hover { background-color: white; }"
167 : dark-mode-css ( classes -- classes' )
168 { "" "/* Dark mode */" "@media (prefers-color-scheme:dark) {" }
171 "{" ?head drop "}" ?tail drop ";" split
172 [ [ blank? ] trim ] map harvest [ ";" append ] map
173 [ R/ (#[0-9a-fA-F]+|white|black);/ re-contains? ] filter
175 R/ (#[0-9a-fA-F]+|white|black);/ [
177 { "#000000;" "#bdc1c6;" }
178 { "#2a5db0;" "#8ab4f8;" }
179 { "#333333;" "#cccccc;" }
180 { "#373e48;" "#ffffff;" }
181 { "#8b4500;" "orange;" }
182 { "#dbdbdb;" "#444444;" }
183 { "#e3e2db;" "#666666;" }
184 { "white;" "#202124;" }
185 { "black;" "white;" }
187 but-last parse-color inverse-color color>hex ";" append
190 ] map " " join "{ " " }" surround
191 ] re-replace-with " " prepend
192 "{ }" over subseq? [ drop f ] when
193 ] map harvest append "}" suffix ;
195 : mobile-css ( classes -- classes' )
196 { "" "/* Mobile */" "@media screen and (max-width: 600px) {" }
199 "{" ?head drop "}" ?tail drop ";" split
200 [ [ blank? ] trim ] map harvest [ ";" append ] map
201 { "margin: 15px;" "width: calc(100% - 30px);" } intersect
202 { "margin: 15px;" } { "margin: 15px 0px;" } replace
203 { "width: calc(100% - 30px);" } { "width: 100%;" } replace
204 " " join "{ " " }" surround
205 ] re-replace-with " " prepend
206 "{ }" over subseq? [ drop f ] when
207 ] map harvest append "}" suffix ;
209 : css-classes ( classes -- stylesheet )
211 [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
212 ] { } assoc>map fix-help-header dup
213 [ dark-mode-css ] [ mobile-css ] bi 3append join-lines ;
215 :: css-styles-to-classes ( body -- stylesheet body )
216 H{ } clone :> classes
225 [ clone [ V{ } like ] change-alist ] change-attrs
226 "style" over delete-at* drop classes css-class
231 ] each classes sort-values css-classes body ;
233 : retina-image ( path -- path' )
234 "@2x" over subseq? [ "." split1-last "@2x." glue ] unless ;
236 : ?copy-file ( from to -- )
237 dup file-exists? [ 2drop ] [ copy-file ] if ;
239 : cache-images ( body -- body' )
243 T{ name { main "img" } } over tag-named? [
245 retina-image dup file-name
253 : help>html ( topic -- xml )
254 [ article-title " - Factor Documentation" append ]
256 [ print-topic ] with-html-writer
257 css-styles-to-classes cache-images
258 "resource:extra/websites/factorcode/favicon.ico" dup file-name ?copy-file
259 [ help-stylesheet help-meta prepend help-navbar ] dip help-footer
260 [XML <-><div class="page"><-><-></div> XML]
263 : generate-help-file ( topic -- )
264 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
266 : all-vocabs-really ( -- seq )
267 all-disk-vocabs-recursive filter-vocabs
268 [ vocab-name "scratchpad" = ] reject ;
270 : all-topics ( -- topics )
272 articles get keys [ >link ] map %
273 all-words [ >link ] map %
274 all-authors [ <vocab-author> ] map %
275 all-tags [ <vocab-tag> ] map %
279 : serialize-index ( index file -- )
281 [ [ topic>filename ] dip ] { } assoc-map-as serialize
284 : generate-article-index ( -- )
285 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
286 "articles.idx" serialize-index ;
288 : generate-word-index ( -- )
289 all-words [ dup name>> ] { } map>assoc
290 "words.idx" serialize-index ;
292 : generate-vocab-index ( -- )
293 all-vocabs-really [ dup vocab-name ] { } map>assoc
294 "vocabs.idx" serialize-index ;
296 : generate-indices ( -- )
297 generate-article-index
299 generate-vocab-index ;
301 : generate-help-files ( -- )
303 { recent-searches f }
305 { recent-articles f }
308 all-topics [ '[ _ generate-help-file ] try ] each
311 : generate-help ( -- )
321 MEMO: load-index ( name -- index )
322 binary file-contents bytes>object ;
324 : offline-apropos ( string index -- results )
325 load-index completions ;
327 : article-apropos ( string -- results )
328 "articles.idx" offline-apropos ;
330 : vocab-apropos ( string -- results )
331 "vocabs.idx" offline-apropos ;
333 : generate-qualified-index ( index -- )
336 over "," split1 nip ".html" ?tail drop
337 [ swap ":" glue 2array ] [ _ push-at ] bi
339 ] keep [ swap ] { } assoc-map-as
340 "qualified.idx" binary [ serialize ] with-file-writer ;
342 : qualified-index ( str index -- str index' )
345 dup file-exists? [ pick generate-qualified-index ] unless
346 load-index completions keys concat
347 ] [ drop f ] if [ append ] unless-empty ;
349 : word-apropos ( string -- results )
350 "words.idx" load-index qualified-index completions ;