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