factor/basis/help/markup/markup.factor

339 lines
8.2 KiB
Factor
Raw Normal View History

2008-03-07 03:28:45 -05:00
! Copyright (C) 2005, 2008 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays definitions generic io kernel assocs
hashtables namespaces make parser prettyprint sequences strings
io.styles vectors words math sorting splitting classes slots
vocabs help.stylesheet help.topics vocabs.loader alias ;
2007-09-20 18:09:08 -04:00
IN: help.markup
! Simple markup language.
! <element> ::== <string> | <simple-element> | <fancy-element>
! <simple-element> ::== { <element>* }
! <fancy-element> ::== { <type> <element> }
! Element types are words whose name begins with $.
2008-03-26 19:23:19 -04:00
PREDICATE: simple-element < array
2008-09-06 20:13:59 -04:00
[ t ] [ first word? not ] if-empty ;
2007-09-20 18:09:08 -04:00
SYMBOL: last-element
SYMBOL: span
SYMBOL: block
SYMBOL: table
2008-06-08 16:32:55 -04:00
: last-span? ( -- ? ) last-element get span eq? ;
: last-block? ( -- ? ) last-element get block eq? ;
2007-09-20 18:09:08 -04:00
: ($span) ( quot -- )
last-block? [ nl ] when
span last-element set
call ; inline
GENERIC: print-element ( element -- )
M: simple-element print-element [ print-element ] each ;
M: string print-element [ write ] ($span) ;
M: array print-element unclip execute ;
M: word print-element { } swap execute ;
M: f print-element drop ;
: print-element* ( element style -- )
[ print-element ] with-style ;
: with-default-style ( quot -- )
2008-03-07 03:28:45 -05:00
default-span-style get [
2007-09-20 18:09:08 -04:00
last-element off
2008-03-07 03:28:45 -05:00
default-block-style get swap with-nesting
2007-09-20 18:09:08 -04:00
] with-style ; inline
: print-content ( element -- )
[ print-element ] with-default-style ;
: ($block) ( quot -- )
last-element get { f table } member? [ nl ] unless
span last-element set
call
block last-element set ; inline
! Some spans
2008-06-08 16:32:55 -04:00
: $snippet ( children -- )
[ snippet-style get print-element* ] ($span) ;
2007-09-20 18:09:08 -04:00
! for help-lint
ALIAS: $slot $snippet
2008-06-08 16:32:55 -04:00
: $emphasis ( children -- )
[ emphasis-style get print-element* ] ($span) ;
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: $strong ( children -- )
[ strong-style get print-element* ] ($span) ;
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: $url ( children -- )
[
dup first href associate url-style get assoc-union
print-element*
] ($span) ;
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: $nl ( children -- )
nl nl drop ;
2007-09-20 18:09:08 -04:00
! Some blocks
2008-06-08 16:32:55 -04:00
: ($heading) ( children quot -- )
2007-09-20 18:09:08 -04:00
last-element get [ nl ] when ($block) ; inline
: $heading ( element -- )
[ heading-style get print-element* ] ($heading) ;
: $subheading ( element -- )
[ strong-style get print-element* ] ($heading) ;
: ($code-style) ( presentation -- hash )
presented associate code-style get assoc-union ;
2007-09-20 18:09:08 -04:00
: ($code) ( presentation quot -- )
[
snippet-style get [
last-element off
>r ($code-style) r> with-nesting
] with-style
] ($block) ; inline
: $code ( element -- )
"\n" join dup <input> [ write ] ($code) ;
: $syntax ( element -- ) "Syntax" $heading $code ;
: $description ( element -- )
"Word description" $heading print-element ;
: $class-description ( element -- )
"Class description" $heading print-element ;
: $error-description ( element -- )
"Error description" $heading print-element ;
: $var-description ( element -- )
"Variable description" $heading print-element ;
: $contract ( element -- )
"Generic word contract" $heading print-element ;
: $examples ( element -- )
"Examples" $heading print-element ;
: $example ( element -- )
2007-10-12 16:30:36 -04:00
1 cut* swap "\n" join dup <input> [
2007-09-20 18:09:08 -04:00
input-style get format nl print-element
] ($code) ;
: $unchecked-example ( element -- )
#! help-lint ignores these.
$example ;
: $markup-example ( element -- )
first dup unparse " print-element" append 1array $code
print-element ;
: $warning ( element -- )
[
warning-style get [
last-element off
"Warning" $heading print-element
] with-nesting
] ($heading) ;
! Some links
: write-link ( string object -- )
link-style get [ write-object ] with-style ;
: ($link) ( article -- )
2008-08-31 11:09:21 -04:00
[ [ article-name ] [ >link ] bi write-link ] ($span) ;
2007-09-20 18:09:08 -04:00
: $link ( element -- )
first ($link) ;
2008-03-04 23:46:01 -05:00
: ($long-link) ( object -- )
2008-08-31 11:09:21 -04:00
[ article-title ] [ >link ] bi write-link ;
2007-09-20 18:09:08 -04:00
2008-03-04 23:46:01 -05:00
: ($subsection) ( element quot -- )
2007-09-20 18:09:08 -04:00
[
subsection-style get [
bullet get write bl
2008-03-04 23:46:01 -05:00
call
2007-09-20 18:09:08 -04:00
] with-style
2008-03-04 23:46:01 -05:00
] ($block) ; inline
2007-09-20 18:09:08 -04:00
2008-03-04 23:46:01 -05:00
: $subsection ( element -- )
[ first ($long-link) ] ($subsection) ;
2007-09-20 18:09:08 -04:00
2008-03-18 21:27:09 -04:00
: ($vocab-link) ( text vocab -- )
2008-03-19 15:39:08 -04:00
>vocab-link write-link ;
2008-03-04 23:46:01 -05:00
: $vocab-subsection ( element -- )
[
first2 dup vocab-help dup [
2nip ($long-link)
] [
drop ($vocab-link)
] if
] ($subsection) ;
2008-03-13 04:45:34 -04:00
: $vocab-link ( element -- )
first dup vocab-name swap ($vocab-link) ;
2007-09-20 18:09:08 -04:00
: $vocabulary ( element -- )
first vocabulary>> [
2008-03-05 16:59:15 -05:00
"Vocabulary" $heading nl dup ($vocab-link)
2007-09-20 18:09:08 -04:00
] when* ;
: textual-list ( seq quot -- )
[ ", " print-element ] swap interleave ; inline
: $links ( topics -- )
[ [ ($link) ] textual-list ] ($span) ;
: $vocab-links ( vocabs -- )
[ vocab ] map $links ;
2007-09-20 18:09:08 -04:00
: $see-also ( topics -- )
"See also" $heading $links ;
: related-words ( seq -- )
dup [ "related" set-word-prop ] curry each ;
: $related ( element -- )
2008-09-06 20:13:59 -04:00
first dup "related" word-prop remove
[ $see-also ] unless-empty ;
2007-09-20 18:09:08 -04:00
: ($grid) ( style quot -- )
[
table-content-style get [
swap [ last-element off call ] tabular-output
] with-style
] ($block) table last-element set ; inline
: $list ( element -- )
list-style get [
[
[
bullet get write-cell
[ print-element ] with-cell
] with-row
] each
] ($grid) ;
: $table ( element -- )
table-style get [
[
[
[ [ print-element ] with-cell ] each
] with-row
] each
] ($grid) ;
: a/an ( str -- str )
first "aeiou" member? "an" "a" ? ;
GENERIC: ($instance) ( element -- )
M: word ($instance)
dup name>> a/an write bl ($link) ;
2007-09-20 18:09:08 -04:00
M: string ($instance)
dup a/an write bl $snippet ;
2008-06-08 16:32:55 -04:00
: $instance ( children -- ) first ($instance) ;
2007-09-20 18:09:08 -04:00
: values-row ( seq -- seq )
unclip \ $snippet swap ?word-name 2array
swap dup first word? [ \ $instance prefix ] when 2array ;
2007-09-20 18:09:08 -04:00
: $values ( element -- )
"Inputs and outputs" $heading
[ values-row ] map $table ;
: $side-effects ( element -- )
"Side effects" $heading "Modifies " print-element
[ $snippet ] textual-list ;
: $errors ( element -- )
"Errors" $heading print-element ;
: $notes ( element -- )
"Notes" $heading print-element ;
2008-08-23 21:24:54 -04:00
: ($see) ( word quot -- )
2007-09-20 18:09:08 -04:00
[
snippet-style get [
2008-08-23 21:24:54 -04:00
code-style get swap with-nesting
2007-09-20 18:09:08 -04:00
] with-style
2008-08-23 21:24:54 -04:00
] ($block) ; inline
: $see ( element -- ) first [ see ] ($see) ;
2007-09-20 18:09:08 -04:00
2008-08-23 21:24:54 -04:00
: $synopsis ( element -- ) first [ synopsis write ] ($see) ;
2007-09-20 18:09:08 -04:00
: $definition ( element -- )
"Definition" $heading $see ;
: $value ( object -- )
"Variable value" $heading
"Current value in global namespace:" print-element
first dup [ pprint-short ] ($code) ;
: $curious ( element -- )
"For the curious..." $heading print-element ;
: $references ( element -- )
"References" $heading
unclip print-element [ \ $link swap ] { } map>assoc $list ;
: $shuffle ( element -- )
drop
"Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
2008-06-08 16:32:55 -04:00
: $low-level-note ( children -- )
2007-09-20 18:09:08 -04:00
drop
"Calling this word directly is not necessary in most cases. Higher-level words call it automatically." $notes ;
2008-06-08 16:32:55 -04:00
: $values-x/y ( children -- )
2007-09-20 18:09:08 -04:00
drop { { "x" number } { "y" number } } $values ;
2008-06-25 04:53:36 -04:00
: $parsing-note ( children -- )
drop
"This word should only be called from parsing words."
$notes ;
2008-06-08 16:32:55 -04:00
: $io-error ( children -- )
2007-09-20 18:09:08 -04:00
drop
"Throws an error if the I/O operation fails." $errors ;
2008-06-08 16:32:55 -04:00
: $prettyprinting-note ( children -- )
2007-09-20 18:09:08 -04:00
drop {
"This word should only be called from inside the "
{ $link with-pprint } " combinator."
} $notes ;
GENERIC: elements* ( elt-type element -- )
2008-01-09 17:36:30 -05:00
M: simple-element elements* [ elements* ] with each ;
2007-09-20 18:09:08 -04:00
M: object elements* 2drop ;
M: array elements*
2008-01-09 17:36:30 -05:00
[ [ elements* ] with each ] 2keep
2007-09-20 18:09:08 -04:00
[ first eq? ] keep swap [ , ] [ drop ] if ;
: elements ( elt-type element -- seq ) [ elements* ] { } make ;
: collect-elements ( element seq -- elements )
[
swap [
elements [
rest [ dup set ] each
2007-09-20 18:09:08 -04:00
] each
] curry each
] H{ } make-assoc keys ;