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 formatting fry help help.home help.topics help.vocabs
5 html 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 regexp sequences
8 sequences.deep serialize sorting splitting tools.completion
9 vocabs vocabs.hierarchy words xml.data xml.syntax xml.writer ;
10 FROM: io.encodings.ascii => ascii ;
11 FROM: ascii => ascii? ;
14 : escape-char ( ch -- )
17 { CHAR: \" "__quo__" }
18 { CHAR: * "__star__" }
19 { CHAR: : "__colon__" }
23 { CHAR: \\ "__back__" }
24 { CHAR: | "__pipe__" }
25 { CHAR: / "__slash__" }
26 { CHAR: , "__comma__" }
28 { CHAR: # "__hash__" }
29 { CHAR: % "__percent__" }
31 ] [ number>string "__" "__" surround % ] if ;
33 : escape-filename ( string -- filename )
34 [ [ escape-char ] each ] "" make ;
36 GENERIC: topic>filename* ( topic -- name prefix )
38 M: word topic>filename*
40 [ name>> ] [ vocabulary>> ] bi 2array "word"
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* ;
50 : topic>filename ( topic -- filename )
55 [ [ escape-filename ] map "," join ]
61 M: topic url-of topic>filename ;
64 string>> "resource:" ?head [
65 "https://github.com/factor/factor/blob/master/"
69 : help-stylesheet ( stylesheet -- xml )
70 "vocab:help/html/stylesheet.css" ascii file-contents
71 swap "\n" glue [XML <style><-></style> XML] ;
73 : help-navbar ( -- xml )
74 "conventions" >link topic>filename
77 <a href="http://factorcode.org">factorcode.org</a>
78 <a href="/">Handbook</a>
79 <a href=<->>Glossary</a>
80 <form method="get" action="/search" style="display:inline;">
81 <input placeholder="Search" name="search" type="text"/>
82 <input type="submit" value="Go"/>
87 : bijective-base26 ( n -- name )
88 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
90 : css-class ( style classes -- name )
91 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
93 : css-style ( style -- style' )
94 R/ font-size: \d+pt;/ [
95 "font-size: " ?head drop "pt;" ?tail drop
96 string>number 12 / "font-size: %frem;" sprintf
99 : css-classes ( classes -- stylesheet )
101 [ css-style " { " "}" surround ] [ "." prepend ] bi* prepend
102 ] { } assoc>map "\n" join ;
104 :: css-styles-to-classes ( body -- stylesheet body )
105 H{ } clone :> classes
114 attrs>> [ V{ } like ] change-alist
115 "style" over delete-at* drop classes css-class
120 ] each classes sort-values css-classes body ;
122 : help>html ( topic -- xml )
123 [ article-title " - Factor Documentation" append ]
125 [ print-topic ] with-html-writer css-styles-to-classes
126 [ help-stylesheet help-navbar ] dip
127 [XML <-><div class="page"><-></div> XML]
130 : generate-help-file ( topic -- )
131 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
133 : all-vocabs-really ( -- seq )
134 all-disk-vocabs-recursive filter-vocabs
135 [ vocab-name "scratchpad" = ] reject ;
137 : all-topics ( -- topics )
139 articles get keys [ >link ] map %
140 all-words [ >link ] map %
141 all-authors [ <vocab-author> ] map %
142 all-tags [ <vocab-tag> ] map %
146 : serialize-index ( index file -- )
148 [ [ topic>filename ] dip ] { } assoc-map-as serialize
151 : generate-article-index ( -- )
152 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
153 "articles.idx" serialize-index ;
155 : generate-word-index ( -- )
156 all-words [ dup name>> ] { } map>assoc
157 "words.idx" serialize-index ;
159 : generate-vocab-index ( -- )
160 all-vocabs-really [ dup vocab-name ] { } map>assoc
161 "vocabs.idx" serialize-index ;
163 : generate-indices ( -- )
164 generate-article-index
166 generate-vocab-index ;
168 : generate-help-files ( -- )
170 { recent-searches f }
172 { recent-articles f }
175 all-topics [ '[ _ generate-help-file ] try ] each
178 : generate-help ( -- )
188 MEMO: load-index ( name -- index )
189 binary file-contents bytes>object ;
191 : offline-apropos ( string index -- results )
192 load-index completions ;
194 : article-apropos ( string -- results )
195 "articles.idx" offline-apropos ;
197 : word-apropos ( string -- results )
198 "words.idx" offline-apropos ;
200 : vocab-apropos ( string -- results )
201 "vocabs.idx" offline-apropos ;