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