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