Skip to content

Commit

Permalink
refactor HTML code escaping for multi embedded markup
Browse files Browse the repository at this point in the history
  • Loading branch information
finanalyst committed Jan 17, 2025
1 parent 3bc58a9 commit 87a4ebd
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 21 deletions.
2 changes: 1 addition & 1 deletion META6.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Rakuast::RakuDoc::Render",
"description": "Renders RakuDoc v2 to text, HTML, HTML-Extra, Markdown",
"version": "0.24.0",
"version": "0.24.1",
"authors": [
"Richard Hainsworth"
],
Expand Down
4 changes: 2 additions & 2 deletions docs/samples/rakudoc.rakudoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
=begin rakudoc :kind("Language") :subkind("Language") :category("reference")
=TITLE RakuDoc
=SUBTITLE A Raku slang for documenting Raku software to aid development and use.
=VERSION 2.11.0
=VERSION 2.11.1

RakuDoc is a markup language with simple instructions for simple tasks and more
complex structures to suit larger projects. There is a clear distinction between
Expand Down Expand Up @@ -3025,7 +3025,7 @@ information can be rendered using L<a C<P<>> markup instruction|#Placement links

Builtin, semantic and custom blocks are considered by default to be equivalent to C<=head> for the
purposes of a Table of Contents. The following are considered to be equivalent to paragraphs (not
included in a Table of Contents) by default: C<=para>, C<=nested>, C<c=ode>, C<=input>, C<=output>,
included in a Table of Contents) by default: C<=para>, C<=nested>, C<=code>, C<=input>, C<=output>,
<=table>, C<=defn>, C<=item>, C<=alias>, and their I<num> variants if they exist.

