| 1 | package Text::Nunjucks::Compiler; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: 1:1 port of Nunjucks src/compiler.js -- emits Perl source | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 16 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 16 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 16 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 16 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 16 | ▶ | use Text::Nunjucks::Parser; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 16 | ▶ | use Text::Nunjucks::Transformer; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 16 | ▶ | use Text::Nunjucks::Compiler::Compiler; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # 1:1 port of upstream src/compiler.js. compiler.js exports { compile, Compiler }; | ||||||||||||||||||||||||||||
| 14 | # the port mirrors that shape the way Parser.pm mirrors parser.js: the module | ||||||||||||||||||||||||||||
| 15 | # functions live here on Text::Nunjucks::Compiler (compile(), and | ||||||||||||||||||||||||||||
| 16 | # compiler_class() for the exported Compiler class), while the class itself is | ||||||||||||||||||||||||||||
| 17 | # the Moo class Text::Nunjucks::Compiler::Compiler in its own file (ADR 0002, | ||||||||||||||||||||||||||||
| 18 | # object.js not ported; ADR 0020). | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # The class carries the emission rules: what compiler.js's JavaScript output | ||||||||||||||||||||||||||||
| 21 | # becomes in Perl is set out at the top of Text/Nunjucks/Compiler/Compiler.pm. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | #### module.exports.compile -- compile(src, asyncFilters, extensions, name, opts) | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 9364 | ▶ | sub compile { | ||||||||||||||||||||||||||
|
Subroutine
compile: called CC=5 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my ( $self, $src, $async_filters, $extensions, $name, $opts ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
compile: not called CC=5 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||
|
Condition25%1/4
$opts //= {}
$opts //= {}
MC/DC0%0/1
$opts //= {}
$opts
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | my $c = $self->compiler_class->new( | ||||||||||||||||||||||||||||
| 30 | template_name => $name, | ||||||||||||||||||||||||||||
| 31 | throw_on_undefined => $opts->{throwOnUndefined}, | ||||||||||||||||||||||||||||
| 32 | 0 | ); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # Run the extension preprocessors against the source. | ||||||||||||||||||||||||||||
| 35 | 0 | my $processed_src = $src; | |||||||||||||||||||||||||||
| 36 | 0 | ▶ | for my $ext ( @{ $extensions || [] } ) { | ||||||||||||||||||||||||||
|
Condition50%2/4
$extensions || []
$extensions || []
MC/DC100%1/1
$extensions || []
$extensions
MC/DC0%0/1
$extensions || []
$extensions
Truth table100%2/2
A
A= $extensions
Truth table0%0/2
A
A= $extensions
| |||||||||||||||||||||||||||||
| 37 | 0 | my $pre = $self->_ext_preprocess($ext); | |||||||||||||||||||||||||||
| 38 | 0 | ▶ | $processed_src = $pre->($processed_src) if $pre; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | $c->compile( | ||||||||||||||||||||||||||||
| 42 | 0 | $self->transformer->transform( | |||||||||||||||||||||||||||
| 43 | $self->parser->parse( $processed_src, $extensions, $opts ), | ||||||||||||||||||||||||||||
| 44 | $async_filters, | ||||||||||||||||||||||||||||
| 45 | $name | ||||||||||||||||||||||||||||
| 46 | ) | ||||||||||||||||||||||||||||
| 47 | ); | ||||||||||||||||||||||||||||
| 48 | 0 | return $c->get_code; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # Collaborators named once, reached through the invocant (house rule). | ||||||||||||||||||||||||||||
| 52 | 9364 | ▶ | sub parser { 'Text::Nunjucks::Parser' } | ||||||||||||||||||||||||||
|
Subroutine
parser: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 53 | 9364 | ▶ | sub transformer { 'Text::Nunjucks::Transformer' } | ||||||||||||||||||||||||||
|
Subroutine
transformer: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 9364 | ▶ | sub compiler_class { 'Text::Nunjucks::Compiler::Compiler' } | ||||||||||||||||||||||||||
|
Subroutine
compiler_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # ext.preprocess -- extension representation is open (k8/k22); read it as an | ||||||||||||||||||||||||||||
| 57 | # object method or a hash coderef, else nothing, and report. | ||||||||||||||||||||||||||||
| 58 | 17 | ▶ | sub _ext_preprocess { | ||||||||||||||||||||||||||
|
Subroutine
_ext_preprocess: called CC=7 SCAR=28.7
| |||||||||||||||||||||||||||||
| 59 | 17 | my ( $self, $ext ) = @_; | |||||||||||||||||||||||||||
| 60 | 17 | ▶ | return undef unless defined $ext && ref $ext; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ext and ref $ext
MC/DC0%0/2
defined $ext and ref $ext
defined $ext
ref $ext
Truth table33%1/3
A and B
A= defined $extB= ref $ext
| |||||||||||||||||||||||||||||
| 61 | 17 | ▶ | if ( blessed($ext) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ▶ | return $ext->can('preprocess') ? sub { $ext->preprocess(@_) } : undef; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return ( ref $ext eq 'HASH' && $ext->{preprocess} ) ? $ext->{preprocess} : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $ext eq "HASH" and $ext->{"preprocess"}
MC/DC0%0/2
ref $ext eq "HASH" and $ext->{"preprocess"}
ref $ext eq "HASH"
$ext->{"preprocess"}
Truth table0%0/3
A and B
A= ref $ext eq "HASH"B= $ext->{"preprocess"}
| |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 16 | 1; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | __END__ | ||||||||||||||||||||||||||||