]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
7930f165b68af0669c2a30b02201e26fc894f526
[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 debugger fry help help.home
4 help.topics help.vocabs html html.streams io.directories
5 io.encodings.binary io.encodings.utf8 io.files io.files.temp
6 io.pathnames kernel make math.parser memoize namespaces
7 sequences serialize splitting tools.completion vocabs
8 vocabs.hierarchy words xml.syntax xml.writer ;
9 FROM: io.encodings.ascii => ascii ;
10 FROM: ascii => ascii? ;
11 IN: help.html
12
13 : escape-char ( ch -- )
14     dup ascii? [
15         dup H{
16             { CHAR: " "__quo__" }
17             { CHAR: * "__star__" }
18             { CHAR: : "__colon__" }
19             { CHAR: < "__lt__" }
20             { CHAR: > "__gt__" }
21             { CHAR: ? "__que__" }
22             { CHAR: \\ "__back__" }
23             { CHAR: | "__pipe__" }
24             { CHAR: / "__slash__" }
25             { CHAR: , "__comma__" }
26             { CHAR: @ "__at__" }
27             { CHAR: # "__hash__" }
28             { CHAR: % "__percent__" }
29         } at [ % ] [ , ] ?if
30     ] [ number>string "__" "__" surround % ] if ;
31
32 : escape-filename ( string -- filename )
33     [ [ escape-char ] each ] "" make ;
34
35 GENERIC: topic>filename* ( topic -- name prefix )
36
37 M: word topic>filename*
38     dup vocabulary>> [
39         [ name>> ] [ vocabulary>> ] bi 2array "word"
40     ] [ drop f f ] if ;
41
42 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
43 M: word-link topic>filename* name>> topic>filename* ;
44 M: vocab-spec topic>filename* vocab-name "vocab" ;
45 M: vocab-tag topic>filename* name>> "tag" ;
46 M: vocab-author topic>filename* name>> "author" ;
47 M: f topic>filename* drop \ f topic>filename* ;
48
49 : topic>filename ( topic -- filename )
50     topic>filename* [
51         [
52             % "-" %
53             dup array?
54             [ [ escape-filename ] map "," join ]
55             [ escape-filename ]
56             if % ".html" %
57         ] "" make
58     ] [ drop f ] if* ;
59
60 M: topic url-of topic>filename ;
61
62 M: pathname url-of
63     string>> "resource:" ?head [
64         "https://github.com/slavapestov/factor/blob/master/"
65         prepend
66     ] [ drop f ] if ;
67
68 : help-stylesheet ( -- xml )
69     "vocab:help/html/stylesheet.css" ascii file-contents
70     [XML <style><-></style> XML] ;
71
72 : help-navbar ( -- xml )
73     "conventions" >link topic>filename
74     [XML
75         <div class="navbar">
76         <b> Factor Documentation </b> |
77         <a href="/">Home</a> |
78         <a href=<->>Glossary</a> |
79         <form method="get" action="/search" style="display:inline;">
80             <input name="search" type="text"/>
81             <button type="submit">Search</button>
82         </form>
83         <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
84         </div>
85      XML] ;
86
87 : help>html ( topic -- xml )
88     [ article-title " - Factor Documentation" append ]
89     [ drop help-stylesheet ]
90     [
91         [ help-navbar ]
92         [ [ print-topic ] with-html-writer ]
93         bi* append
94     ] tri
95     simple-page ;
96
97 : generate-help-file ( topic -- )
98     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
99
100 : all-vocabs-really ( -- seq )
101     all-vocabs-recursive no-roots remove-redundant-prefixes
102     [ vocab-name "scratchpad" = not ] filter ;
103
104 : all-topics ( -- topics )
105     [
106         articles get keys [ >link ] map %
107         all-words [ >link ] map %
108         all-authors [ <vocab-author> ] map %
109         all-tags [ <vocab-tag> ] map %
110         all-vocabs-really %
111     ] { } make ;
112
113 : serialize-index ( index file -- )
114     binary [
115         [ [ topic>filename ] dip ] { } assoc-map-as serialize
116     ] with-file-writer ;
117
118 : generate-article-index ( -- )
119     articles get [ [ >link ] [ article-title ] bi* ] assoc-map
120     "articles.idx" serialize-index ;
121
122 : generate-word-index ( -- )
123     all-words [ dup name>> ] { } map>assoc
124     "words.idx" serialize-index ;
125
126 : generate-vocab-index ( -- )
127     all-vocabs-really [ dup vocab-name ] { } map>assoc
128     "vocabs.idx" serialize-index ;
129
130 : generate-indices ( -- )
131     generate-article-index
132     generate-word-index
133     generate-vocab-index ;
134
135 : generate-help-files ( -- )
136     H{
137         { recent-searches f }
138         { recent-words f }
139         { recent-articles f }
140         { recent-vocabs f }
141     } [
142         all-topics [ '[ _ generate-help-file ] try ] each
143     ] with-variables ;
144
145 : generate-help ( -- )
146     "docs" cache-file
147     [ make-directories ]
148     [
149         [
150             generate-indices
151             generate-help-files
152         ] with-directory
153     ] bi ;
154
155 MEMO: load-index ( name -- index )
156     binary file-contents bytes>object ;
157
158 : offline-apropos ( string index -- results )
159     load-index completions ;
160
161 : article-apropos ( string -- results )
162     "articles.idx" offline-apropos ;
163
164 : word-apropos ( string -- results )
165     "words.idx" offline-apropos ;
166
167 : vocab-apropos ( string -- results )
168     "vocabs.idx" offline-apropos ;