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