]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: fix navigation links at top.
[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 colors.constants debugger fry
4 help help.crossref help.home help.markup help.stylesheet
5 help.topics help.vocabs html html.streams io io.directories
6 io.encodings.binary io.encodings.utf8 io.files io.files.temp
7 io.pathnames io.styles kernel make math.parser memoize
8 namespaces sequences serialize splitting tools.completion vocabs
9 vocabs.hierarchy words 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/slavapestov/factor/blob/master/"
66         prepend
67     ] [ drop f ] if ;
68
69 : help-stylesheet ( -- xml )
70     "vocab:help/html/stylesheet.css" ascii file-contents
71     [XML <style><-></style> XML] ;
72
73 : help-navbar ( -- xml )
74     "conventions" >link topic>filename
75     [XML
76         <div class="navbar">
77         <b> Factor Documentation </b> |
78         <a href="/">Home</a> |
79         <a href=<->>Glossary</a> |
80         <form method="get" action="/search" style="display:inline;">
81             <input name="search" type="text"/>
82             <button type="submit">Search</button>
83         </form>
84         <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
85         </div>
86      XML] ;
87
88 : $navigation-row ( content element label -- )
89     [ prefix 1array ] dip prefix , ;
90
91 : ($navigation-links) ( topic -- )
92     help-path-style get [
93         [
94             [ prev-article [ 1array \ $long-link "Prev:" $navigation-row ] when* ]
95             [ next-article [ 1array \ $long-link "Next:" $navigation-row ] when* ]
96             bi
97         ] { } make [ ($navigation-table) ] unless-empty
98     ] with-style ;
99
100 : $title ( topic -- )
101     title-style get
102     { { page-color COLOR: FactorLightTan } } assoc-union dup
103     [
104         [
105             [ ($title) ]
106             [ ($navigation-path) ]
107             [ ($navigation-links) ] tri
108         ] with-nesting
109     ] with-style ;
110
111 : help>html ( topic -- xml )
112     [ article-title " - Factor Documentation" append ]
113     [ drop help-stylesheet ]
114     [
115         [ help-navbar ]
116         [ [ [ $title ($blank-line) ] [ print-topic ] bi ] with-html-writer ]
117         bi* append
118     ] tri
119     simple-page ;
120
121 : generate-help-file ( topic -- )
122     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
123
124 : all-vocabs-really ( -- seq )
125     all-disk-vocabs-recursive no-roots remove-redundant-prefixes
126     [ vocab-name "scratchpad" = ] reject ;
127
128 : all-topics ( -- topics )
129     [
130         articles get keys [ >link ] map %
131         all-words [ >link ] map %
132         all-authors [ <vocab-author> ] map %
133         all-tags [ <vocab-tag> ] map %
134         all-vocabs-really %
135     ] { } make ;
136
137 : serialize-index ( index file -- )
138     binary [
139         [ [ topic>filename ] dip ] { } assoc-map-as serialize
140     ] with-file-writer ;
141
142 : generate-article-index ( -- )
143     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
144     "articles.idx" serialize-index ;
145
146 : generate-word-index ( -- )
147     all-words [ dup name>> ] { } map>assoc
148     "words.idx" serialize-index ;
149
150 : generate-vocab-index ( -- )
151     all-vocabs-really [ dup vocab-name ] { } map>assoc
152     "vocabs.idx" serialize-index ;
153
154 : generate-indices ( -- )
155     generate-article-index
156     generate-word-index
157     generate-vocab-index ;
158
159 : generate-help-files ( -- )
160     H{
161         { recent-searches f }
162         { recent-words f }
163         { recent-articles f }
164         { recent-vocabs f }
165     } [
166         all-topics [ '[ _ generate-help-file ] try ] each
167     ] with-variables ;
168
169 : generate-help ( -- )
170     "docs" cache-file
171     [ make-directories ]
172     [
173         [
174             generate-indices
175             generate-help-files
176         ] with-directory
177     ] bi ;
178
179 MEMO: load-index ( name -- index )
180     binary file-contents bytes>object ;
181
182 : offline-apropos ( string index -- results )
183     load-index completions ;
184
185 : article-apropos ( string -- results )
186     "articles.idx" offline-apropos ;
187
188 : word-apropos ( string -- results )
189     "words.idx" offline-apropos ;
190
191 : vocab-apropos ( string -- results )
192     "vocabs.idx" offline-apropos ;