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 system 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 -- )
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-meta ( -- xml )
77 content="width=device-width, initial-scale=1"
81 : help-navbar ( -- xml )
82 "conventions" >link topic>filename
85 <a href="/">Handbook</a>
86 <a href=<->>Glossary</a>
87 <form method="get" action="/search" style="float: right;">
88 <input placeholder="Search" name="search" type="text"/>
89 <input type="submit" value="Go"/>
90 <a href="https://factorcode.org">factorcode.org</a>
95 : help-footer ( -- xml )
96 version-info "\n" split1 drop
99 <p style="color: #666; font-size: smaller;">
100 This documentation was generated offline from a
101 <code>load-all</code> image. If you want, you can also
102 browse the documentation from within the <a
103 href="article-ui-tools.html">UI developer tools</a>. See
104 the <a href="https://factorcode.org">Factor website</a>
105 for more information.
107 <p style="color: #666; font-size: smaller;"><-></p>
110 : bijective-base26 ( n -- name )
111 [ dup 0 > ] [ 1 - 26 /mod CHAR: a + ] "" produce-as nip reverse! ;
113 : css-class ( style classes -- name )
114 dup '[ drop _ assoc-size 1 + bijective-base26 ] cache ;
116 : fix-css-style ( style -- style' )
117 R/ font-size: \d+pt;/ [
118 "font-size: " ?head drop "pt;" ?tail drop
120 "font-size: %dpt;" sprintf
123 R/ padding: \d+px;/ [
124 "padding: " ?head drop "px;" ?tail drop
125 string>number dup even? [ 2 * 1 + ] [ 2 * ] if
126 number>string "padding: " "px;" surround
133 R/ font-family: monospace;/ [
134 " white-space: pre-wrap; line-height: 125%;" append
137 : fix-help-header ( classes -- classes )
139 [ ".a" head? ] [ "#f4efd9;" swap subseq? ] bi and
141 "padding: 10px;" "padding: 15px;" replace
143 " border-bottom: 1px solid #ccc; width: calc(100% + 30px); margin: -15px; }"
144 append swap pick set-nth
145 ".a tr:hover { background-color: #f4efd9; }" prefix
148 : css-classes ( classes -- stylesheet )
150 [ fix-css-style " { " "}" surround ] [ "." prepend ] bi* prepend
151 ] { } assoc>map fix-help-header join-lines ;
153 :: css-styles-to-classes ( body -- stylesheet body )
154 H{ } clone :> classes
163 [ clone [ V{ } like ] change-alist ] change-attrs
164 "style" over delete-at* drop classes css-class
169 ] each classes sort-values css-classes body ;
171 : retina-image ( path -- path' )
172 "@2x" over subseq? [ "." split1-last "@2x." glue ] unless ;
174 : ?copy-file ( from to -- )
175 dup file-exists? [ 2drop ] [ copy-file ] if ;
177 : cache-images ( body -- body' )
181 T{ name { main "img" } } over tag-named? [
183 retina-image dup file-name
191 : help>html ( topic -- xml )
192 [ article-title " - Factor Documentation" append ]
194 [ print-topic ] with-html-writer
195 css-styles-to-classes cache-images
196 [ help-stylesheet help-meta prepend help-navbar ] dip help-footer
197 [XML <div id="container"><-><div class="page"><-><-></div></div> XML]
200 : generate-help-file ( topic -- )
201 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
203 : all-vocabs-really ( -- seq )
204 all-disk-vocabs-recursive filter-vocabs
205 [ vocab-name "scratchpad" = ] reject ;
207 : all-topics ( -- topics )
209 articles get keys [ >link ] map %
210 all-words [ >link ] map %
211 all-authors [ <vocab-author> ] map %
212 all-tags [ <vocab-tag> ] map %
216 : serialize-index ( index file -- )
218 [ [ topic>filename ] dip ] { } assoc-map-as serialize
221 : generate-article-index ( -- )
222 articles get [ [ >link ] [ article-title ] bi* ] assoc-map
223 "articles.idx" serialize-index ;
225 : generate-word-index ( -- )
226 all-words [ dup name>> ] { } map>assoc
227 "words.idx" serialize-index ;
229 : generate-vocab-index ( -- )
230 all-vocabs-really [ dup vocab-name ] { } map>assoc
231 "vocabs.idx" serialize-index ;
233 : generate-indices ( -- )
234 generate-article-index
236 generate-vocab-index ;
238 : generate-help-files ( -- )
240 { recent-searches f }
242 { recent-articles f }
245 all-topics [ '[ _ generate-help-file ] try ] each
248 : generate-help ( -- )
258 MEMO: load-index ( name -- index )
259 binary file-contents bytes>object ;
261 : offline-apropos ( string index -- results )
262 load-index completions ;
264 : article-apropos ( string -- results )
265 "articles.idx" offline-apropos ;
267 : word-apropos ( string -- results )
268 "words.idx" offline-apropos ;
270 : vocab-apropos ( string -- results )
271 "vocabs.idx" offline-apropos ;