]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: use GET not POST for search form, so that the search term appears in the...
[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     [XML
69         <div class="navbar">
70         <b> Factor Documentation </b> |
71         <a href="/">Home</a> |
72         <a href="article-conventions.html">Glossary</a> |
73         <form method="get" action="/search" style="display:inline;">
74             <input name="search" type="text"/>
75             <button type="submit">Search</button>
76         </form>
77         <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
78         </div>
79      XML] ;
80
81 : help>html ( topic -- xml )
82     [ article-title " - Factor Documentation" append ]
83     [ drop help-stylesheet ]
84     [
85         [ help-navbar ]
86         [ [ print-topic ] with-html-writer ]
87         bi* append
88     ] tri
89     simple-page ;
90
91 : generate-help-file ( topic -- )
92     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
93
94 : all-vocabs-really ( -- seq )
95     all-vocabs-recursive >hashtable no-roots remove-redundant-prefixes
96     [ vocab-name "scratchpad" = not ] filter ;
97
98 : all-topics ( -- topics )
99     [
100         articles get keys [ >link ] map %
101         all-words [ >link ] map %
102         all-authors [ <vocab-author> ] map %
103         all-tags [ <vocab-tag> ] map %
104         all-vocabs-really %
105     ] { } make ;
106
107 : serialize-index ( index file -- )
108     [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
109     binary set-file-contents ;
110
111 : generate-indices ( -- )
112     articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
113     all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
114     all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
115
116 : (generate-help-files) ( -- )
117     all-topics [ '[ _ generate-help-file ] try ] each ;
118
119 : generate-help-files ( -- )
120     [
121         recent-searches off
122         recent-words off
123         recent-articles off
124         recent-vocabs off
125         (generate-help-files)
126     ] with-scope ;
127
128 : generate-help ( -- )
129     "docs" temp-file
130     [ make-directories ]
131     [
132         [
133             generate-indices
134             generate-help-files
135         ] with-directory
136     ] bi ;
137
138 MEMO: load-index ( name -- index )
139     binary file-contents bytes>object ;
140
141 TUPLE: result title href ;
142
143 : partition-exact ( string results -- results' )
144     [ title>> = ] with partition append ;
145
146 : offline-apropos ( string index -- results )
147     load-index over >lower
148     '[ [ drop _ ] dip >lower subseq? ] assoc-filter
149     [ swap result boa ] { } assoc>map
150     [ title>> ] sort-with
151     partition-exact ;
152
153 : article-apropos ( string -- results )
154     "articles.idx" offline-apropos ;
155
156 : word-apropos ( string -- results )
157     "words.idx" offline-apropos ;
158
159 : vocab-apropos ( string -- results )
160     "vocabs.idx" offline-apropos ;