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 fonts formatting fry help help.home help.topics
5 help.vocabs 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 default-font-size /f
97 "font-size: %.3frem;" sprintf
100 R/ padding: \d+px;/ [
101 "padding: " ?head drop "px;" ?tail drop
102 string>number dup even? [ 1 + ] when 2 * number>string
103 "padding: " "px;" surround
106 : css-classes ( classes -- stylesheet )
108 [ css-style " { " "}" surround ] [ "." prepend ] bi* prepend
109 ] { } assoc>map "\n" join ;
111 :: css-styles-to-classes ( body -- stylesheet body )
112 H{ } clone :> classes
121 [ clone [ V{ } like ] change-alist ] change-attrs
122 "style" over delete-at* drop classes css-class
127 ] each classes sort-values css-classes body ;
129 : help>html ( topic -- xml )
130 [ article-title " - Factor Documentation" append ]
132 [ print-topic ] with-html-writer css-styles-to-classes
133 [ help-stylesheet help-navbar ] dip
134 [XML <-><div class="page"><-></div> XML]
137 : generate-help-file ( topic -- )
138 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
140 : all-vocabs-really ( -- seq )
141 all-disk-vocabs-recursive filter-vocabs
142 [ vocab-name "scratchpad" = ] reject ;
144 : all-topics ( -- topics )
146 articles get keys [ >link ] map %
147 all-words [ >link ] map %
148 all-authors [ <vocab-author> ] map %
149 all-tags [ <vocab-tag> ] map %
153 : serialize-index ( index file -- )
155 [ [ topic>filename ] dip ] { } assoc-map-as serialize
158 : generate-article-index ( -- )
159 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
160 "articles.idx" serialize-index ;
162 : generate-word-index ( -- )
163 all-words [ dup name>> ] { } map>assoc
164 "words.idx" serialize-index ;
166 : generate-vocab-index ( -- )
167 all-vocabs-really [ dup vocab-name ] { } map>assoc
168 "vocabs.idx" serialize-index ;
170 : generate-indices ( -- )
171 generate-article-index
173 generate-vocab-index ;
175 : generate-help-files ( -- )
177 { recent-searches f }
179 { recent-articles f }
182 all-topics [ '[ _ generate-help-file ] try ] each
185 : generate-help ( -- )
195 MEMO: load-index ( name -- index )
196 binary file-contents bytes>object ;
198 : offline-apropos ( string index -- results )
199 load-index completions ;
201 : article-apropos ( string -- results )
202 "articles.idx" offline-apropos ;
204 : word-apropos ( string -- results )
205 "words.idx" offline-apropos ;
207 : vocab-apropos ( string -- results )
208 "vocabs.idx" offline-apropos ;