 ! Copyright (C) 2020 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license
 
-USING: accessors arrays ascii combinators help.markup kernel
+USING: accessors arrays ascii combinators
+combinators.short-circuit help help.markup help.topics kernel
 lexer math namespaces parser sequences splitting strings vectors
 vocabs.parser words ;
 
 IN: easy-help
 
-:: parse-help-token ( end -- str/obj/f )
+:: parse-help-token ( -- str/obj/f )
     ?scan-token dup search {
-        { [ dup end eq? ] [ 2drop f ] }
-        { [ dup parsing-word? ] [ nip V{ } clone swap execute-parsing first ] }
+        { [ dup \ } eq? ] [ 2drop f ] }
+        { [ dup parsing-word? ] [
+            nip V{ } clone swap execute-parsing first
+            dup wrapper? [ wrapped>> \ $link swap 2array ] when ] }
         [ drop ]
     } cond ;
 

    '[ \ } parse-help-words _ prefix suffix! ] define-syntax ;
>>

HELP-WORD: $subsection{
HELP-WORD: $subsections{
HELP-WORD: $link{

! HELP-SYNTAX: $prettyprinting-note{
! HELP-SYNTAX: $definition-icons{

: help-text? ( word -- ? )
    {
        $description $snippet $emphasis $strong $url $heading
        $subheading $code $syntax $class-description
        $error-description $var-description $contract $notes
        $curious $deprecated $errors $side-effects $content
        $slot $image
    } member-eq? ;

: help-values? ( word -- ? )
    { $values $inputs $outputs } member-eq? ;

: help-examples? ( word -- ? )
    { $examples } member-eq? ;

<<
SYNTAX: HELP{
    scan-word dup \ } eq? [ drop { } ] [
        {
            { [ dup help-text? ] [ parse-help-text ] }
            { [ dup help-values? ] [ parse-help-values ] }
            { [ dup help-examples? ] [ parse-help-examples ] }
            [ \ } parse-until >array ]
        } cond swap prefix
    ] if suffix! ;
>>

! HELP{ $description something blah blah \ execute }

SYNTAX: EASY-HELP:
    H{ { "{" POSTPONE: HELP{ } } [
        scan-word bootstrap-word
        [ >link save-location ]
        [ [ parse-array-def ] dip set-word-help ] bi
    ] with-words ;
