new accessors

db4
Doug Coleman 2008-08-31 02:52:02 -05:00
parent b07cb1e803
commit fbc1076ac2
1 changed files with 18 additions and 18 deletions

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: xml kernel sequences xml.utilities combinators.lib USING: xml kernel sequences xml.utilities combinators.lib
math xml.data arrays assocs xml.generator xml.writer namespaces math xml.data arrays assocs xml.generator xml.writer namespaces
math.parser io ; math.parser io accessors ;
IN: faq IN: faq
: find-after ( seq quot -- elem after ) : find-after ( seq quot -- elem after )
@ -21,16 +21,16 @@ C: <q/a> q/a
>r tag-children r> <q/a> ; >r tag-children r> <q/a> ;
: q/a>li ( q/a -- li ) : q/a>li ( q/a -- li )
[ q/a-question "strong" build-tag* f "br" build-tag* 2array ] keep [ question>> "strong" build-tag* f "br" build-tag* 2array ] keep
q/a-answer append "li" build-tag* ; answer>> append "li" build-tag* ;
: xml>q/a ( xml -- q/a ) : xml>q/a ( xml -- q/a )
[ "question" tag-named tag-children ] keep [ "question" tag-named tag-children ] keep
"answer" tag-named tag-children <q/a> ; "answer" tag-named tag-children <q/a> ;
: q/a>xml ( q/a -- xml ) : q/a>xml ( q/a -- xml )
[ q/a-question "question" build-tag* ] keep [ question>> "question" build-tag* ] keep
q/a-answer "answer" build-tag* answer>> "answer" build-tag*
"\n" swap 3array "qa" build-tag* ; "\n" swap 3array "qa" build-tag* ;
! Lists of questions ! Lists of questions
@ -43,23 +43,23 @@ C: <question-list> question-list
<question-list> ; <question-list> ;
: question-list>xml ( question-list -- list ) : question-list>xml ( question-list -- list )
[ question-list-seq [ q/a>xml "\n" swap 2array ] [ seq>> [ q/a>xml "\n" swap 2array ]
map concat "list" build-tag* ] keep map concat "list" build-tag* ] keep
question-list-title [ "title" pick set-at ] when* ; title>> [ "title" pick set-at ] when* ;
: html>question-list ( h3 ol -- question-list ) : html>question-list ( h3 ol -- question-list )
>r [ children>string ] [ f ] if* r> >r [ children>string ] [ f ] if* r>
children-tags [ li>q/a ] map <question-list> ; children-tags [ li>q/a ] map <question-list> ;
: question-list>h3 ( id question-list -- h3 ) : question-list>h3 ( id question-list -- h3 )
question-list-title [ title>> [
"h3" build-tag "h3" build-tag
swap number>string "id" pick set-at swap number>string "id" pick set-at
] [ drop f ] if* ; ] [ drop f ] if* ;
: question-list>html ( question-list start id -- h3/f ol ) : question-list>html ( question-list start id -- h3/f ol )
-rot >r [ question-list>h3 ] keep -rot >r [ question-list>h3 ] keep
question-list-seq [ q/a>li ] map "ol" build-tag* r> seq>> [ q/a>li ] map "ol" build-tag* r>
number>string "start" pick set-at number>string "start" pick set-at
"margin-left: 5em" "style" pick set-at ; "margin-left: 5em" "style" pick set-at ;
@ -72,32 +72,32 @@ C: <faq> faq
first2 >r f prefix r> [ html>question-list ] 2map <faq> ; first2 >r f prefix r> [ html>question-list ] 2map <faq> ;
: header, ( faq -- ) : header, ( faq -- )
dup faq-header , dup header>> ,
faq-lists first 1 -1 question-list>html nip , ; lists>> first 1 -1 question-list>html nip , ;
: br, ( -- ) : br, ( -- )
"br" contained, nl, ; "br" contained, nl, ;
: toc-link, ( question-list number -- ) : toc-link, ( question-list number -- )
number>string "#" prepend "href" swap 2array 1array number>string "#" prepend "href" swap 2array 1array
"a" swap [ question-list-title , ] tag*, br, ; "a" swap [ title>> , ] tag*, br, ;
: toc, ( faq -- ) : toc, ( faq -- )
"div" { { "style" "background-color: #eee; margin-left: 30%; margin-right: 30%; width: auto; padding: 5px; margin-top: 1em; margin-bottom: 1em" } } [ "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, "strong" [ "The big questions" , ] tag, br,
faq-lists rest dup length [ toc-link, ] 2each lists>> rest dup length [ toc-link, ] 2each
] tag*, ; ] tag*, ;
: faq-sections, ( question-lists -- ) : faq-sections, ( question-lists -- )
unclip question-list-seq length 1+ dupd unclip seq>> length 1+ dupd
[ question-list-seq length + ] accumulate nip [ seq>> length + ] accumulate nip
0 -rot [ pick question-list>html [ , nl, ] bi@ 1+ ] 2each drop ; 0 -rot [ pick question-list>html [ , nl, ] bi@ 1+ ] 2each drop ;
: faq>html ( faq -- div ) : faq>html ( faq -- div )
"div" [ "div" [
dup header, dup header,
dup toc, dup toc,
faq-lists faq-sections, lists>> faq-sections,
] make-xml ; ] make-xml ;
: xml>faq ( xml -- faq ) : xml>faq ( xml -- faq )
@ -106,8 +106,8 @@ C: <faq> faq
: faq>xml ( faq -- xml ) : faq>xml ( faq -- xml )
"faq" [ "faq" [
"header" [ dup faq-header , ] tag, "header" [ dup header>> , ] tag,
faq-lists [ question-list>xml , nl, ] each lists>> [ question-list>xml , nl, ] each
] make-xml ; ] make-xml ;
: read-write-faq ( xml-stream -- ) : read-write-faq ( xml-stream -- )