| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | ! Copyright (C) 2007 Daniel Ehrenberg | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: xml kernel sequences xml.utilities combinators.lib | 
					
						
							| 
									
										
										
										
											2007-12-10 03:01:39 -05:00
										 |  |  | math xml.data arrays assocs xml.generator xml.writer namespaces | 
					
						
							| 
									
										
										
										
											2008-09-10 23:11:40 -04:00
										 |  |  | make math.parser io accessors ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | IN: faq | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : find-after ( seq quot -- elem after )
 | 
					
						
							|  |  |  |     over >r find r> rot 1+ tail ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-12-19 12:33:34 -05:00
										 |  |  | : tag-named*? ( tag name -- ? )
 | 
					
						
							| 
									
										
										
										
											2007-12-18 00:43:13 -05:00
										 |  |  |     assure-name swap tag-named? ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | ! Questions | 
					
						
							|  |  |  | TUPLE: q/a question answer ;
 | 
					
						
							|  |  |  | C: <q/a> q/a | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : li>q/a ( li -- q/a )
 | 
					
						
							| 
									
										
										
										
											2008-04-26 00:17:08 -04:00
										 |  |  |     [ "br" tag-named*? not ] filter
 | 
					
						
							| 
									
										
										
										
											2007-12-19 12:33:34 -05:00
										 |  |  |     [ "strong" tag-named*? ] find-after | 
					
						
							| 
									
										
										
										
											2008-09-01 19:44:07 -04:00
										 |  |  |     >r children>> r> <q/a> ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : q/a>li ( q/a -- li )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |     [ question>> "strong" build-tag* f "br" build-tag* 2array ] keep
 | 
					
						
							|  |  |  |     answer>> append "li" build-tag* ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : xml>q/a ( xml -- q/a )
 | 
					
						
							| 
									
										
										
										
											2008-09-01 19:44:07 -04:00
										 |  |  |     [ "question" tag-named children>> ] keep
 | 
					
						
							|  |  |  |     "answer" tag-named children>> <q/a> ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : q/a>xml ( q/a -- xml )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |     [ question>> "question" build-tag* ] keep
 | 
					
						
							|  |  |  |     answer>> "answer" build-tag* | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  |     "\n" swap 3array "qa" build-tag* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Lists of questions | 
					
						
							|  |  |  | TUPLE: question-list title seq ;
 | 
					
						
							|  |  |  | C: <question-list> question-list | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : xml>question-list ( list -- question-list )
 | 
					
						
							|  |  |  |     [ "title" swap at ] keep
 | 
					
						
							| 
									
										
										
										
											2008-09-01 19:44:07 -04:00
										 |  |  |     children>> [ tag? ] filter [ xml>q/a ] map
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  |     <question-list> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : question-list>xml ( question-list -- list )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |     [ seq>> [ q/a>xml "\n" swap 2array ] | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  |       map concat "list" build-tag* ] keep
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |     title>> [ "title" pick set-at ] when* ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : html>question-list ( h3 ol -- question-list )
 | 
					
						
							|  |  |  |     >r [ children>string ] [ f ] if* r> | 
					
						
							|  |  |  |     children-tags [ li>q/a ] map <question-list> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : question-list>h3 ( id question-list -- h3 )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |     title>> [ | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  |         "h3" build-tag | 
					
						
							|  |  |  |         swap number>string "id" pick set-at
 | 
					
						
							|  |  |  |     ] [ drop f ] if* ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : question-list>html ( question-list start id -- h3/f ol )
 | 
					
						
							|  |  |  |     -rot >r [ question-list>h3 ] keep
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |     seq>> [ q/a>li ] map "ol" build-tag* r> | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  |     number>string "start" pick set-at
 | 
					
						
							|  |  |  |     "margin-left: 5em" "style" pick set-at ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! Overall everything | 
					
						
							|  |  |  | TUPLE: faq header lists ;
 | 
					
						
							|  |  |  | C: <faq> faq | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : html>faq ( div -- faq )
 | 
					
						
							| 
									
										
										
										
											2008-01-09 17:36:30 -05:00
										 |  |  |     unclip swap { "h3" "ol" } [ tags-named ] with map
 | 
					
						
							| 
									
										
										
										
											2008-03-31 20:18:05 -04:00
										 |  |  |     first2 >r f prefix r> [ html>question-list ] 2map <faq> ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : header, ( faq -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |     dup header>> , | 
					
						
							|  |  |  |     lists>> first 1 -1 question-list>html nip , ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : br, ( -- )
 | 
					
						
							|  |  |  |     "br" contained, nl, ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : toc-link, ( question-list number -- )
 | 
					
						
							| 
									
										
										
										
											2008-03-19 20:15:32 -04:00
										 |  |  |     number>string "#" prepend "href" swap 2array 1array
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |     "a" swap [ title>> , ] tag*, br, ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : toc, ( faq -- )
 | 
					
						
							|  |  |  |     "div" { { "style" "background-color: #eee; margin-left: 30%; margin-right: 30%; width: auto; padding: 5px; margin-top: 1em; margin-bottom: 1em" } } [ | 
					
						
							|  |  |  |         "strong" [ "The big questions" , ] tag, br, | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |         lists>> rest dup length [ toc-link, ] 2each
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  |     ] tag*, ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : faq-sections, ( question-lists -- )
 | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |     unclip seq>> length 1+ dupd
 | 
					
						
							|  |  |  |     [ seq>> length + ] accumulate nip
 | 
					
						
							| 
									
										
										
										
											2008-03-29 21:36:58 -04:00
										 |  |  |     0 -rot [ pick question-list>html [ , nl, ] bi@ 1+ ] 2each drop ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : faq>html ( faq -- div )
 | 
					
						
							|  |  |  |     "div" [ | 
					
						
							|  |  |  |         dup header, | 
					
						
							|  |  |  |         dup toc, | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |         lists>> faq-sections, | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  |     ] make-xml ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : xml>faq ( xml -- faq )
 | 
					
						
							|  |  |  |     [ "header" tag-named children>string ] keep
 | 
					
						
							|  |  |  |     "list" tags-named [ xml>question-list ] map <faq> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : faq>xml ( faq -- xml )
 | 
					
						
							|  |  |  |     "faq" [ | 
					
						
							| 
									
										
										
										
											2008-08-31 03:52:02 -04:00
										 |  |  |         "header" [ dup header>> , ] tag, | 
					
						
							|  |  |  |         lists>> [ question-list>xml , nl, ] each
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:35:04 -05:00
										 |  |  |     ] make-xml ;
 | 
					
						
							| 
									
										
										
										
											2007-12-10 00:37:32 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | : read-write-faq ( xml-stream -- )
 | 
					
						
							| 
									
										
										
										
											2007-12-10 03:05:15 -05:00
										 |  |  |     read-xml xml>faq faq>html write-xml ;
 |