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