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