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