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