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