]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: don't use sprintf.
[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 fry help help.home help.topics help.vocabs html
5 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-navbar ( -- xml )
74     "conventions" >link topic>filename
75     [XML
76         <div class="navbar">
77         <a href="http://factorcode.org">factorcode.org</a>
78         <a href="/">Handbook</a>
79         <a href=<->>Glossary</a>
80         <form method="get" action="/search" style="display:inline;">
81             <input placeholder="Search" name="search" type="text"/>
82             <input type="submit" value="Go"/>
83         </form>
84         </div>
85      XML] ;
86
87 : bijective-base26 ( n -- name )
88     [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
89
90 : css-class ( style classes -- name )
91     dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
92
93 : css-style ( style -- style' )
94     R/ font-size: \d+pt;/ [
95         "font-size: " ?head drop "pt;" ?tail drop
96         string>number 12 /f number>string
97         "font-size: " "rem; " surround
98     ] re-replace-with ;
99
100 : css-classes ( classes -- stylesheet )
101     [
102         [ css-style " { " "}" surround ] [ "." prepend ] bi* prepend
103     ] { } assoc>map "\n" join ;
104
105 :: css-styles-to-classes ( body -- stylesheet body )
106     H{ } clone :> classes
107     body [
108         dup xml-chunk? [
109             seq>> [
110                 dup {
111                     [ tag? ]
112                     [ "style" attr ]
113                     [ "class" attr not ]
114                 } 1&& [
115                     attrs>> [ V{ } like ] change-alist
116                     "style" over delete-at* drop classes css-class
117                     "class" rot set-at
118                 ] [ drop ] if
119             ] deep-each
120         ] [ drop ] if
121     ] each classes sort-values css-classes body ;
122
123 : help>html ( topic -- xml )
124     [ article-title " - Factor Documentation" append ]
125     [
126         [ print-topic ] with-html-writer css-styles-to-classes
127         [ help-stylesheet help-navbar ] dip
128         [XML <-><div class="page"><-></div> XML]
129     ] bi simple-page ;
130
131 : generate-help-file ( topic -- )
132     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
133
134 : all-vocabs-really ( -- seq )
135     all-disk-vocabs-recursive filter-vocabs
136     [ vocab-name "scratchpad" = ] reject ;
137
138 : all-topics ( -- topics )
139     [
140         articles get keys [ >link ] map %
141         all-words [ >link ] map %
142         all-authors [ <vocab-author> ] map %
143         all-tags [ <vocab-tag> ] map %
144         all-vocabs-really %
145     ] { } make ;
146
147 : serialize-index ( index file -- )
148     binary [
149         [ [ topic>filename ] dip ] { } assoc-map-as serialize
150     ] with-file-writer ;
151
152 : generate-article-index ( -- )
153     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
154     "articles.idx" serialize-index ;
155
156 : generate-word-index ( -- )
157     all-words [ dup name>> ] { } map>assoc
158     "words.idx" serialize-index ;
159
160 : generate-vocab-index ( -- )
161     all-vocabs-really [ dup vocab-name ] { } map>assoc
162     "vocabs.idx" serialize-index ;
163
164 : generate-indices ( -- )
165     generate-article-index
166     generate-word-index
167     generate-vocab-index ;
168
169 : generate-help-files ( -- )
170     H{
171         { recent-searches f }
172         { recent-words f }
173         { recent-articles f }
174         { recent-vocabs f }
175     } [
176         all-topics [ '[ _ generate-help-file ] try ] each
177     ] with-variables ;
178
179 : generate-help ( -- )
180     "docs" cache-file
181     [ make-directories ]
182     [
183         [
184             generate-indices
185             generate-help-files
186         ] with-directory
187     ] bi ;
188
189 MEMO: load-index ( name -- index )
190     binary file-contents bytes>object ;
191
192 : offline-apropos ( string index -- results )
193     load-index completions ;
194
195 : article-apropos ( string -- results )
196     "articles.idx" offline-apropos ;
197
198 : word-apropos ( string -- results )
199     "words.idx" offline-apropos ;
200
201 : vocab-apropos ( string -- results )
202     "vocabs.idx" offline-apropos ;