| 
									
										
										
										
											2009-01-31 21:44:30 -05:00
										 |  |  | ! Copyright (C) 2008, 2009 Slava Pestov. | 
					
						
							| 
									
										
										
										
											2008-06-06 19:57:37 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | USING: io.encodings.utf8 io.encodings.ascii io.encodings.binary | 
					
						
							| 
									
										
										
										
											2009-01-30 20:28:16 -05:00
										 |  |  | io.files io.files.temp io.directories html.streams help kernel | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | assocs sequences make words accessors arrays help.topics vocabs | 
					
						
							| 
									
										
										
										
											2009-03-24 05:11:08 -04:00
										 |  |  | tools.vocabs help.vocabs namespaces prettyprint io | 
					
						
							| 
									
										
										
										
											2009-04-14 20:24:32 -04:00
										 |  |  | vocabs.loader serialize fry memoize ascii unicode.case math.order | 
					
						
							| 
									
										
										
										
											2009-04-14 20:57:25 -04:00
										 |  |  | sorting debugger html xml.syntax xml.writer math.parser ;
 | 
					
						
							| 
									
										
										
										
											2008-06-06 19:57:37 -04:00
										 |  |  | IN: help.html | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | : escape-char ( ch -- )
 | 
					
						
							| 
									
										
										
										
											2009-04-14 20:24:32 -04:00
										 |  |  |     dup ascii? [ | 
					
						
							|  |  |  |         dup H{ | 
					
						
							|  |  |  |             { CHAR: " "__quo__" } | 
					
						
							|  |  |  |             { CHAR: * "__star__" } | 
					
						
							|  |  |  |             { CHAR: : "__colon__" } | 
					
						
							|  |  |  |             { CHAR: < "__lt__" } | 
					
						
							|  |  |  |             { CHAR: > "__gt__" } | 
					
						
							|  |  |  |             { CHAR: ? "__que__" } | 
					
						
							|  |  |  |             { CHAR: \\ "__back__" } | 
					
						
							|  |  |  |             { CHAR: | "__pipe__" } | 
					
						
							|  |  |  |             { CHAR: / "__slash__" } | 
					
						
							|  |  |  |             { CHAR: , "__comma__" } | 
					
						
							|  |  |  |             { CHAR: @ "__at__" } | 
					
						
							|  |  |  |         } at [ % ] [ , ] ?if
 | 
					
						
							| 
									
										
										
										
											2009-04-14 20:57:25 -04:00
										 |  |  |     ] [ number>string "__" "__" surround % ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-06-06 19:57:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | : escape-filename ( string -- filename )
 | 
					
						
							|  |  |  |     [ [ escape-char ] each ] "" make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: topic>filename* ( topic -- name prefix )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-29 20:43:59 -04:00
										 |  |  | M: word topic>filename* | 
					
						
							|  |  |  |     dup vocabulary>> [ | 
					
						
							|  |  |  |         [ name>> ] [ vocabulary>> ] bi 2array "word" | 
					
						
							|  |  |  |     ] [ drop f f ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: link topic>filename* name>> dup [ "article" ] [ topic>filename* ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | M: word-link topic>filename* name>> topic>filename* ;
 | 
					
						
							|  |  |  | M: vocab-spec topic>filename* vocab-name "vocab" ;
 | 
					
						
							|  |  |  | M: vocab-tag topic>filename* name>> "tag" ;
 | 
					
						
							|  |  |  | M: vocab-author topic>filename* name>> "author" ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 20:43:59 -04:00
										 |  |  | M: f topic>filename* drop \ f topic>filename* ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : topic>filename ( topic -- filename )
 | 
					
						
							| 
									
										
										
										
											2008-09-29 20:43:59 -04:00
										 |  |  |     topic>filename* dup [ | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             % "-" % | 
					
						
							|  |  |  |             dup array?
 | 
					
						
							|  |  |  |             [ [ escape-filename ] map "," join ] | 
					
						
							|  |  |  |             [ escape-filename ] | 
					
						
							|  |  |  |             if % ".html" % | 
					
						
							|  |  |  |         ] "" make | 
					
						
							|  |  |  |     ] [ 2drop f ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-31 21:44:30 -05:00
										 |  |  | M: topic url-of topic>filename ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-31 21:44:30 -05:00
										 |  |  | : help-stylesheet ( -- string )
 | 
					
						
							| 
									
										
										
										
											2009-02-15 20:53:21 -05:00
										 |  |  |     "vocab:help/html/stylesheet.css" ascii file-contents | 
					
						
							| 
									
										
										
										
											2009-01-31 21:44:30 -05:00
										 |  |  |     [XML <style><-></style> XML] ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-31 21:44:30 -05:00
										 |  |  | : help>html ( topic -- xml )
 | 
					
						
							|  |  |  |     [ article-title ] | 
					
						
							|  |  |  |     [ drop help-stylesheet ] | 
					
						
							| 
									
										
										
										
											2009-04-02 14:48:53 -04:00
										 |  |  |     [ [ print-topic ] with-html-writer ] | 
					
						
							| 
									
										
										
										
											2009-01-31 21:44:30 -05:00
										 |  |  |     tri simple-page ;
 | 
					
						
							|  |  |  |            | 
					
						
							|  |  |  | : generate-help-file ( topic -- )
 | 
					
						
							|  |  |  |     dup topic>filename utf8 [ help>html write-xml ] with-file-writer ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : all-vocabs-really ( -- seq )
 | 
					
						
							|  |  |  |     #! Hack. | 
					
						
							|  |  |  |     all-vocabs values concat
 | 
					
						
							|  |  |  |     vocabs [ find-vocab-root not ] filter [ vocab ] map append ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : all-topics ( -- topics )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2008-09-29 05:23:42 -04:00
										 |  |  |         articles get keys [ >link ] map % | 
					
						
							|  |  |  |         all-words [ >link ] map % | 
					
						
							|  |  |  |         all-authors [ <vocab-author> ] map % | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  |         all-tags [ <vocab-tag> ] map % | 
					
						
							| 
									
										
										
										
											2008-09-29 05:23:42 -04:00
										 |  |  |         all-vocabs-really % | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  |     ] { } make ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : serialize-index ( index file -- )
 | 
					
						
							|  |  |  |     [ [ [ topic>filename ] dip ] { } assoc-map-as object>bytes ] dip
 | 
					
						
							|  |  |  |     binary set-file-contents ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : generate-indices ( -- )
 | 
					
						
							|  |  |  |     articles get keys [ [ >link ] [ article-title ] bi ] { } map>assoc "articles.idx" serialize-index | 
					
						
							|  |  |  |     all-words [ dup name>> ] { } map>assoc "words.idx" serialize-index | 
					
						
							|  |  |  |     all-vocabs-really [ dup vocab-name ] { } map>assoc "vocabs.idx" serialize-index ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:23:42 -04:00
										 |  |  | : generate-help-files ( -- )
 | 
					
						
							| 
									
										
										
										
											2009-01-31 21:44:30 -05:00
										 |  |  |     all-topics [ '[ _ generate-help-file ] try ] each ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:23:42 -04:00
										 |  |  | : generate-help ( -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-16 12:29:35 -05:00
										 |  |  |     "docs" temp-file | 
					
						
							|  |  |  |     [ make-directories ] | 
					
						
							|  |  |  |     [ | 
					
						
							|  |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-09-29 05:23:42 -04:00
										 |  |  |             generate-indices | 
					
						
							|  |  |  |             generate-help-files | 
					
						
							|  |  |  |         ] with-directory | 
					
						
							| 
									
										
										
										
											2008-11-16 12:29:35 -05:00
										 |  |  |     ] bi ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:23:42 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | MEMO: load-index ( name -- index )
 | 
					
						
							|  |  |  |     binary file-contents bytes>object ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: result title href ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : offline-apropos ( string index -- results )
 | 
					
						
							|  |  |  |     load-index swap >lower | 
					
						
							|  |  |  |     '[ [ drop _ ] dip >lower subseq? ] assoc-filter
 | 
					
						
							| 
									
										
										
										
											2008-09-29 06:08:16 -04:00
										 |  |  |     [ swap result boa ] { } assoc>map
 | 
					
						
							|  |  |  |     [ [ title>> ] compare ] sort ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : article-apropos ( string -- results )
 | 
					
						
							| 
									
										
										
										
											2008-11-17 18:30:47 -05:00
										 |  |  |     "articles.idx" offline-apropos ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : word-apropos ( string -- results )
 | 
					
						
							| 
									
										
										
										
											2008-11-17 18:30:47 -05:00
										 |  |  |     "words.idx" offline-apropos ;
 | 
					
						
							| 
									
										
										
										
											2008-09-29 05:10:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : vocab-apropos ( string -- results )
 | 
					
						
							| 
									
										
										
										
											2008-11-17 18:30:47 -05:00
										 |  |  |     "vocabs.idx" offline-apropos ;
 |