]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: change back to the old naming scheme because we have too many links to...
[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: io.encodings.utf8 io.encodings.binary io.files
4 io.files.temp io.directories html.streams help help.home kernel
5 assocs sequences make words accessors arrays help.topics vocabs
6 vocabs.hierarchy help.vocabs namespaces prettyprint io
7 vocabs.loader serialize fry memoize unicode.case math.order
8 sorting debugger html xml.syntax xml.writer math.parser
9 sets hashtables ;
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* dup [
52         [
53             % "-" %
54             dup array?
55             [ [ escape-filename ] map "," join ]
56             [ escape-filename ]
57             if % ".html" %
58         ] "" make
59     ] [ 2drop f ] if ;
60
61 M: topic url-of topic>filename ;
62
63 : help-stylesheet ( -- xml )
64     "vocab:help/html/stylesheet.css" ascii file-contents
65     [XML <style><-></style> XML] ;
66
67 : help-navbar ( -- xml )
68     "conventions" >link topic>filename
69     [XML
70         <div class="navbar">
71         <b> Factor Documentation </b> |
72         <a href="/">Home</a> |
73         <a href=<->>Glossary</a> |
74         <form method="get" action="/search" style="display:inline;">
75             <input name="search" type="text"/>
76             <button type="submit">Search</button>
77         </form>
78         <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
79         </div>
80      XML] ;
81
82 : help>html ( topic -- xml )
83     [ article-title " - Factor Documentation" append ]
84     [ drop help-stylesheet ]
85     [
86         [ help-navbar ]
87         [ [ print-topic ] with-html-writer ]
88         bi* append
89     ] tri
90     simple-page ;
91
92 : generate-help-file ( topic -- )
93     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
94
95 : all-vocabs-really ( -- seq )
96     all-vocabs-recursive >hashtable no-roots remove-redundant-prefixes
97     [ vocab-name "scratchpad" = not ] filter ;
98
99 : all-topics ( -- topics )
100     [
101         articles get keys [ >link ] map %
102         all-words [ >link ] map %
103         all-authors [ <vocab-author> ] map %
104         all-tags [ <vocab-tag> ] map %
105         all-vocabs-really %
106     ] { } make ;
107
108 : serialize-index ( index file -- )
109     [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
110     binary set-file-contents ;
111
112 : generate-indices ( -- )
113     articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
114     all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
115     all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
116
117 : (generate-help-files) ( -- )
118     all-topics [ '[ _ generate-help-file ] try ] each ;
119
120 : generate-help-files ( -- )
121     [
122         recent-searches off
123         recent-words off
124         recent-articles off
125         recent-vocabs off
126         (generate-help-files)
127     ] with-scope ;
128
129 : generate-help ( -- )
130     "docs" temp-file
131     [ make-directories ]
132     [
133         [
134             generate-indices
135             generate-help-files
136         ] with-directory
137     ] bi ;
138
139 MEMO: load-index ( name -- index )
140     binary file-contents bytes>object ;
141
142 TUPLE: result title href ;
143
144 : partition-exact ( string results -- results' )
145     [ title>> = ] with partition append ;
146
147 : offline-apropos ( string index -- results )
148     load-index over >lower
149     '[ [ drop _ ] dip >lower subseq? ] assoc-filter
150     [ swap result boa ] { } assoc>map
151     [ title>> ] sort-with
152     partition-exact ;
153
154 : article-apropos ( string -- results )
155     "articles.idx" offline-apropos ;
156
157 : word-apropos ( string -- results )
158     "words.idx" offline-apropos ;
159
160 : vocab-apropos ( string -- results )
161     "vocabs.idx" offline-apropos ;