]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: adding a page footer with version-info
[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         <br />
99         <p style="font-size: smaller;">
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 style="font-size: smaller;"><-></p>
108     XML] ;
109
110 : bijective-base26 ( n -- name )
111     [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
112
113 : css-class ( style classes -- name )
114     dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
115
116 : fix-css-style ( style -- style' )
117     R/ font-size: \d+pt;/ [
118         "font-size: " ?head drop "pt;" ?tail drop
119         string>number 2 -
120         "font-size: %dpt;" sprintf
121     ] re-replace-with
122
123     R/ padding: \d+px;/ [
124         "padding: " ?head drop "px;" ?tail drop
125         string>number dup even? [ 2 * 1 + ] [ 2 * ] if
126         number>string "padding: " "px;" surround
127     ] re-replace-with
128
129     R/ width: \d+px;/ [
130        drop ""
131     ] re-replace-with
132
133     R/ font-family: monospace;/ [
134         " white-space: pre-wrap; line-height: 125%;" append
135     ] re-replace-with ;
136
137 : fix-help-header ( classes -- classes )
138     dup [
139         [ ".a" head? ] [ "#f3f2ea;" swap subseq? ] bi and
140     ] find [
141         "background-color: #f3f2ea;" "background-color: #f4efd9;" replace
142         "padding: 10px;" "padding: 15px;" replace
143         "}" ?tail drop
144         " border-bottom: 1px solid #ccc; width: calc(100% + 30px); margin: -15px; }"
145         append swap pick set-nth
146         ".a tr:hover { background-color: #f4efd9; }" prefix
147     ] [ drop ] if* ;
148
149 : css-classes ( classes -- stylesheet )
150     [
151         [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
152     ] { } assoc>map fix-help-header join-lines ;
153
154 :: css-styles-to-classes ( body -- stylesheet body )
155     H{ } clone :> classes
156     body [
157         dup xml-chunk? [
158             seq>> [
159                 dup {
160                     [ tag? ]
161                     [ "style" attr ]
162                     [ "class" attr not ]
163                 } 1&& [
164                     [ clone [ V{ } like ] change-alist ] change-attrs
165                     "style" over delete-at* drop classes css-class
166                     "class" rot set-at
167                 ] [ drop ] if
168             ] deep-each
169         ] [ drop ] if
170     ] each classes sort-values css-classes body ;
171
172 : retina-image ( path -- path' )
173     "@2x" over subseq? [ "." split1-last "@2x." glue ] unless ;
174
175 : ?copy-file ( from to -- )
176     dup file-exists? [ 2drop ] [ copy-file ] if ;
177
178 : cache-images ( body -- body' )
179     dup [
180         dup xml-chunk? [
181             seq>> [
182                 T{ name { main "img" } } over tag-named? [
183                     dup "src" attr
184                     retina-image dup file-name
185                     [ ?copy-file ] keep
186                     "src" set-attr
187                 ] [ drop ] if
188             ] deep-each
189         ] [ drop ] if
190     ] each ;
191
192 : help>html ( topic -- xml )
193     [ article-title " - Factor Documentation" append ]
194     [
195         [ print-topic ] with-html-writer
196         css-styles-to-classes cache-images
197         [ help-stylesheet help-meta prepend help-navbar ] dip help-footer
198         [XML <div id="container"><-><div class="page"><-><-></div></div> XML]
199     ] bi simple-page ;
200
201 : generate-help-file ( topic -- )
202     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
203
204 : all-vocabs-really ( -- seq )
205     all-disk-vocabs-recursive filter-vocabs
206     [ vocab-name "scratchpad" = ] reject ;
207
208 : all-topics ( -- topics )
209     [
210         articles get keys [ >link ] map %
211         all-words [ >link ] map %
212         all-authors [ <vocab-author> ] map %
213         all-tags [ <vocab-tag> ] map %
214         all-vocabs-really %
215     ] { } make ;
216
217 : serialize-index ( index file -- )
218     binary [
219         [ [ topic>filename ] dip ] { } assoc-map-as serialize
220     ] with-file-writer ;
221
222 : generate-article-index ( -- )
223     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
224     "articles.idx" serialize-index ;
225
226 : generate-word-index ( -- )
227     all-words [ dup name>> ] { } map>assoc
228     "words.idx" serialize-index ;
229
230 : generate-vocab-index ( -- )
231     all-vocabs-really [ dup vocab-name ] { } map>assoc
232     "vocabs.idx" serialize-index ;
233
234 : generate-indices ( -- )
235     generate-article-index
236     generate-word-index
237     generate-vocab-index ;
238
239 : generate-help-files ( -- )
240     H{
241         { recent-searches f }
242         { recent-words f }
243         { recent-articles f }
244         { recent-vocabs f }
245     } [
246         all-topics [ '[ _ generate-help-file ] try ] each
247     ] with-variables ;
248
249 : generate-help ( -- )
250     "docs" cache-file
251     [ make-directories ]
252     [
253         [
254             generate-indices
255             generate-help-files
256         ] with-directory
257     ] bi ;
258
259 MEMO: load-index ( name -- index )
260     binary file-contents bytes>object ;
261
262 : offline-apropos ( string index -- results )
263     load-index completions ;
264
265 : article-apropos ( string -- results )
266     "articles.idx" offline-apropos ;
267
268 : word-apropos ( string -- results )
269     "words.idx" offline-apropos ;
270
271 : vocab-apropos ( string -- results )
272     "vocabs.idx" offline-apropos ;