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 sequences.deep serialize sorting splitting tools.completion
9 vocabs vocabs.hierarchy words xml.data xml.syntax xml.traversal
11 FROM: io.encodings.ascii => ascii ;
12 FROM: ascii => ascii? ;
15 : escape-char ( ch -- )
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="/">Home</a> |
80 <a href=<->>Glossary</a> |
81 <form method="get" action="/search" style="display:inline;">
82 <input name="search" type="text"/>
83 <button type="submit">Search</button>
85 <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
89 : bijective-base26 ( n -- name )
90 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
92 : css-class ( style classes -- name )
93 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
95 : css-classes ( classes -- stylesheet )
97 [ " { " "}" surround ] [ "." prepend ] bi* prepend
98 ] { } assoc>map "\n" join ;
100 :: css-styles-to-classes ( body -- stylesheet body )
101 H{ } clone :> classes
104 seq>> [ tag? ] filter
105 "span" "div" [ deep-tags-named ] bi-curry@ bi append
111 attrs>> [ V{ } like ] change-alist
112 "style" over delete-at* drop classes css-class
117 ] each classes sort-values css-classes body ;
119 : help>html ( topic -- xml )
120 [ article-title " - Factor Documentation" append ]
122 [ print-topic ] with-html-writer css-styles-to-classes
123 [ help-stylesheet ] [ help-navbar prepend ] bi*
126 : generate-help-file ( topic -- )
127 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
129 : all-vocabs-really ( -- seq )
130 all-disk-vocabs-recursive no-roots remove-redundant-prefixes
131 [ vocab-name "scratchpad" = ] reject ;
133 : all-topics ( -- topics )
135 articles get keys [ >link ] map %
136 all-words [ >link ] map %
137 all-authors [ <vocab-author> ] map %
138 all-tags [ <vocab-tag> ] map %
142 : serialize-index ( index file -- )
144 [ [ topic>filename ] dip ] { } assoc-map-as serialize
147 : generate-article-index ( -- )
148 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
149 "articles.idx" serialize-index ;
151 : generate-word-index ( -- )
152 all-words [ dup name>> ] { } map>assoc
153 "words.idx" serialize-index ;
155 : generate-vocab-index ( -- )
156 all-vocabs-really [ dup vocab-name ] { } map>assoc
157 "vocabs.idx" serialize-index ;
159 : generate-indices ( -- )
160 generate-article-index
162 generate-vocab-index ;
164 : generate-help-files ( -- )
166 { recent-searches f }
168 { recent-articles f }
171 all-topics [ '[ _ generate-help-file ] try ] each
174 : generate-help ( -- )
184 MEMO: load-index ( name -- index )
185 binary file-contents bytes>object ;
187 : offline-apropos ( string index -- results )
188 load-index completions ;
190 : article-apropos ( string -- results )
191 "articles.idx" offline-apropos ;
193 : word-apropos ( string -- results )
194 "words.idx" offline-apropos ;
196 : vocab-apropos ( string -- results )
197 "vocabs.idx" offline-apropos ;