use v6.d;
use Test;
use Test::Run :runs_ok;
plan 11;

use RakuDoc::Render;
use RakuDoc::To::HTML;
my $html-instance = RakuDoc::To::HTML.new;
my @generic = RakuDoc::Processor.new.default-text-templates.keys;
my @html = $html-instance.html-templates.keys;

ok @generic (<=) @html, 'HTML redefines all the keys in generic';
cmp-ok (@html (-) @generic), '(==)', < VERSION footer top-of-page head-block html-root escape-code main-content >, 'other templates RakuDoc::To::HTML additionally defines';
%*ENV<RAKUDO_RAKUAST> = 1;
my @args = <<$*EXECUTABLE -I. --rakudoc=HTML xt/test-files/test.rakudoc>>;
runs_ok(
    'simple html is generated',
    :@args,
    :out( / '<html' .+  'lang' .+ 'en' .+' <div' .+  'Just a test' / )
);
%*ENV<MORE_HTML> = 'xt/test-filles/example.raku';
runs_ok(
    :@args,
    :exitcode(1),
    :err(/ 'filles' .+ 'not a file' /),
    'Bad file error'
);
%*ENV<MORE_HTML> = 'xt/test-files/html-example.raku';
runs_ok(
    :@args,
    :exitcode(0),
    :out(/
        '<html' .+
        '<head>'  .+
        '<meta' .+ 'A Customised description' .+ '/>' .+
        '</head>'
    / ),
    'customisation in meta of head'
);
%*ENV<ALT_CSS> = 'xt/test-files/strawberry.css';
runs_ok(
    :@args,
    :exitcode(0),
    :out(/ 'h.title{font-size:larger;color:red' / ),
    'CSS customisation occurs'
);

@args = <<$*EXECUTABLE -I. --rakudoc=HTML xt/test-files/test-fr.rakudoc>>;
runs_ok(
    'simple html with fr lang set is generated',
    :@args,
    :out( / '<html' .+  'lang' .+ 'fr' .+' <div' .+  'Un simple test' / )
);

%*ENV<MORE_HTML>:delete;
%*ENV<ALT_CSS>:delete;
%*ENV<RAKURENDEROPTS> = 'AstBlock';
runs_ok(
    :@args,
    :exitcode(0),
    :out(/ 'Handling: RakuAST::Doc::Block' / ),
    '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' .+? 'rakudoc' .+? '=head2 X&lt;C&lt;hyper&gt;' .+? 'prefix)&gt;'  .+?
    '<pre' .+? 'rakudoc' .+? '=head2 X&lt;&lt;&lt;Hyper operators|Operators,&lt;&lt;;Operators,&gt;&gt;;'
    'Operators,«;Operators,»;Operators,»=«;Operators,«=»&gt;&gt;&gt;' .+?
    '<pre' .+? 'rakudoc' .+? '=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' .+? 'rakudoc' .+? '=head2 X&lt;<span class="basis">hyper</span>' .+? 'prefix)&gt;' .+?
    'X&lt;<span class="basis">race</span>|Syntax,race (statement prefix)&gt;' .+?
    '<pre' .+? 'rakudoc' .+? '=head2 X&lt;&lt;&lt;Hyper operators|Operators,&lt;&lt;;Operators,&gt;&gt;;'
    'Operators,«;Operators,»;Operators,»=«;Operators,«=»&gt;&gt;&gt;' .+?
    '<pre' .+? 'rakudoc' .+? '=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';


$ast = q:to/CODE/.AST;
=begin rakudoc
This is a block with escaped HTML C<&amp;> in it
=end rakudoc
CODE

$rv = $html-instance.rdp.render( $ast, :pre-finalized ).body.Str;
todo 'explicitly requested HTML are properly escaped';
like $rv, /
    '<span class="code">&amp;amp;</span>'
    /, 'explicitly escaped HTML entity';

done-testing;
