]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
4cd0f8c889da55ad40ea890a9f3b1dd952f45038
[factor.git] / basis / help / html / html.factor
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
11 xml.writer ;
12 FROM: io.encodings.ascii => ascii ;
13 FROM: ascii => ascii? ;
14 IN: help.html
15
16 : escape-char ( ch -- )
17     dup ascii? [
18         dup H{
19             { CHAR: \" "__quo__" }
20             { CHAR: * "__star__" }
21             { CHAR: : "__colon__" }
22             { CHAR: < "__lt__" }
23             { CHAR: > "__gt__" }
24             { CHAR: ? "__que__" }
25             { CHAR: \\ "__back__" }
26             { CHAR: | "__pipe__" }
27             { CHAR: / "__slash__" }
28             { CHAR: , "__comma__" }
29             { CHAR: @ "__at__" }
30             { CHAR: # "__hash__" }
31             { CHAR: % "__percent__" }
32         } at [ % ] [ , ] ?if
33     ] [ number>string "__" "__" surround % ] if ;
34
35 : escape-filename ( string -- filename )
36     [ [ escape-char ] each ] "" make ;
37
38 GENERIC: topic>filename* ( topic -- name prefix )
39
40 M: word topic>filename*
41     dup vocabulary>> [
42         [ name>> ] [ vocabulary>> ] bi 2array "word"
43     ] [ drop f f ] if ;
44
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* ;
51
52 : topic>filename ( topic -- filename )
53     topic>filename* [
54         [
55             % "-" %
56             dup array?
57             [ [ escape-filename ] map "," join ]
58             [ escape-filename ]
59             if % ".html" %
60         ] "" make
61     ] [ drop f ] if* ;
62
63 M: topic url-of topic>filename ;
64
65 M: pathname url-of
66     string>> "resource:" ?head [
67         "https://github.com/factor/factor/blob/master/"
68         prepend
69     ] [ drop f ] if ;
70
71 : help-stylesheet ( stylesheet -- xml )
72     "vocab:help/html/stylesheet.css" ascii file-contents
73     swap "\n" glue [XML <style><-></style> XML] ;
74
75 : help-meta ( -- xml )
76     [XML
77         <meta
78             name="viewport"
79             content="width=device-width, initial-scale=1"
80             charset="utf-8"
81         />
82         <meta
83             name="theme-color"
84             content="#f5f5f5"
85             media="(prefers-color-scheme: light)"
86         />
87         <meta
88             name="theme-color"
89             content="#373e48"
90             media="(prefers-color-scheme: dark)"
91         />
92     XML] ;
93
94 : help-script ( -- xml )
95     [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()
103                     }, 0);
104                 }
105             }
106         });
107         </script>
108      XML] ;
109
110 : help-header ( stylesheet -- xml )
111     help-stylesheet help-meta swap help-script 3append ;
112
113 : help-nav ( -- xml )
114     "conventions" >link topic>filename
115     [XML
116         <nav>
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" />
120             </form>
121             <a href="https://factorcode.org">
122             <img src="favicon.ico" width="24" height="24" />
123             </a>
124             <a href="/">Handbook</a>
125             <a href=<->>Glossary</a>
126         </nav>
127      XML] ;
128
129 : help-footer ( -- xml )
130     version-info "\n" split1 drop
131     [XML
132         <footer>
133         <p>
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.
140         </p>
141         <p><-></p>
142         </footer>
143     XML] ;
144
145 : bijective-base26 ( n -- name )
146     [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
147
148 : css-class ( style classes -- name )
149     dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
150
151 : fix-css-style ( style -- style' )
152     R/ font-size: \d+pt;/ [
153         "font-size: " ?head drop "pt;" ?tail drop
154         string>number 2 -
155         "font-size: %dpt;" sprintf
156     ] re-replace-with
157
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
162     ] re-replace-with
163
164     R/ width: \d+px;/ [
165        drop ""
166     ] re-replace-with
167
168     R/ font-family: monospace;/ [
169         " white-space: pre-wrap; line-height: 125%;" append
170     ] re-replace-with
171
172     dup { "font-family: monospace;" "background-color:" } [ subseq-of? ] with all? [
173         " margin: 10px 0px;" append
174     ] when
175
176     dup { "border:" "background-color:" } [ subseq-of? ] with all? [
177         " border-radius: 5px;" append
178     ] when ;
179
180 : fix-help-header ( classes -- classes )
181     dup [
182         [ ".a" head? ] [ "#f4efd9;" subseq-of? ] bi and
183     ] find [
184         "padding: 10px;" "padding: 0px;" replace
185         "background-color: #f4efd9;" "background-color: white;" replace
186         "}" ?tail drop
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; }"
193         } prepend
194     ] [ drop ] if* ;
195
196 : dark-mode-css ( classes -- classes' )
197     { "" "/* Dark mode */" "@media (prefers-color-scheme:dark) {" }
198     swap [
199         R/ {[^}]+}/ [
200             "{" ?head drop "}" ?tail drop ";" split
201             [ [ blank? ] trim ] map harvest [ ";" append ] map
202             [ R/ (#[0-9a-fA-F]+|white|black);/ re-contains? ] filter
203             [
204                 R/ (#[0-9a-fA-F]+|white|black);/ [
205                     >string H{
206                         { "#000000;" "#bdc1c6;" }
207                         { "#2a5db0;" "#8ab4f8;" }
208                         { "#333333;" "#d5d5d5;" }
209                         { "#373e48;" "#ffffff;" }
210                         { "#8b4500;" "orange;" }
211                         { "#e3e2db;" "#444444;" }
212                         { "white;" "#202124;" }
213                         { "black;" "white;" }
214                     } ?at [
215                         but-last parse-color inverse-color color>hex ";" append
216                     ] unless
217                 ] re-replace-with
218             ] map " " join "{ " " }" surround
219         ] re-replace-with "    " prepend
220         dup "{  }" subseq-of? [ drop f ] when
221     ] map harvest append "}" suffix ;
222
223 : css-classes ( classes -- stylesheet )
224     [
225         [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
226     ] { } assoc>map fix-help-header dup dark-mode-css append join-lines ;
227
228 :: css-styles-to-classes ( body -- stylesheet body )
229     H{ } clone :> classes
230     body [
231         dup xml-chunk? [
232             seq>> [
233                 dup {
234                     [ tag? ]
235                     [ "style" attr ]
236                     [ "class" attr not ]
237                 } 1&& [
238                     [ clone [ V{ } like ] change-alist ] change-attrs
239                     "style" over delete-at* drop classes css-class
240                     "class" rot set-at
241                 ] [ drop ] if
242             ] deep-each
243         ] [ drop ] if
244     ] each classes sort-values css-classes body ;
245
246 : retina-image ( path -- path' )
247     dup "@2x" subseq-of? [ "." split1-last "@2x." glue ] unless ;
248
249 : ?copy-file ( from to -- )
250     dup file-exists? [ 2drop ] [ copy-file ] if ;
251
252 : cache-images ( body -- body' )
253     dup [
254         dup xml-chunk? [
255             seq>> [
256                 T{ name { main "img" } } over tag-named? [
257                     dup "src" attr
258                     retina-image dup file-name
259                     [ ?copy-file ] keep
260                     "src" set-attr
261                 ] [ drop ] if
262             ] deep-each
263         ] [ drop ] if
264     ] each ;
265
266 : help>html ( topic -- xml )
267     [ article-title " - Factor Documentation" append ]
268     [
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]
274     ] bi simple-page ;
275
276 : generate-help-file ( topic -- )
277     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
278
279 : all-vocabs-really ( -- seq )
280     all-disk-vocabs-recursive filter-vocabs
281     [ vocab-name "scratchpad" = ] reject ;
282
283 : all-topics ( -- topics )
284     [
285         articles get keys [ >link ] map %
286         all-words [ >link ] map %
287         all-authors [ <vocab-author> ] map %
288         all-tags [ <vocab-tag> ] map %
289         all-vocabs-really %
290     ] { } make ;
291
292 : serialize-index ( index file -- )
293     binary [
294         [ [ topic>filename ] dip ] { } assoc-map-as serialize
295     ] with-file-writer ;
296
297 : generate-article-index ( -- )
298     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
299     "articles.idx" serialize-index ;
300
301 : generate-word-index ( -- )
302     all-words [ dup name>> ] { } map>assoc
303     "words.idx" serialize-index ;
304
305 : generate-vocab-index ( -- )
306     all-vocabs-really [ dup vocab-name ] { } map>assoc
307     "vocabs.idx" serialize-index ;
308
309 : generate-indices ( -- )
310     generate-article-index
311     generate-word-index
312     generate-vocab-index ;
313
314 : generate-help-files ( -- )
315     H{
316         { recent-searches f }
317         { recent-words f }
318         { recent-articles f }
319         { recent-vocabs f }
320     } [
321         all-topics [ '[ _ generate-help-file ] try ] each
322     ] with-variables ;
323
324 : generate-help ( -- )
325     "docs" cache-file
326     [ make-directories ]
327     [
328         [
329             generate-indices
330             generate-help-files
331         ] with-directory
332     ] bi ;
333
334 MEMO: load-index ( name -- index )
335     binary file-contents bytes>object ;
336
337 : offline-apropos ( string index -- results )
338     load-index completions ;
339
340 : article-apropos ( string -- results )
341     "articles.idx" offline-apropos ;
342
343 : vocab-apropos ( string -- results )
344     "vocabs.idx" offline-apropos ;
345
346 : generate-qualified-index ( index -- )
347     H{ } clone [
348         '[
349             over "," split1 nip ".html" ?tail drop
350             [ swap ":" glue 2array ] [ _ push-at ] bi
351         ] assoc-each
352     ] keep [ swap ] { } assoc-map-as
353     "qualified.idx" binary [ serialize ] with-file-writer ;
354
355 : qualified-index ( str index -- str index' )
356     over ":" split1 [
357         "qualified.idx"
358         dup file-exists? [ pick generate-qualified-index ] unless
359         load-index completions keys concat
360     ] [ drop f ] if [ append ] unless-empty ;
361
362 : word-apropos ( string -- results )
363     "words.idx" load-index qualified-index completions ;