Skip to content

Commit

Permalink
RakuAST: add preliminary support for =alias / A<>
Browse files Browse the repository at this point in the history
- add $*DOC-ALIASES dynamic variable, contains has with mapping
  of lemmas and their content
- add handling of =alias to ::Doc::Block.from-paragraphs
  The first atom is the lemma, the rest is the content
- add handling of A<> to set the contents of the lemma as the
  meta of the ::Doc::Markup
- add =alias and A<> handling to RakuDoc::To::Text (as in: don't
  render 'alias' blocks, and use the meta for rendering A<>
  • Loading branch information
lizmat committed Jul 8, 2023
1 parent b8a3096 commit eb753a3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
6 changes: 5 additions & 1 deletion lib/RakuDoc/To/Text.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ my multi sub rakudoc2text(RakuAST::Node:D $ast --> Str:D) {
# the general handler, with specific sub-actions
my multi sub rakudoc2text(RakuAST::Doc::Block:D $ast --> Str:D) {
given $ast.type {
when 'alias' { '' }
when 'code' { code2text($ast) }
when 'comment' { '' }
when 'config' { '' }
Expand Down Expand Up @@ -139,7 +140,10 @@ my multi sub rakudoc2text(RakuAST::Doc::Markup:D $ast --> Str:D) {
else {
my str $text = $ast.atoms.map(&rakudoc2text).join;

if $letter eq 'L' {
if $letter eq 'A' {
rakudoc2text $ast.meta.head
}
elsif $letter eq 'L' {
$text = colored($text, 'underline');

# remember the URL as a note
Expand Down
3 changes: 3 additions & 0 deletions src/Raku/Grammar.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@ grammar Raku::Grammar is HLL::Grammar does Raku::Common {
# into its statement list.
my $*DOC-BLOCKS-COLLECTED := [];
# RakuDoc aliases (=alias -> A<>) collected so far
my $*DOC-ALIASES := {};
# Any resolver that exists outside this grammar: usually this is the
# resolver that is active whenever code is being EVALled inside BEGIN
# block, which would create a new resolver and put it in $*R.
Expand Down
6 changes: 5 additions & 1 deletion src/Raku/ast/doc-block.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ class RakuAST::Doc::Block
!! nqp::list);
Nil
}
method add-paragraph($paragraph) { nqp::push($!paragraphs, $paragraph) }
method add-paragraph($paragraph, :$at-start) {
$at-start
?? nqp::unshift($!paragraphs, $paragraph)
!! nqp::push( $!paragraphs, $paragraph)
}
method paragraphs() { self.IMPL-WRAP-LIST($!paragraphs) }

method visit-children(Code $visitor) {
Expand Down
25 changes: 21 additions & 4 deletions src/core.c/RakuAST/Fixups.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,14 @@ augment class RakuAST::Doc::Markup {
}
}
}
elsif $letter eq 'A' {
my $aliases := $*DOC-ALIASES;
unless nqp::istype($aliases,Failure) {
if nqp::atkey($aliases,self.atoms.head) -> $alias {
self.set-meta($alias);
}
}
}
}

# flatten this markup recursively
Expand Down Expand Up @@ -754,18 +762,27 @@ augment class RakuAST::Doc::Block {

elsif $type eq 'alias' {
my @parts = @paragraphs;
# first line is the lemma, separate that
@parts.splice(0,1,@parts.head.split(/ \s+ /,2));

# lemma does not allow markup
$block.add-paragraph(@parts.shift);
# separate first word: lemma does not allow markup
@parts.splice(0,1,@parts.head.split(/ \s+ /,2));
my str $lemma = @parts.shift;

# add rest with possible markup
$block.add-paragraph(
nqp::istype($_,Str)
?? RakuAST::Doc::Paragraph.from-string($_)
!! $_
) for @parts;

# collect alias info if being collected
my $aliases := $*DOC-ALIASES;
nqp::bindkey(
$aliases,
$lemma,
nqp::clone(nqp::getattr($block.paragraphs,List,'$!reified'))
) unless nqp::istype($aliases,Failure);

$block.add-paragraph($lemma, :at-start);
}

elsif $type eq 'defn' {
Expand Down

0 comments on commit eb753a3

Please sign in to comment.