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