]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: title should be black
[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 assocs combinators.short-circuit
4 debugger formatting fry help help.home help.topics help.vocabs
5 html html.streams io.directories io.encodings.binary
6 io.encodings.utf8 io.files io.files.temp io.pathnames kernel
7 locals make math math.parser memoize namespaces regexp sequences
8 sequences.deep serialize sorting splitting system tools.completion
9 vocabs vocabs.hierarchy words xml.data xml.syntax xml.traversal
10 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         <a href="/">Handbook</a>
86         <a href=<->>Glossary</a>
87         <form method="get" action="/search" style="float: right;">
88             <input placeholder="Search" name="search" type="text"/>
89             <input type="submit" value="Go"/>
90             <a href="https://factorcode.org">factorcode.org</a>
91         </form>
92         </div>
93      XML] ;
94
95 : help-footer ( -- xml )
96     version-info "\n" split1 drop
97     [XML
98         <div class="footer">
99         <p>
100         This documentation was generated offline from a
101         <code>load-all</code> image.  If you want, you can also
102         browse the documentation from within the <a
103         href="article-ui-tools.html">UI developer tools</a>. See
104         the <a href="https://factorcode.org">Factor website</a>
105         for more information.
106         </p>
107         <p><-></p>
108         </div>
109     XML] ;
110
111 : bijective-base26 ( n -- name )
112     [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
113
114 : css-class ( style classes -- name )
115     dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
116
117 : fix-css-style ( style -- style' )
118     R/ font-size: \d+pt;/ [
119         "font-size: " ?head drop "pt;" ?tail drop
120         string>number 2 -
121         "font-size: %dpt;" sprintf
122     ] re-replace-with
123
124     R/ padding: \d+px;/ [
125         "padding: " ?head drop "px;" ?tail drop
126         string>number dup even? [ 2 * 1 + ] [ 2 * ] if
127         number>string "padding: " "px;" surround
128     ] re-replace-with
129
130     R/ width: \d+px;/ [
131        drop ""
132     ] re-replace-with
133
134     R/ font-family: monospace;/ [
135         " white-space: pre-wrap; line-height: 125%;" append
136     ] re-replace-with ;
137
138 : fix-help-header ( classes -- classes )
139     dup [
140         [ ".a" head? ] [ "#f4efd9;" swap subseq? ] bi and
141     ] find [
142         "padding: 10px;" "padding: 0px;" replace
143         "background-color: #f4efd9;" "background-color: white;" replace
144         "}" ?tail drop
145         " border-bottom: 1px dashed #ccc; width: 100%; padding-bottom: 10px; }"
146         append swap pick set-nth
147         ".a tr:hover { background-color: white; }" prefix
148         ".a td { border: none; }" prefix
149         ".a * a { color: #2A5DB0; }" prefix
150         ".a a { color: black; }" prefix
151     ] [ drop ] if* ;
152
153 : css-classes ( classes -- stylesheet )
154     [
155         [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
156     ] { } assoc>map fix-help-header join-lines ;
157
158 :: css-styles-to-classes ( body -- stylesheet body )
159     H{ } clone :> classes
160     body [
161         dup xml-chunk? [
162             seq>> [
163                 dup {
164                     [ tag? ]
165                     [ "style" attr ]
166                     [ "class" attr not ]
167                 } 1&& [
168                     [ clone [ V{ } like ] change-alist ] change-attrs
169                     "style" over delete-at* drop classes css-class
170                     "class" rot set-at
171                 ] [ drop ] if
172             ] deep-each
173         ] [ drop ] if
174     ] each classes sort-values css-classes body ;
175
176 : retina-image ( path -- path' )
177     "@2x" over subseq? [ "." split1-last "@2x." glue ] unless ;
178
179 : ?copy-file ( from to -- )
180     dup file-exists? [ 2drop ] [ copy-file ] if ;
181
182 : cache-images ( body -- body' )
183     dup [
184         dup xml-chunk? [
185             seq>> [
186                 T{ name { main "img" } } over tag-named? [
187                     dup "src" attr
188                     retina-image dup file-name
189                     [ ?copy-file ] keep
190                     "src" set-attr
191                 ] [ drop ] if
192             ] deep-each
193         ] [ drop ] if
194     ] each ;
195
196 : help>html ( topic -- xml )
197     [ article-title " - Factor Documentation" append ]
198     [
199         [ print-topic ] with-html-writer
200         css-styles-to-classes cache-images
201         [ help-stylesheet help-meta prepend help-navbar ] dip help-footer
202         [XML <div id="container"><-><div class="page"><-><-></div></div> XML]
203     ] bi simple-page ;
204
205 : generate-help-file ( topic -- )
206     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
207
208 : all-vocabs-really ( -- seq )
209     all-disk-vocabs-recursive filter-vocabs
210     [ vocab-name "scratchpad" = ] reject ;
211
212 : all-topics ( -- topics )
213     [
214         articles get keys [ >link ] map %
215         all-words [ >link ] map %
216         all-authors [ <vocab-author> ] map %
217         all-tags [ <vocab-tag> ] map %
218         all-vocabs-really %
219     ] { } make ;
220
221 : serialize-index ( index file -- )
222     binary [
223         [ [ topic>filename ] dip ] { } assoc-map-as serialize
224     ] with-file-writer ;
225
226 : generate-article-index ( -- )
227     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
228     "articles.idx" serialize-index ;
229
230 : generate-word-index ( -- )
231     all-words [ dup name>> ] { } map>assoc
232     "words.idx" serialize-index ;
233
234 : generate-vocab-index ( -- )
235     all-vocabs-really [ dup vocab-name ] { } map>assoc
236     "vocabs.idx" serialize-index ;
237
238 : generate-indices ( -- )
239     generate-article-index
240     generate-word-index
241     generate-vocab-index ;
242
243 : generate-help-files ( -- )
244     H{
245         { recent-searches f }
246         { recent-words f }
247         { recent-articles f }
248         { recent-vocabs f }
249     } [
250         all-topics [ '[ _ generate-help-file ] try ] each
251     ] with-variables ;
252
253 : generate-help ( -- )
254     "docs" cache-file
255     [ make-directories ]
256     [
257         [
258             generate-indices
259             generate-help-files
260         ] with-directory
261     ] bi ;
262
263 MEMO: load-index ( name -- index )
264     binary file-contents bytes>object ;
265
266 : offline-apropos ( string index -- results )
267     load-index completions ;
268
269 : article-apropos ( string -- results )
270     "articles.idx" offline-apropos ;
271
272 : word-apropos ( string -- results )
273     "words.idx" offline-apropos ;
274
275 : vocab-apropos ( string -- results )
276     "vocabs.idx" offline-apropos ;