### ----------------------------------------------------
### -- Raku::Pod::Render
### -- Licenses: Artistic-2.0
### -- Authors: Richard Hainsworth
### -- File: xt/55-custom-formatting-codes.rakutest
### ----------------------------------------------------
use v6.*;
use Test;
use File::Directory::Tree;
use ProcessedPod;

# A helper sub for RakuClosureTemplates
sub gen-closure-template (Str $tag) is export {
    my $start = '<' ~ $tag ~ '>';
    my $end = '</' ~ $tag ~ '>';
    return sub (%prm, %tml? --> Str) {
        $start ~ (%prm<contents> // '') ~ $end;
    }
}

my $rv;
my $processor = ProcessedPod.new;
my $pc = 0;

plan 5;

use RenderPod::Templating;
my @templates = SetupTemplates.new.required;


my %templates  = @templates Z=> @templates.map( { gen-closure-template( $_ ) });
# this creates a set of pseudo templates
$processor.templates( %templates );
$processor.modify-templates( %(
# escape must work on each non-space character, not on the string!!! So test with .uc
    escaped => sub ( $str ) { $str.uc },
));

=begin pod
This text has a F<fa-snowflake-o> icon
=end pod

$rv = $processor.render-block($=pod[$pc++]);
like $rv,
        /
        '<unknown-name>FA-SNOWFLAKE-O</unknown-name>'
        /, 'unknown format F';

$processor.modify-templates( %(
    :format-f( sub (%prm, %tmpl) { "<i class=\"fa { %prm<contents> }\"></i>" }),
    format-s => sub (%prm, %tmpl) {
        '<div class="' ~ %prm<meta>[0] ~ '">' ~ %prm<contents> ~ '</div>'
    },
    format-w => sub (%prm, %tmpl) {
        "<div class=\"{ %prm<meta>[0] }\" on-click=\"{ %prm<meta>[1] }\">{ %prm<contents> }</div>"
    },
));

=begin pod
This text has a F<fa-snowflake-o> icon
=end pod

$rv = $processor.render-block($=pod[$pc++]);
like $rv,
        /
        '<i class="fa FA-SNOWFLAKE-O"></i>'
        /, 'renders a fa';

=begin pod
This is an item for sale S<Click to purchase with XXXProvider | xxxprovider_button>
=end pod

$rv = $processor.render-block($=pod[$pc++]);
like $rv,
        /
        '<div class="xxxprovider_button">CLICK TO PURCHASE WITH XXXPROVIDER</div>'
        /, 'renders an s';
=begin pod
This is an item for sale W<Click to purchase with XXXProvider | xxxprovider_button; some-js-function> and stuff
=end pod

$rv = $processor.render-block($=pod[$pc++]);
like $rv,
        /
        '<div class="xxxprovider_button"'
        \s+ 'on-click="some-js-function">'
        'CLICK TO PURCHASE WITH XXXPROVIDER</div>'
        /, 'renders an w';

=begin pod

This contains a customised F<fa-B<address>-card|customisation> format code.

=end pod

$processor.modify-templates( %( :format-f(
    sub (%prm, %tml) { '<i class="fa ' ~ %prm<contents> ~ '" extra="' ~ %prm<meta> ~ '"></i>'} ),
) );

$rv = $processor.render-block($=pod[$pc++]);
like $rv, /
    'THIS CONTAINS A CUSTOMISED '
    '<i class="fa FA-<format-b>ADDRESS</format-b>-CARD" '
    'extra="customisation"></i> FORMAT CODE.'
/, 'customised format code';

done-testing;


