1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs debugger fry hashtables help
4 help.home help.topics help.vocabs html html.streams
5 io.directories io.encodings.binary io.encodings.utf8 io.files
6 io.files.temp io.pathnames kernel make math.parser memoize
7 namespaces sequences serialize sorting splitting unicode.case
8 vocabs vocabs.hierarchy words xml.syntax xml.writer ;
9 FROM: io.encodings.ascii => ascii ;
10 FROM: ascii => ascii? ;
13 : escape-char ( ch -- )
17 { CHAR: * "__star__" }
18 { CHAR: : "__colon__" }
22 { CHAR: \\ "__back__" }
23 { CHAR: | "__pipe__" }
24 { CHAR: / "__slash__" }
25 { CHAR: , "__comma__" }
27 { CHAR: # "__hash__" }
28 { CHAR: % "__percent__" }
30 ] [ number>string "__" "__" surround % ] if ;
32 : escape-filename ( string -- filename )
33 [ [ escape-char ] each ] "" make ;
35 GENERIC: topic>filename* ( topic -- name prefix )
37 M: word topic>filename*
39 [ name>> ] [ vocabulary>> ] bi 2array "word"
42 M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
43 M: word-link topic>filename* name>> topic>filename* ;
44 M: vocab-spec topic>filename* vocab-name "vocab" ;
45 M: vocab-tag topic>filename* name>> "tag" ;
46 M: vocab-author topic>filename* name>> "author" ;
47 M: f topic>filename* drop \ f topic>filename* ;
49 : topic>filename ( topic -- filename )
54 [ [ escape-filename ] map "," join ]
60 M: topic url-of topic>filename ;
63 string>> "resource:" ?head [
64 "https://github.com/slavapestov/factor/blob/master/"
68 : help-stylesheet ( -- xml )
69 "vocab:help/html/stylesheet.css" ascii file-contents
70 [XML <style><-></style> XML] ;
72 : help-navbar ( -- xml )
73 "conventions" >link topic>filename
76 <b> Factor Documentation </b> |
77 <a href="/">Home</a> |
78 <a href=<->>Glossary</a> |
79 <form method="get" action="/search" style="display:inline;">
80 <input name="search" type="text"/>
81 <button type="submit">Search</button>
83 <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
87 : help>html ( topic -- xml )
88 [ article-title " - Factor Documentation" append ]
89 [ drop help-stylesheet ]
92 [ [ print-topic ] with-html-writer ]
97 : generate-help-file ( topic -- )
98 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
100 : all-vocabs-really ( -- seq )
101 all-vocabs-recursive >hashtable no-roots remove-redundant-prefixes
102 [ vocab-name "scratchpad" = not ] filter ;
104 : all-topics ( -- topics )
106 articles get keys [ >link ] map %
107 all-words [ >link ] map %
108 all-authors [ <vocab-author> ] map %
109 all-tags [ <vocab-tag> ] map %
113 : serialize-index ( index file -- )
114 [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
115 binary set-file-contents ;
117 : generate-indices ( -- )
118 articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
119 all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
120 all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
122 : (generate-help-files) ( -- )
123 all-topics [ '[ _ generate-help-file ] try ] each ;
125 : generate-help-files ( -- )
127 { recent-searches f }
129 { recent-articles f }
131 } [ (generate-help-files) ] with-variables ;
133 : generate-help ( -- )
143 MEMO: load-index ( name -- index )
144 binary file-contents bytes>object ;
146 TUPLE: result title href ;
148 : partition-exact ( string results -- results' )
149 [ title>> = ] with partition append ;
151 : offline-apropos ( string index -- results )
152 load-index over >lower
153 '[ [ drop _ ] dip >lower subseq? ] assoc-filter
154 [ swap result boa ] { } assoc>map
155 [ title>> ] sort-with
158 : article-apropos ( string -- results )
159 "articles.idx" offline-apropos ;
161 : word-apropos ( string -- results )
162 "words.idx" offline-apropos ;
164 : vocab-apropos ( string -- results )
165 "vocabs.idx" offline-apropos ;