]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: assume even padding started out odd.
[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 fonts formatting fry help help.home help.topics
5 help.vocabs 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-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 default-font-size /f
97         "font-size: %.3frem;" sprintf
98     ] re-replace-with
99
100     R/ padding: \d+px;/ [
101         "padding: " ?head drop "px;" ?tail drop
102         string>number dup even? [ 2 * 1 + ] [ 2 * ] if
103         number>string "padding: " "px;" surround
104     ] re-replace-with ;
105
106 : css-classes ( classes -- stylesheet )
107     [
108         [ css-style " { " "}" surround ] [ "." prepend ] bi* prepend
109     ] { } assoc>map "\n" join ;
110
111 :: css-styles-to-classes ( body -- stylesheet body )
112     H{ } clone :> classes
113     body [
114         dup xml-chunk? [
115             seq>> [
116                 dup {
117                     [ tag? ]
118                     [ "style" attr ]
119                     [ "class" attr not ]
120                 } 1&& [
121                     [ clone [ V{ } like ] change-alist ] change-attrs
122                     "style" over delete-at* drop classes css-class
123                     "class" rot set-at
124                 ] [ drop ] if
125             ] deep-each
126         ] [ drop ] if
127     ] each classes sort-values css-classes body ;
128
129 : help>html ( topic -- xml )
130     [ article-title " - Factor Documentation" append ]
131     [
132         [ print-topic ] with-html-writer css-styles-to-classes
133         [ help-stylesheet help-navbar ] dip
134         [XML <-><div class="page"><-></div> XML]
135     ] bi simple-page ;
136
137 : generate-help-file ( topic -- )
138     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
139
140 : all-vocabs-really ( -- seq )
141     all-disk-vocabs-recursive filter-vocabs
142     [ vocab-name "scratchpad" = ] reject ;
143
144 : all-topics ( -- topics )
145     [
146         articles get keys [ >link ] map %
147         all-words [ >link ] map %
148         all-authors [ <vocab-author> ] map %
149         all-tags [ <vocab-tag> ] map %
150         all-vocabs-really %
151     ] { } make ;
152
153 : serialize-index ( index file -- )
154     binary [
155         [ [ topic>filename ] dip ] { } assoc-map-as serialize
156     ] with-file-writer ;
157
158 : generate-article-index ( -- )
159     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
160     "articles.idx" serialize-index ;
161
162 : generate-word-index ( -- )
163     all-words [ dup name>> ] { } map>assoc
164     "words.idx" serialize-index ;
165
166 : generate-vocab-index ( -- )
167     all-vocabs-really [ dup vocab-name ] { } map>assoc
168     "vocabs.idx" serialize-index ;
169
170 : generate-indices ( -- )
171     generate-article-index
172     generate-word-index
173     generate-vocab-index ;
174
175 : generate-help-files ( -- )
176     H{
177         { recent-searches f }
178         { recent-words f }
179         { recent-articles f }
180         { recent-vocabs f }
181     } [
182         all-topics [ '[ _ generate-help-file ] try ] each
183     ] with-variables ;
184
185 : generate-help ( -- )
186     "docs" cache-file
187     [ make-directories ]
188     [
189         [
190             generate-indices
191             generate-help-files
192         ] with-directory
193     ] bi ;
194
195 MEMO: load-index ( name -- index )
196     binary file-contents bytes>object ;
197
198 : offline-apropos ( string index -- results )
199     load-index completions ;
200
201 : article-apropos ( string -- results )
202     "articles.idx" offline-apropos ;
203
204 : word-apropos ( string -- results )
205     "words.idx" offline-apropos ;
206
207 : vocab-apropos ( string -- results )
208     "vocabs.idx" offline-apropos ;