factor/extra/help/help.factor

143 lines
3.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2005, 2007 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
2006-08-17 23:08:04 -04:00
USING: arrays io kernel namespaces parser prettyprint sequences
words assocs definitions generic quotations effects
slots continuations tuples debugger combinators
vocabs help.stylesheet help.topics help.crossref help.markup
sorting ;
IN: help
GENERIC: word-help* ( word -- content )
: word-help ( word -- content )
dup "help" word-prop [ ] [
dup word-help* dup
[ swap 2array 1array ] [ 2drop f ] if
] ?if ;
M: word word-help* drop f ;
M: slot-reader word-help* drop \ $slot-reader ;
M: slot-writer word-help* drop \ $slot-writer ;
: all-articles ( -- seq )
articles get keys
all-words [ word-help ] subset append ;
: xref-help ( -- )
all-articles [ xref-article ] each ;
: error? ( word -- ? )
\ $error-description swap word-help elements empty? not ;
: sort-articles ( seq -- newseq )
[ dup article-title ] { } map>assoc sort-values 0 <column> ;
: all-errors ( -- seq )
all-words [ error? ] subset sort-articles ;
M: word article-name word-name ;
2006-06-17 01:03:56 -04:00
M: word article-title
dup parsing? over symbol? or [
2006-08-17 23:08:04 -04:00
word-name
] [
dup word-name
2006-08-18 03:51:41 -04:00
swap stack-effect
[ effect>string " " swap 3append ] when*
2006-08-17 23:08:04 -04:00
] if ;
2006-06-17 01:03:56 -04:00
M: word article-content
[
2006-06-17 01:18:46 -04:00
\ $vocabulary over 2array ,
dup word-help %
\ $related over 2array ,
dup get-global [ \ $value swap 2array , ] when*
\ $definition swap 2array ,
2006-06-17 01:03:56 -04:00
] { } make ;
2006-06-16 23:12:40 -04:00
M: word article-parent "help-parent" word-prop ;
M: word set-article-parent swap "help-parent" set-word-prop ;
: $doc-path ( article -- )
help-path dup empty? [
drop
] [
[
help-path-style get [
"Parent topics: " write $links
] with-style
] ($block)
] if ;
2006-08-16 21:55:53 -04:00
: $title ( topic -- )
title-style get [
title-style get [
dup [
dup article-title swap >link write-object
] ($block) $doc-path
] with-nesting
] with-style nl ;
2005-12-28 20:25:17 -05:00
: help ( topic -- )
last-element off dup $title
article-content print-content nl ;
2006-06-17 01:03:56 -04:00
: about ( vocab -- )
dup vocab-help [
help
] [
"The " write vocab-name write
" vocabulary does not define a main help article." print
"To define one, refer to \\ ABOUT: help" print
] ?if ;
: ($index) ( seq quot -- )
subsection-style get [
sort-articles [ nl ] [ ($subsection) ] interleave
] with-style ;
2006-06-17 01:03:56 -04:00
: $index ( element -- )
2006-10-26 00:06:25 -04:00
first call dup empty?
[ drop ] [ [ ($index) ] ($block) ] if ;
: $about ( element -- )
first vocab-help [ 1array $subsection ] when* ;
: (:help-multi)
"This error has multiple delegates:" print
($index) nl ;
: (:help-none)
drop "No help for this error. " print ;
: :help ( -- )
error get delegates [ error-help ] map [ ] subset
{
{ [ dup empty? ] [ (:help-none) ] }
{ [ dup length 1 = ] [ first help ] }
{ [ t ] [ (:help-multi) ] }
} cond ;
: remove-article ( name -- )
dup articles get key? [
dup unxref-article
dup articles get delete-at
] when drop ;
: add-article ( article name -- )
[ remove-article ] keep
[ articles get set-at ] keep
xref-article ;
: remove-word-help ( word -- )
dup word-help [ dup unxref-article ] when
f "help" set-word-prop ;
: set-word-help ( content word -- )
[ remove-word-help ] keep
[ swap "help" set-word-prop ] keep
xref-article ;