]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: only input.focus().
[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 != null) {
101                     if (input !== document.activeElement) {
102                         event.preventDefault();
103                         setTimeout(function() {
104                             input.focus();
105                         }, 0);
106                     }
107                 }
108             }
109         });
110         </script>
111      XML] ;
112
113 : help-header ( stylesheet -- xml )
114     help-stylesheet help-meta swap help-script 3append ;
115
116 : help-nav ( -- xml )
117     "conventions" >link topic>filename
118     [XML
119         <nav>
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" />
123             </form>
124             <a href="https://factorcode.org">
125             <img src="favicon.ico" width="24" height="24" />
126             </a>
127             <a href="/">Handbook</a>
128             <a href=<->>Glossary</a>
129         </nav>
130      XML] ;
131
132 : help-footer ( -- xml )
133     version-info "\n" split1 drop
134     [XML
135         <footer>
136         <p>
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.
143         </p>
144         <p><-></p>
145         </footer>
146     XML] ;
147
148 : bijective-base26 ( n -- name )
149     [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
150
151 : css-class ( style classes -- name )
152     dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
153
154 : fix-css-style ( style -- style' )
155     R/ font-size: \d+pt;/ [
156         "font-size: " ?head drop "pt;" ?tail drop
157         string>number 2 -
158         "font-size: %dpt;" sprintf
159     ] re-replace-with
160
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
165     ] re-replace-with
166
167     R/ width: \d+px;/ [
168        drop ""
169     ] re-replace-with
170
171     R/ font-family: monospace;/ [
172         " white-space: pre-wrap; line-height: 125%;" append
173     ] re-replace-with
174
175     dup { "font-family: monospace;" "background-color:" } [ subseq-of? ] with all? [
176         " margin: 10px 0px;" append
177     ] when
178
179     dup { "border:" "background-color:" } [ subseq-of? ] with all? [
180         " border-radius: 5px;" append
181     ] when ;
182
183 : fix-help-header ( classes -- classes )
184     dup [
185         [ ".a" head? ] [ "#f4efd9;" subseq-of? ] bi and
186     ] find [
187         "padding: 10px;" "padding: 0px;" replace
188         "background-color: #f4efd9;" "background-color: white;" replace
189         "}" ?tail drop
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; }"
196         } prepend
197     ] [ drop ] if* ;
198
199 : dark-mode-css ( classes -- classes' )
200     { "" "/* Dark mode */" "@media (prefers-color-scheme:dark) {" }
201     swap [
202         R/ {[^}]+}/ [
203             "{" ?head drop "}" ?tail drop ";" split
204             [ [ blank? ] trim ] map harvest [ ";" append ] map
205             [ R/ (#[0-9a-fA-F]+|white|black);/ re-contains? ] filter
206             [
207                 R/ (#[0-9a-fA-F]+|white|black);/ [
208                     >string H{
209                         { "#000000;" "#bdc1c6;" }
210                         { "#2a5db0;" "#8ab4f8;" }
211                         { "#333333;" "#d5d5d5;" }
212                         { "#373e48;" "#ffffff;" }
213                         { "#8b4500;" "orange;" }
214                         { "#e3e2db;" "#444444;" }
215                         { "white;" "#202124;" }
216                         { "black;" "white;" }
217                     } ?at [
218                         but-last parse-color inverse-color color>hex ";" append
219                     ] unless
220                 ] re-replace-with
221             ] map " " join "{ " " }" surround
222         ] re-replace-with "    " prepend
223         dup "{  }" subseq-of? [ drop f ] when
224     ] map harvest append "}" suffix ;
225
226 : css-classes ( classes -- stylesheet )
227     [
228         [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
229     ] { } assoc>map fix-help-header dup dark-mode-css append join-lines ;
230
231 :: css-styles-to-classes ( body -- stylesheet body )
232     H{ } clone :> classes
233     body [
234         dup xml-chunk? [
235             seq>> [
236                 dup {
237                     [ tag? ]
238                     [ "style" attr ]
239                     [ "class" attr not ]
240                 } 1&& [
241                     [ clone [ V{ } like ] change-alist ] change-attrs
242                     "style" over delete-at* drop classes css-class
243                     "class" rot set-at
244                 ] [ drop ] if
245             ] deep-each
246         ] [ drop ] if
247     ] each classes sort-values css-classes body ;
248
249 : retina-image ( path -- path' )
250     dup "@2x" subseq-of? [ "." split1-last "@2x." glue ] unless ;
251
252 : ?copy-file ( from to -- )
253     dup file-exists? [ 2drop ] [ copy-file ] if ;
254
255 : cache-images ( body -- body' )
256     dup [
257         dup xml-chunk? [
258             seq>> [
259                 T{ name { main "img" } } over tag-named? [
260                     dup "src" attr
261                     retina-image dup file-name
262                     [ ?copy-file ] keep
263                     "src" set-attr
264                 ] [ drop ] if
265             ] deep-each
266         ] [ drop ] if
267     ] each ;
268
269 : help>html ( topic -- xml )
270     [ article-title " - Factor Documentation" append ]
271     [
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]
277     ] bi simple-page ;
278
279 : generate-help-file ( topic -- )
280     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
281
282 : all-vocabs-really ( -- seq )
283     all-disk-vocabs-recursive filter-vocabs
284     [ vocab-name "scratchpad" = ] reject ;
285
286 : all-topics ( -- topics )
287     [
288         articles get keys [ >link ] map %
289         all-words [ >link ] map %
290         all-authors [ <vocab-author> ] map %
291         all-tags [ <vocab-tag> ] map %
292         all-vocabs-really %
293     ] { } make ;
294
295 : serialize-index ( index file -- )
296     binary [
297         [ [ topic>filename ] dip ] { } assoc-map-as serialize
298     ] with-file-writer ;
299
300 : generate-article-index ( -- )
301     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
302     "articles.idx" serialize-index ;
303
304 : generate-word-index ( -- )
305     all-words [ dup name>> ] { } map>assoc
306     "words.idx" serialize-index ;
307
308 : generate-vocab-index ( -- )
309     all-vocabs-really [ dup vocab-name ] { } map>assoc
310     "vocabs.idx" serialize-index ;
311
312 : generate-indices ( -- )
313     generate-article-index
314     generate-word-index
315     generate-vocab-index ;
316
317 : generate-help-files ( -- )
318     H{
319         { recent-searches f }
320         { recent-words f }
321         { recent-articles f }
322         { recent-vocabs f }
323     } [
324         all-topics [ '[ _ generate-help-file ] try ] each
325     ] with-variables ;
326
327 : generate-help ( -- )
328     "docs" cache-file
329     [ make-directories ]
330     [
331         [
332             generate-indices
333             generate-help-files
334         ] with-directory
335     ] bi ;
336
337 MEMO: load-index ( name -- index )
338     binary file-contents bytes>object ;
339
340 : offline-apropos ( string index -- results )
341     load-index completions ;
342
343 : article-apropos ( string -- results )
344     "articles.idx" offline-apropos ;
345
346 : vocab-apropos ( string -- results )
347     "vocabs.idx" offline-apropos ;
348
349 : generate-qualified-index ( index -- )
350     H{ } clone [
351         '[
352             over "," split1 nip ".html" ?tail drop
353             [ swap ":" glue 2array ] [ _ push-at ] bi
354         ] assoc-each
355     ] keep [ swap ] { } assoc-map-as
356     "qualified.idx" binary [ serialize ] with-file-writer ;
357
358 : qualified-index ( str index -- str index' )
359     over ":" split1 [
360         "qualified.idx"
361         dup file-exists? [ pick generate-qualified-index ] unless
362         load-index completions keys concat
363     ] [ drop f ] if [ append ] unless-empty ;
364
365 : word-apropos ( string -- results )
366     "words.idx" load-index qualified-index completions ;