]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
webapps.help: remove frames. Fixes #242.
[factor.git] / basis / help / html / html.factor
1 ! Copyright (C) 2008, 2010 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="post" 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     [ [ print-topic ] with-html-writer help-navbar prepend ]
85     tri simple-page ;
86
87 : generate-help-file ( topic -- )
88     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
89
90 : all-vocabs-really ( -- seq )
91     all-vocabs-recursive >hashtable no-roots remove-redundant-prefixes
92     [ vocab-name "scratchpad" = not ] filter ;
93
94 : all-topics ( -- topics )
95     [
96         articles get keys [ >link ] map %
97         all-words [ >link ] map %
98         all-authors [ <vocab-author> ] map %
99         all-tags [ <vocab-tag> ] map %
100         all-vocabs-really %
101     ] { } make ;
102
103 : serialize-index ( index file -- )
104     [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
105     binary set-file-contents ;
106
107 : generate-indices ( -- )
108     articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
109     all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
110     all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
111
112 : (generate-help-files) ( -- )
113     all-topics [ '[ _ generate-help-file ] try ] each ;
114
115 : generate-help-files ( -- )
116     [
117         recent-searches off
118         recent-words off
119         recent-articles off
120         recent-vocabs off
121         (generate-help-files)
122     ] with-scope ;
123
124 : generate-help ( -- )
125     "docs" temp-file
126     [ make-directories ]
127     [
128         [
129             generate-indices
130             generate-help-files
131         ] with-directory
132     ] bi ;
133
134 MEMO: load-index ( name -- index )
135     binary file-contents bytes>object ;
136
137 TUPLE: result title href ;
138
139 : partition-exact ( string results -- results' )
140     [ title>> = ] with partition append ;
141
142 : offline-apropos ( string index -- results )
143     load-index over >lower
144     '[ [ drop _ ] dip >lower subseq? ] assoc-filter
145     [ swap result boa ] { } assoc>map
146     [ title>> ] sort-with
147     partition-exact ;
148
149 : article-apropos ( string -- results )
150     "articles.idx" offline-apropos ;
151
152 : word-apropos ( string -- results )
153     "words.idx" offline-apropos ;
154
155 : vocab-apropos ( string -- results )
156     "vocabs.idx" offline-apropos ;