This default can be changed with the C<:toc> and C<:headlevel> meta tabs.
Expand Down
18 changes: 11 additions & 7 deletions lib/RakuDoc/To/HTML.rakumod
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,23 @@ class RakuDoc::To::HTML {
my constant BAD-MARK-ON = '<span class="bad-markdown">';
my constant BAD-MARK-OFF = '</span>';
my @bullets = <<\x2022 \x25b9 \x2023 \x2043 \x2219>> ;
my regex tab { '<' $<name> = (<-[>\s]>+) $<cont> = (.*?) '>'};
my regex spantab { '<' \/? 'span' <-[>]>* '>'};
%(
#| special key to name template set
_name => -> %, $ { 'markdown templates' },
#| escape contents of code block
#| escape contents of code block, which may contain <span ...> & </span> tabs, not to be escaped
escape-code => -> %prm, $tmpl {
my $cont = %prm<contents>.Str // '';
while $cont ~~ m:c/ <tab> / {
my $name = ~$<tab><name>;
$cont = $/.replace-with('&lt;' ~ $<tab><name> ~ $<tab><cont> ~ '&gt;')
unless ($name eq <span /span>.any).so
if $cont ~~ / <spantab> / {
( $cont ~~ / ^ [ .*? <spantab> ]+ .*? $ / )
.chunks
.map({
$tmpl.globals.escape.( .value ) if .key eq '~';
.value
})
.join
}
$cont
else { $tmpl.globals.escape.( $cont ) }
},
#| renders =code blocks
code => -> %prm, $tmpl {
Expand Down
51 changes: 50 additions & 1 deletion xt/600-R-2-HTML.rakutest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use v6.d;
use Test;
use Test::Run :runs_ok;
plan 7;
plan 9;

use RakuDoc::Render;
use RakuDoc::To::HTML;
Expand Down Expand Up @@ -50,4 +50,53 @@ runs_ok(
'outputs debug info on AstBlock'
);

%*ENV<RAKURENDEROPTS>:delete;
my $ast = q:to/CODE/.AST;
=begin rakudoc
=begin code :lang<text>
=head2 X<C<hyper>|Syntax,hyper (statement prefix)>, X<C<race>|Syntax,race (statement prefix)>
=end code
=begin code :lang<text>
=head2 X<<<Hyper operators|Operators,<<;Operators,>>;Operators,«;Operators,»;Operators,»=«;Operators,«=»>>>
=end code
=begin code :lang<text>
=head2 X<<<Word quoting with interpolation and quote protection: qqww|Syntax,qqww>>>
=end code
=end rakudoc
CODE

my $rv = $html-instance.rdp.render( $ast, :pre-finalized ).body.Str;

like $rv, /
'<pre' .+? 'code-block' .+? '=head2 X&lt;C&lt;hyper&gt;' .+? 'prefix)&gt;' .+?
'<pre' .+? 'code-block' .+? '=head2 X&lt;&lt;&lt;Hyper operators|Operators,&lt;&lt;;Operators,&gt;&gt;;'
'Operators,«;Operators,»;Operators,»=«;Operators,«=»&gt;&gt;&gt;' .+?
'<pre' .+? 'code-block' .+? '=head2 X&lt;&lt;&lt;Word quoting with interpolation and quote protection: qqww|Syntax,qqww&gt;&gt;&gt;'
/, 'escaping without allow';

$ast = q:to/CODE/.AST;
=begin rakudoc
=begin code :allow<B>
=head2 X<B<hyper>|Syntax,hyper (statement prefix)>, X<B<race>|Syntax,race (statement prefix)>
=end code
=begin code :allow<B>
=head2 X<<<Hyper operators|Operators,<<;Operators,>>;Operators,«;Operators,»;Operators,»=«;Operators,«=»>>>
=end code
=begin code :allow<B>
=head2 X<<<Word quoting with B<interpolation> and quote protection: qqww|Syntax,qqww>>>
=end code
=end rakudoc
CODE

$rv = $html-instance.rdp.render( $ast, :pre-finalized ).body.Str;

like $rv, /
'<pre' .+? 'code-block' .+? '=head2 X&lt;<span class="basis">hyper</span>' .+? 'prefix)&gt;' .+?
'X&lt;<span class="basis">race</span>|Syntax,race (statement prefix)&gt;' .+?
'<pre' .+? 'code-block' .+? '=head2 X&lt;&lt;&lt;Hyper operators|Operators,&lt;&lt;;Operators,&gt;&gt;;'
'Operators,«;Operators,»;Operators,»=«;Operators,«=»&gt;&gt;&gt;' .+?
'<pre' .+? 'code-block' .+? '=head2 X&lt;&lt;&lt;Word quoting with <span class="basis">interpolation</span> and quote protection: qqww|Syntax,qqww&gt;&gt;&gt;'
/, 'escaping with allow';


done-testing;
34 changes: 24 additions & 10 deletions xt/605-H-E-Plugin-Hilite.rakutest
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,33 @@ use RakuDoc::To::HTML;
my $html-instance = RakuDoc::To::HTML.new;
my $rdp := $html-instance.rdp;

plan 2;

lives-ok {
require RakuDoc::Plugin::HTML::Hilite;
RakuDoc::Plugin::HTML::Hilite.new.enable( $rdp );
}, 'hilite plugin enabled';
#my $src = q[my $var = 2;];
#my $hl = highlight( $src, 'HTML' );
#like $hl, /
# '<span' .+ 'magenta' .+ 'my</span>' .+
# 'cyan' .+ '$var</span>' .+
# 'yellow' .+ '=</span' .+
# 'red' .+ '2</span>'
# /, 'statement highlighted with default colors';
#my $hl-pl = $rdp.templates<code>(%(:contents($src), :lang<raku>) );
#like $hl-pl, / '<pre' .+ 'lights">' \s* $hl /, 'similar highlighting';

my $ast = q:to/CODE/.AST;
=begin rakudoc
=begin code
=head2 X<C<hyper>|Syntax,hyper (statement prefix)>, X<C<race>|Syntax,race (statement prefix)>
=end code
=begin code
=head2 X<<<Hyper operators|Operators,<<;Operators,>>;Operators,«;Operators,»;Operators,»=«;Operators,«=»>>>
=end code
=begin code
=head2 X<<<Word quoting with interpolation and quote protection: qqww|Syntax,qqww>>>
=end code
=end rakudoc
CODE

my $rv = $html-instance.rdp.render( $ast, :pre-finalized ).body.Str;

like $rv, /
'<label>Raku highlighting</label>' .+?
'<span class="rainbow-operator">X</span>' .+?
'C&lt;<span class="rainbow-routine">hyper</span>&gt;'
/, 'got Rainbow hilighting in code';

done-testing;

0 comments on commit 87a4ebd

Please sign in to comment.