1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators.short-circuit
4 debugger fry help help.home help.topics help.vocabs html
5 html.streams io.directories io.encodings.binary
6 io.encodings.utf8 io.files io.files.temp io.pathnames kernel
7 locals make math math.parser memoize namespaces sequences
8 serialize sorting splitting tools.completion vocabs
9 vocabs.hierarchy words xml.data xml.syntax xml.traversal
11 FROM: io.encodings.ascii => ascii ;
12 FROM: ascii => ascii? ;
15 : escape-char ( ch -- )
18 { CHAR: \" "__quo__" }
19 { CHAR: * "__star__" }
20 { CHAR: : "__colon__" }
24 { CHAR: \\ "__back__" }
25 { CHAR: | "__pipe__" }
26 { CHAR: / "__slash__" }
27 { CHAR: , "__comma__" }
29 { CHAR: # "__hash__" }
30 { CHAR: % "__percent__" }
32 ] [ number>string "__" "__" surround % ] if ;
34 : escape-filename ( string -- filename )
35 [ [ escape-char ] each ] "" make ;
37 GENERIC: topic>filename* ( topic -- name prefix )
39 M: word topic>filename*
41 [ name>> ] [ vocabulary>> ] bi 2array "word"
44 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
45 M: word-link topic>filename* name>> topic>filename* ;
46 M: vocab-spec topic>filename* vocab-name "vocab" ;
47 M: vocab-tag topic>filename* name>> "tag" ;
48 M: vocab-author topic>filename* name>> "author" ;
49 M: f topic>filename* drop \ f topic>filename* ;
51 : topic>filename ( topic -- filename )
56 [ [ escape-filename ] map "," join ]
62 M: topic url-of topic>filename ;
65 string>> "resource:" ?head [
66 "https://github.com/factor/factor/blob/master/"
70 : help-stylesheet ( stylesheet -- xml )
71 "vocab:help/html/stylesheet.css" ascii file-contents
72 swap "\n" glue [XML <style><-></style> XML] ;
74 : help-navbar ( -- xml )
75 "conventions" >link topic>filename
78 <b> Factor Documentation </b> |
79 <a href="http://factorcode.org">Factor</a>
81 <a href=<->>Glossary</a>
82 <form method="get" action="/search" style="display:inline;">
83 <input placeholder="Search" name="search" type="text"/>
84 <input type="submit" value="Go"/>
86 <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
90 : bijective-base26 ( n -- name )
91 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
93 : css-class ( style classes -- name )
94 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
96 : css-classes ( classes -- stylesheet )
98 [ " { " "}" surround ] [ "." prepend ] bi* prepend
99 ] { } assoc>map "\n" join ;
101 :: css-styles-to-classes ( body -- stylesheet body )
102 H{ } clone :> classes
105 seq>> [ tag? ] filter
106 "span" "div" [ deep-tags-named ] bi-curry@ bi append
112 attrs>> [ V{ } like ] change-alist
113 "style" over delete-at* drop classes css-class
118 ] each classes sort-values css-classes body ;
120 : help>html ( topic -- xml )
121 [ article-title " - Factor Documentation" append ]
123 [ print-topic ] with-html-writer css-styles-to-classes
124 [ help-stylesheet help-navbar ] dip
125 [XML <-><div class="page"><-></div> XML]
128 : generate-help-file ( topic -- )
129 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
131 : all-vocabs-really ( -- seq )
132 all-disk-vocabs-recursive filter-vocabs
133 [ vocab-name "scratchpad" = ] reject ;
135 : all-topics ( -- topics )
137 articles get keys [ >link ] map %
138 all-words [ >link ] map %
139 all-authors [ <vocab-author> ] map %
140 all-tags [ <vocab-tag> ] map %
144 : serialize-index ( index file -- )
146 [ [ topic>filename ] dip ] { } assoc-map-as serialize
149 : generate-article-index ( -- )
150 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
151 "articles.idx" serialize-index ;
153 : generate-word-index ( -- )
154 all-words [ dup name>> ] { } map>assoc
155 "words.idx" serialize-index ;
157 : generate-vocab-index ( -- )
158 all-vocabs-really [ dup vocab-name ] { } map>assoc
159 "vocabs.idx" serialize-index ;
161 : generate-indices ( -- )
162 generate-article-index
164 generate-vocab-index ;
166 : generate-help-files ( -- )
168 { recent-searches f }
170 { recent-articles f }
173 all-topics [ '[ _ generate-help-file ] try ] each
176 : generate-help ( -- )
186 MEMO: load-index ( name -- index )
187 binary file-contents bytes>object ;
189 : offline-apropos ( string index -- results )
190 load-index completions ;
192 : article-apropos ( string -- results )
193 "articles.idx" offline-apropos ;
195 : word-apropos ( string -- results )
196 "words.idx" offline-apropos ;
198 : vocab-apropos ( string -- results )
199 "vocabs.idx" offline-apropos ;