]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help: fix some html navigation styles.
[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 : help>html ( topic -- xml )
89     [ article-title " - Factor Documentation" append ]
90     [ drop help-stylesheet ]
91     [
92         [ help-navbar ]
93         [
94             [
95                 title-style get
96                 { { page-color COLOR: FactorLightTan } } assoc-union
97                 title-style [ print-topic ] with-variable
98             ] with-html-writer ]
99         bi* append
100     ] tri
101     simple-page ;
102
103 : generate-help-file ( topic -- )
104     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
105
106 : all-vocabs-really ( -- seq )
107     all-disk-vocabs-recursive no-roots remove-redundant-prefixes
108     [ vocab-name "scratchpad" = ] reject ;
109
110 : all-topics ( -- topics )
111     [
112         articles get keys [ >link ] map %
113         all-words [ >link ] map %
114         all-authors [ <vocab-author> ] map %
115         all-tags [ <vocab-tag> ] map %
116         all-vocabs-really %
117     ] { } make ;
118
119 : serialize-index ( index file -- )
120     binary [
121         [ [ topic>filename ] dip ] { } assoc-map-as serialize
122     ] with-file-writer ;
123
124 : generate-article-index ( -- )
125     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
126     "articles.idx" serialize-index ;
127
128 : generate-word-index ( -- )
129     all-words [ dup name>> ] { } map>assoc
130     "words.idx" serialize-index ;
131
132 : generate-vocab-index ( -- )
133     all-vocabs-really [ dup vocab-name ] { } map>assoc
134     "vocabs.idx" serialize-index ;
135
136 : generate-indices ( -- )
137     generate-article-index
138     generate-word-index
139     generate-vocab-index ;
140
141 : generate-help-files ( -- )
142     H{
143         { recent-searches f }
144         { recent-words f }
145         { recent-articles f }
146         { recent-vocabs f }
147     } [
148         all-topics [ '[ _ generate-help-file ] try ] each
149     ] with-variables ;
150
151 : generate-help ( -- )
152     "docs" cache-file
153     [ make-directories ]
154     [
155         [
156             generate-indices
157             generate-help-files
158         ] with-directory
159     ] bi ;
160
161 MEMO: load-index ( name -- index )
162     binary file-contents bytes>object ;
163
164 : offline-apropos ( string index -- results )
165     load-index completions ;
166
167 : article-apropos ( string -- results )
168     "articles.idx" offline-apropos ;
169
170 : word-apropos ( string -- results )
171     "words.idx" offline-apropos ;
172
173 : vocab-apropos ( string -- results )
174     "vocabs.idx" offline-apropos ;