]> gitweb.factorcode.org Git - factor.git/blob - basis/help/html/html.factor
help.html: escape % in word names (reported by Anton Gorenko)
[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
4 io.files io.files.temp io.directories html.streams help 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 ( -- string )
64     "vocab:help/html/stylesheet.css" ascii file-contents
65     [XML <style><-></style> XML] ;
66
67 : help>html ( topic -- xml )
68     [ article-title ]
69     [ drop help-stylesheet ]
70     [ [ print-topic ] with-html-writer ]
71     tri simple-page ;
72           
73 : generate-help-file ( topic -- )
74     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
75
76 : all-vocabs-really ( -- seq )
77     all-vocabs-recursive >hashtable no-roots remove-redundant-prefixes
78     [ vocab-name "scratchpad" = not ] filter ;
79
80 : all-topics ( -- topics )
81     [
82         articles get keys [ >link ] map %
83         all-words [ >link ] map %
84         all-authors [ <vocab-author> ] map %
85         all-tags [ <vocab-tag> ] map %
86         all-vocabs-really %
87     ] { } make ;
88
89 : serialize-index ( index file -- )
90     [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
91     binary set-file-contents ;
92
93 : generate-indices ( -- )
94     articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
95     all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
96     all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
97
98 : generate-help-files ( -- )
99     all-topics [ '[ _ generate-help-file ] try ] each ;
100
101 : generate-help ( -- )
102     "docs" temp-file
103     [ make-directories ]
104     [
105         [
106             generate-indices
107             generate-help-files
108         ] with-directory
109     ] bi ;
110
111 MEMO: load-index ( name -- index )
112     binary file-contents bytes>object ;
113
114 TUPLE: result title href ;
115
116 : offline-apropos ( string index -- results )
117     load-index swap >lower
118     '[ [ drop _ ] dip >lower subseq? ] assoc-filter
119     [ swap result boa ] { } assoc>map
120     [ title>> ] sort-with ;
121
122 : article-apropos ( string -- results )
123     "articles.idx" offline-apropos ;
124
125 : word-apropos ( string -- results )
126     "words.idx" offline-apropos ;
127
128 : vocab-apropos ( string -- results )
129     "vocabs.idx" offline-apropos ;