factor/library/help/markup.factor

201 lines
4.7 KiB
Factor
Raw Normal View History

2006-01-06 02:04:42 -05:00
! Copyright (C) 2005, 2006 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2005-12-01 00:53:12 -05:00
IN: help
2006-05-15 01:01:47 -04:00
USING: arrays generic hashtables io kernel namespaces
2006-01-09 01:34:23 -05:00
parser prettyprint sequences strings styles vectors words ;
2005-12-01 00:53:12 -05:00
! Simple markup language.
! <element> ::== <string> | <simple-element> | <fancy-element>
! <simple-element> ::== { <element>* }
! <fancy-element> ::== { <type> <element> }
! Element types are words whose name begins with $.
PREDICATE: array simple-element
dup empty? [ drop t ] [ first word? not ] if ;
M: string print-element last-block off format* ;
2005-12-28 20:25:17 -05:00
M: array print-element unclip execute ;
M: word print-element { } swap execute ;
2005-12-01 00:53:12 -05:00
: ($span) ( content style -- )
last-block off [ print-element ] with-style ;
2005-12-01 00:53:12 -05:00
: ($block) ( quot -- )
2006-01-23 21:03:22 -05:00
last-block [ [ terpri ] unless t ] change
call
terpri
last-block on ; inline
2005-12-01 00:53:12 -05:00
! Some spans
: $heading [ heading-style ($span) ] ($block) ;
2005-12-01 00:53:12 -05:00
2005-12-28 20:25:17 -05:00
: $snippet snippet-style ($span) ;
2005-12-01 00:53:12 -05:00
2005-12-19 23:18:15 -05:00
: $emphasis emphasis-style ($span) ;
: $url url-style ($span) ;
: $terpri last-block off terpri terpri drop ;
2005-12-19 23:18:15 -05:00
2005-12-01 00:53:12 -05:00
! Some blocks
M: simple-element print-element
[ print-element ] each ;
2005-12-19 23:18:15 -05:00
: ($code) ( presentation quot -- )
[
code-style [
>r current-style swap presented pick set-hash r>
with-nesting
] with-style
] ($block) ; inline
2005-12-01 00:53:12 -05:00
2005-12-28 20:25:17 -05:00
: $code ( content -- )
2006-01-05 02:00:57 -05:00
"\n" join dup <input> [ format* ] ($code) ;
2005-12-28 20:25:17 -05:00
2006-01-03 17:43:29 -05:00
: $syntax ( word -- )
dup stack-effect [
"Syntax" $heading
2006-01-03 17:43:29 -05:00
>r word-name $snippet " " $snippet r> $snippet
] [
drop
] if* ;
: $stack-effect ( word -- )
stack-effect [
"Stack effect" $heading $snippet
] when* ;
: $vocabulary ( content -- )
"Vocabulary" $heading $snippet ;
2006-01-03 17:43:29 -05:00
2005-12-28 20:25:17 -05:00
: $synopsis ( content -- )
2005-12-31 20:51:58 -05:00
first dup
word-vocabulary [ $vocabulary ] when*
dup parsing? [ $syntax ] [ $stack-effect ] if ;
2005-12-28 20:25:17 -05:00
: $description ( content -- )
"Description" $heading print-element ;
2005-12-28 20:25:17 -05:00
: $contract ( content -- )
"Contract" $heading print-element ;
2005-12-28 20:25:17 -05:00
: $examples ( content -- )
"Examples" $heading print-element ;
2005-12-28 20:25:17 -05:00
2006-01-06 02:04:42 -05:00
: $warning ( content -- )
[
current-style warning-style hash-union [
"Warning" $heading print-element
] with-nesting
] ($block) ;
2006-01-06 02:04:42 -05:00
2006-01-02 01:04:02 -05:00
: textual-list ( seq quot -- )
[ ", " print-element ] interleave ; inline
2006-01-02 01:04:02 -05:00
2006-01-07 16:03:31 -05:00
: $see ( content -- )
code-style [ first see ] with-nesting* ;
: $example ( content -- )
1 swap cut* swap "\n" join dup <input> [
input-style [ format* ] with-style terpri print-element
2006-01-02 01:04:02 -05:00
] ($code) ;
2005-12-28 20:25:17 -05:00
2005-12-01 00:53:12 -05:00
! Some links
2005-12-02 01:02:08 -05:00
TUPLE: link name ;
M: link article-title link-name article-title ;
M: link article-content link-name article-content ;
: ($subsection) ( quot object -- )
subsection-style [
[ swap curry ] keep dup article-title swap <link>
rot simple-outliner
] with-style ;
2005-12-01 00:53:12 -05:00
: $subsection ( object -- )
2006-05-20 17:02:08 -04:00
[
2006-05-25 23:45:19 -04:00
first [ help ] swap ($subsection)
2006-05-20 17:02:08 -04:00
] ($block) ;
2005-12-01 00:53:12 -05:00
2006-03-28 23:31:45 -05:00
: ($subtopic) ( element -- quot )
[
default-style
[ last-block on print-element ] with-nesting*
] curry ;
: $subtopic ( object -- )
[
unclip swap ($subtopic) [
2006-03-28 23:31:45 -05:00
subtopic-style [ print-element ] with-style
] write-outliner
] ($block) ;
: >link ( obj -- obj ) dup string? [ <link> ] when ;
2006-01-06 02:04:42 -05:00
: $link ( article -- )
last-block off first dup word? [
2006-01-13 20:13:14 -05:00
pprint
] [
link-style [
dup article-title swap >link simple-object
2006-01-13 20:13:14 -05:00
] with-style
] if ;
2005-12-01 00:53:12 -05:00
2005-12-30 03:57:38 -05:00
: $definition ( content -- )
"Definition" $heading $see ;
2005-12-30 03:57:38 -05:00
2006-01-02 16:35:37 -05:00
: $see-also ( content -- )
"See also" $heading [ 1array $link ] textual-list ;
2006-01-02 16:35:37 -05:00
2006-01-06 02:04:42 -05:00
: $values ( content -- )
"Arguments and values" $heading
[ unclip $snippet " -- " format* print-element ]
[ terpri ] interleave ;
2006-01-06 02:04:42 -05:00
: $predicate ( content -- )
{ { "object" "an object" } } $values
[
"Tests if the object is an instance of the " ,
{ $link } swap append ,
" class." ,
] { } make $description ;
2005-12-30 03:57:38 -05:00
: $list ( content -- )
[
[
list-element-style [ print-element ] with-nesting*
] ($block)
] each ;
2005-12-30 03:57:38 -05:00
: $errors ( content -- )
"Errors" $heading print-element ;
2005-12-30 03:57:38 -05:00
: $side-effects ( content -- )
"Side effects" $heading "Modifies " print-element
2006-01-02 01:04:02 -05:00
[ $snippet ] textual-list ;
2005-12-30 03:57:38 -05:00
: $notes ( content -- )
"Notes" $heading print-element ;
2006-01-06 22:42:07 -05:00
: $shuffle ( content -- )
drop
"Shuffle word. Re-arranges the stack according to the stack effect pattern." $description ;
2006-01-08 20:41:31 -05:00
: $low-level-note
drop
"Calling this word directly is not necessary in most cases. Higher-level words call it automatically." print-element ;
2006-01-12 00:34:56 -05:00
: $values-x/y
2006-01-12 01:08:45 -05:00
drop
2006-01-12 00:34:56 -05:00
{ { "x" "a complex number" } { "y" "a complex number" } } $values ;
2006-01-16 02:48:15 -05:00
: $io-error
drop
2006-01-16 02:48:15 -05:00
"Throws an error if the I/O operation fails." $errors ;