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