factor/basis/help/markup/markup.factor

458 lines
11 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2009 Slava Pestov.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs classes colors colors.constants
2009-10-29 20:34:25 -04:00
combinators combinators.smart definitions definitions.icons effects
fry generic hashtables help.stylesheet help.topics io io.styles
kernel make math namespaces parser present prettyprint
prettyprint.stylesheet quotations see sequences sets slots
sorting splitting strings vectors vocabs vocabs.loader words
words.symbol ;
2009-05-16 05:26:45 -04:00
FROM: prettyprint.sections => with-pprint ;
2007-09-20 18:09:08 -04:00
IN: help.markup
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: blank-line
2007-09-20 18:09:08 -04:00
2008-06-08 16:32:55 -04:00
: last-span? ( -- ? ) last-element get span eq? ;
: last-block? ( -- ? ) last-element get block eq? ;
: last-blank-line? ( -- ? ) last-element get blank-line eq? ;
: ?nl ( -- )
last-element get
last-blank-line? not
and [ nl ] when ;
2007-09-20 18:09:08 -04:00
: ($blank-line) ( -- )
nl nl blank-line last-element set ;
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) ;
2009-02-09 02:47:31 -05:00
M: array print-element unclip execute( arg -- ) ;
M: word print-element { } swap execute( arg -- ) ;
2007-09-20 18:09:08 -04:00
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 [
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 -- )
?nl
2007-09-20 18:09:08 -04:00
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 -- )
drop nl last-element get [ nl ] when
blank-line last-element set ;
2007-09-20 18:09:08 -04:00
! Some blocks
2008-06-08 16:32:55 -04:00
: ($heading) ( children quot -- )
?nl ($block) ; inline
2007-09-20 18:09:08 -04:00
: $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 -- )
[
code-char-style get [
2007-09-20 18:09:08 -04:00
last-element off
2008-12-03 09:46:16 -05:00
[ ($code-style) ] dip with-nesting
2007-09-20 18:09:08 -04:00
] 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 -- )
1 cut* [ "\n" join ] bi@ over <input> [
[ print ] [ output-style get format ] bi*
2007-09-20 18:09:08 -04:00
] ($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) ;
2009-08-20 19:36:55 -04:00
: $deprecated ( element -- )
[
deprecated-style get [
last-element off
"This word is deprecated" $heading print-element
] with-nesting
] ($heading) ;
2009-02-11 05:53:33 -05:00
! Images
: $image ( element -- )
[ first write-image ] ($span) ;
2009-02-11 05:53:33 -05:00
: <$image> ( path -- element )
1array \ $image prefix ;
2007-09-20 18:09:08 -04:00
! Some links
<PRIVATE
2007-09-20 18:09:08 -04:00
: write-link ( string object -- )
link-style get [ write-object ] with-style ;
: link-icon ( topic -- )
definition-icon 1array $image ;
2007-09-20 18:09:08 -04:00
: link-text ( topic -- )
2009-02-12 03:09:45 -05:00
[ article-name ] keep write-link ;
2009-02-01 07:33:44 -05:00
GENERIC: link-long-text ( topic -- )
M: topic link-long-text
[ article-title ] keep write-link ;
GENERIC: link-effect? ( word -- ? )
M: parsing-word link-effect? drop f ;
M: symbol link-effect? drop f ;
M: word link-effect? drop t ;
: $effect ( effect -- )
effect>string stack-effect-style get format ;
M: word link-long-text
dup presented associate [
[ article-name link-style get format ]
[
dup link-effect? [
bl stack-effect $effect
] [ drop ] if
] bi
] with-nesting ;
: >topic ( obj -- topic ) dup topic? [ >link ] unless ;
: topic-span ( topic quot -- ) [ >topic ] dip ($span) ; inline
PRIVATE>
: ($link) ( topic -- ) [ link-text ] topic-span ;
: $link ( element -- ) first ($link) ;
: ($long-link) ( topic -- ) [ link-long-text ] topic-span ;
: $long-link ( element -- ) first ($long-link) ;
: ($pretty-link) ( topic -- )
[ [ link-icon ] [ drop bl ] [ link-text ] tri ] topic-span ;
: $pretty-link ( element -- ) first ($pretty-link) ;
2009-02-01 07:33:44 -05:00
: ($long-pretty-link) ( topic -- )
[ [ link-icon ] [ drop bl ] [ link-long-text ] tri ] topic-span ;
: <$pretty-link> ( definition -- element )
1array \ $pretty-link prefix ;
2008-12-20 18:32:38 -05:00
2008-03-04 23:46:01 -05:00
: ($subsection) ( element quot -- )
2007-09-20 18:09:08 -04:00
[
subsection-style get [ call ] with-style
2008-03-04 23:46:01 -05:00
] ($block) ; inline
2007-09-20 18:09:08 -04:00
: $subsection* ( topic -- )
[
[ ($long-pretty-link) ] with-scope
] ($subsection) ;
: $subsections ( children -- )
[ $subsection* ] each ($blank-line) ;
2008-03-04 23:46:01 -05:00
: $subsection ( element -- )
first $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
[ 2nip ($long-pretty-link) ]
[ [ >vocab-link link-icon bl ] [ ($vocab-link) ] bi ]
if*
2008-03-04 23:46:01 -05:00
] ($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 sep -- )
'[ _ print-element ] swap interleave ; inline
2007-09-20 18:09:08 -04:00
: textual-list ( seq quot -- )
", " (textual-list) ; inline
2007-09-20 18:09:08 -04:00
: $links ( topics -- )
[ [ ($link) ] textual-list ] ($span) ;
: $vocab-links ( vocabs -- )
[ vocab ] map $links ;
: $breadcrumbs ( topics -- )
[ [ ($link) ] " > " (textual-list) ] ($span) ;
2007-09-20 18:09:08 -04:00
: $see-also ( topics -- )
"See also" $heading $links ;
: related-words ( seq -- )
2008-12-20 18:32:38 -05:00
dup '[ _ "related" set-word-prop ] each ;
2007-09-20 18:09:08 -04:00
: $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) ; inline
2007-09-20 18:09:08 -04:00
: $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 )
2008-11-16 07:02:13 -05:00
[ first ] [ length ] bi 1 =
"afhilmnorsx" "aeiou" ? member? "an" "a" ? ;
2007-09-20 18:09:08 -04:00
GENERIC: ($instance) ( element -- )
M: word ($instance)
dup name>> a/an write bl ($link) ;
2007-09-20 18:09:08 -04:00
M: string ($instance)
write ;
2007-09-20 18:09:08 -04:00
2008-11-16 07:02:13 -05:00
M: f ($instance)
drop { f } $link ;
2008-11-16 11:31:04 -05:00
: $instance ( element -- ) first ($instance) ;
2007-09-20 18:09:08 -04:00
: $or ( element -- )
dup length {
{ 1 [ first ($instance) ] }
2009-01-22 23:30:43 -05:00
{ 2 [ first2 [ ($instance) " or " print-element ] [ ($instance) ] bi* ] }
[
drop
unclip-last
[ [ ($instance) ", " print-element ] each ]
[ "or " print-element ($instance) ]
bi*
]
} case ;
2008-11-16 11:31:04 -05:00
: $maybe ( element -- )
f suffix $or ;
2008-11-16 07:02:13 -05:00
2008-11-16 11:31:04 -05:00
: $quotation ( element -- )
2008-11-16 10:03:30 -05:00
{ "a " { $link quotation } " with stack effect " } print-element
$snippet ;
2007-09-20 18:09:08 -04:00
: values-row ( seq -- seq )
unclip \ $snippet swap present 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
[
code-char-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 ;
: $methods ( element -- )
first methods [
"Methods" $heading
[ see-all ] ($see)
] unless-empty ;
2007-09-20 18:09:08 -04:00
: $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
2009-10-29 20:34:25 -04:00
"Shuffle word. Rearranges the top of the datastack as indicated in the stack effect pattern." $description ;
: $complex-shuffle ( element -- )
drop
"Shuffle word. Rearranges the top of the datastack as indicated in the stack effect pattern." $description
{ "The data flow represented by this shuffle word can be more clearly expressed using " { $link "locals" } "." } $deprecated ;
2007-09-20 18:09:08 -04:00
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-12-20 18:32:38 -05:00
M: simple-element elements*
[ elements* ] with each ;
2007-09-20 18:09:08 -04:00
M: object elements* 2drop ;
M: array elements*
[ dup first \ $markup-example eq? [ 2drop ] [ [ elements* ] with each ] if ]
[ [ first eq? ] keep swap [ , ] [ drop ] if ] 2bi ;
2007-09-20 18:09:08 -04:00
: elements ( elt-type element -- seq ) [ elements* ] { } make ;
: collect-elements ( element seq -- elements )
2008-12-20 18:32:38 -05:00
swap '[ _ elements [ rest ] map concat ] map concat prune ;
2008-11-22 04:22:19 -05:00
: <$link> ( topic -- element )
2008-12-11 17:47:38 -05:00
1array \ $link prefix ;
: <$snippet> ( str -- element )
1array \ $snippet prefix ;
: $definition-icons ( element -- )
drop
icons get >alist sort-keys
[ [ <$link> ] [ definition-icon-path <$image> ] bi* swap ] assoc-map
{ "" "Definition class" } prefix
2009-10-29 20:34:25 -04:00
$table ;