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