1 ! Copyright (C) 2008, 2011 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: io.encodings.utf8 io.encodings.binary io.files
4 io.files.temp io.directories html.streams help help.home kernel
5 assocs sequences make words accessors arrays help.topics vocabs
6 vocabs.hierarchy help.vocabs namespaces prettyprint io
7 vocabs.loader serialize fry memoize unicode.case math.order
8 sorting debugger html xml.syntax xml.writer math.parser
10 FROM: io.encodings.ascii => ascii ;
11 FROM: ascii => ascii? ;
14 : escape-char ( ch -- )
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/slavapestov/factor/blob/master/"
69 : help-stylesheet ( -- xml )
70 "vocab:help/html/stylesheet.css" ascii file-contents
71 [XML <style><-></style> XML] ;
73 : help-navbar ( -- xml )
74 "conventions" >link topic>filename
77 <b> Factor Documentation </b> |
78 <a href="/">Home</a> |
79 <a href=<->>Glossary</a> |
80 <form method="get" action="/search" style="display:inline;">
81 <input name="search" type="text"/>
82 <button type="submit">Search</button>
84 <a href="http://factorcode.org" style="float:right; padding: 4px;">factorcode.org</a>
88 : help>html ( topic -- xml )
89 [ article-title " - Factor Documentation" append ]
90 [ drop help-stylesheet ]
93 [ [ print-topic ] with-html-writer ]
98 : generate-help-file ( topic -- )
99 dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
101 : all-vocabs-really ( -- seq )
102 all-vocabs-recursive >hashtable no-roots remove-redundant-prefixes
103 [ vocab-name "scratchpad" = not ] filter ;
105 : all-topics ( -- topics )
107 articles get keys [ >link ] map %
108 all-words [ >link ] map %
109 all-authors [ <vocab-author> ] map %
110 all-tags [ <vocab-tag> ] map %
114 : serialize-index ( index file -- )
115 [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
116 binary set-file-contents ;
118 : generate-indices ( -- )
119 articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index
120 all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index
121 all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
123 : (generate-help-files) ( -- )
124 all-topics [ '[ _ generate-help-file ] try ] each ;
126 : generate-help-files ( -- )
128 { recent-searches f }
130 { recent-articles f }
132 } [ (generate-help-files) ] with-variables ;
134 : generate-help ( -- )
144 MEMO: load-index ( name -- index )
145 binary file-contents bytes>object ;
147 TUPLE: result title href ;
149 : partition-exact ( string results -- results' )
150 [ title>> = ] with partition append ;
152 : offline-apropos ( string index -- results )
153 load-index over >lower
154 '[ [ drop _ ] dip >lower subseq? ] assoc-filter
155 [ swap result boa ] { } assoc>map
156 [ title>> ] sort-with
159 : article-apropos ( string -- results )
160 "articles.idx" offline-apropos ;
162 : word-apropos ( string -- results )
163 "words.idx" offline-apropos ;
165 : vocab-apropos ( string -- results )
166 "vocabs.idx" offline-apropos ;