]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: move pre-wrap to css style fixup.
[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.writer ;
10 FROM: io.encodings.ascii => ascii ;
11 FROM: ascii => ascii? ;
12 IN: help.html
13
14 : escape-char ( ch -- )
15     dup ascii? [
16         dup H{
17             { CHAR: \" "__quo__" }
18             { CHAR: * "__star__" }
19             { CHAR: : "__colon__" }
20             { CHAR: < "__lt__" }
21             { CHAR: > "__gt__" }
22             { CHAR: ? "__que__" }
23             { CHAR: \\ "__back__" }
24             { CHAR: | "__pipe__" }
25             { CHAR: / "__slash__" }
26             { CHAR: , "__comma__" }
27             { CHAR: @ "__at__" }
28             { CHAR: # "__hash__" }
29             { CHAR: % "__percent__" }
30         } at [ % ] [ , ] ?if
31     ] [ number>string "__" "__" surround % ] if ;
32
33 : escape-filename ( string -- filename )
34     [ [ escape-char ] each ] "" make ;
35
36 GENERIC: topic>filename* ( topic -- name prefix )
37
38 M: word topic>filename*
39     dup vocabulary>> [
40         [ name>> ] [ vocabulary>> ] bi 2array "word"
41     ] [ drop f f ] if ;
42
43 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
44 M: word-link topic>filename* name>> topic>filename* ;
45 M: vocab-spec topic>filename* vocab-name "vocab" ;
46 M: vocab-tag topic>filename* name>> "tag" ;
47 M: vocab-author topic>filename* name>> "author" ;
48 M: f topic>filename* drop \ f topic>filename* ;
49
50 : topic>filename ( topic -- filename )
51     topic>filename* [
52         [
53             % "-" %
54             dup array?
55             [ [ escape-filename ] map "," join ]
56             [ escape-filename ]
57             if % ".html" %
58         ] "" make
59     ] [ drop f ] if* ;
60
61 M: topic url-of topic>filename ;
62
63 M: pathname url-of
64     string>> "resource:" ?head [
65         "https://github.com/factor/factor/blob/master/"
66         prepend
67     ] [ drop f ] if ;
68
69 : help-stylesheet ( stylesheet -- xml )
70     "vocab:help/html/stylesheet.css" ascii file-contents
71     swap "\n" glue [XML <style><-></style> XML] ;
72
73 : help-meta ( -- xml )
74     [XML <meta
75             name="viewport"
76             content="width=device-width, initial-scale=1"
77         /> XML] ;
78
79 : help-navbar ( -- xml )
80     "conventions" >link topic>filename
81     [XML
82         <div class="navbar">
83         <a href="/">Handbook</a>
84         <a href=<->>Glossary</a>
85         <form method="get" action="/search" style="display:inline;">
86             <input placeholder="Search" name="search" type="text"/>
87             <input type="submit" value="Go"/>
88         </form>
89         <div style="float: right;">
90             <a href="//factorcode.org"><b>factorcode.org</b></a>
91         </div>
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 : 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 : css-classes ( classes -- stylesheet )
123     [
124         [ css-style " { " "}" surround ] [ "." prepend ] bi* prepend
125     ] { } assoc>map "\n" join ;
126
127 :: css-styles-to-classes ( body -- stylesheet body )
128     H{ } clone :> classes
129     body [
130         dup xml-chunk? [
131             seq>> [
132                 dup {
133                     [ tag? ]
134                     [ "style" attr ]
135                     [ "class" attr not ]
136                 } 1&& [
137                     [ clone [ V{ } like ] change-alist ] change-attrs
138                     "style" over delete-at* drop classes css-class
139                     "class" rot set-at
140                 ] [ drop ] if
141             ] deep-each
142         ] [ drop ] if
143     ] each classes sort-values css-classes body ;
144
145 : help>html ( topic -- xml )
146     [ article-title " - Factor Documentation" append ]
147     [
148         [ print-topic ] with-html-writer css-styles-to-classes
149         [ help-stylesheet help-meta prepend help-navbar ] dip
150         [XML <div id="container"><-><div class="page"><-></div></div> XML]
151     ] bi simple-page ;
152
153 : generate-help-file ( topic -- )
154     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
155
156 : all-vocabs-really ( -- seq )
157     all-disk-vocabs-recursive filter-vocabs
158     [ vocab-name "scratchpad" = ] reject ;
159
160 : all-topics ( -- topics )
161     [
162         articles get keys [ >link ] map %
163         all-words [ >link ] map %
164         all-authors [ <vocab-author> ] map %
165         all-tags [ <vocab-tag> ] map %
166         all-vocabs-really %
167     ] { } make ;
168
169 : serialize-index ( index file -- )
170     binary [
171         [ [ topic>filename ] dip ] { } assoc-map-as serialize
172     ] with-file-writer ;
173
174 : generate-article-index ( -- )
175     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
176     "articles.idx" serialize-index ;
177
178 : generate-word-index ( -- )
179     all-words [ dup name>> ] { } map>assoc
180     "words.idx" serialize-index ;
181
182 : generate-vocab-index ( -- )
183     all-vocabs-really [ dup vocab-name ] { } map>assoc
184     "vocabs.idx" serialize-index ;
185
186 : generate-indices ( -- )
187     generate-article-index
188     generate-word-index
189     generate-vocab-index ;
190
191 : generate-help-files ( -- )
192     H{
193         { recent-searches f }
194         { recent-words f }
195         { recent-articles f }
196         { recent-vocabs f }
197     } [
198         all-topics [ '[ _ generate-help-file ] try ] each
199     ] with-variables ;
200
201 : generate-help ( -- )
202     "docs" cache-file
203     [ make-directories ]
204     [
205         [
206             generate-indices
207             generate-help-files
208         ] with-directory
209     ] bi ;
210
211 MEMO: load-index ( name -- index )
212     binary file-contents bytes>object ;
213
214 : offline-apropos ( string index -- results )
215     load-index completions ;
216
217 : article-apropos ( string -- results )
218     "articles.idx" offline-apropos ;
219
220 : word-apropos ( string -- results )
221     "words.idx" offline-apropos ;
222
223 : vocab-apropos ( string -- results )
224     "vocabs.idx" offline-apropos ;