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