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