« blib/lib/Text/Nunjucks/Compiler/Compiler.pm
↑ Summary
blib/lib/Text/Nunjucks/Nodes/CallExtension.pm »
| 1 | package Text::Nunjucks::Loader::FileSystem; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: 1:1 port of Nunjucks src/node-loaders.js FileSystemLoader -- templates from search paths on disk | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 15 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use builtin qw( is_bool true false ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Text::Nunjucks::Lib; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Text::Nunjucks::Lib::Error; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use Text::Nunjucks::Runtime; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 15 | ▶ | no warnings 'experimental::builtin'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 15 | extends 'Text::Nunjucks::Loader'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # src/node-loaders.js defines FileSystemLoader and NodeResolveLoader and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # re-exports PrecompiledLoader. Only FileSystemLoader is ported here; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # PrecompiledLoader is Text::Nunjucks::Loader::Precompiled, NodeResolveLoader | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # (require.resolve over node_modules) is out of scope (ADR 0018). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # The node built-ins it calls -- path.normalize, path.resolve, fs.existsSync, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # fs.readFileSync(p, 'utf-8'), console.log -- are Lib's node_* functions, node | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # v24.19.0's behaviour, posix (ADR 0018). The opts it reads are any JS value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # (upstream still warns about a boolean), so they are read as JS reads them, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # through Runtime's member_lookup and truthiness. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # constructor(searchPaths, opts) sets this.pathsToNames, this.noCache and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # this.searchPaths: the attributes below, snake_cased for Perl. They are rw | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # because the constructor body assigns them, as upstream does, once Moo built | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # the object: BUILDARGS keeps the positional arguments, BUILD is the body | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # (super() is Moo's). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 15 | has search_paths => ( is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 15 | has no_cache => ( is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 15 | has paths_to_names => ( is => 'rw' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 15 | has _init_args => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # Collaborators named once, reached through the invocant (house rule). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 1047 | ▶ | sub _lib { 'Text::Nunjucks::Lib' } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_lib: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 111 | ▶ | sub _runtime { 'Text::Nunjucks::Runtime' } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_runtime: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | sub _error_class { 'Text::Nunjucks::Lib::Error' } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_error_class: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # new FileSystemLoader(searchPaths, opts) -- positional. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 111 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my ( $class, @args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | return { _init_args => [@args] }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # constructor(searchPaths, opts) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 111 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=8 SCAR=28.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: not called CC=8 SCAR=28.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | my ( $search_paths, $opts ) = @{ $self->_init_args // [] }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition25%1/4
$self->_init_args // []
$self->_init_args // []
MC/DC0%0/1
$self->_init_args // []
$self->_init_args
MC/DC0%0/1
$self->_init_args // []
$self->_init_args
Truth table50%1/2
A
A= $self->_init_args
Truth table0%0/2
A
A= $self->_init_args
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | delete $self->{_init_args}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | my $rt = $self->_runtime; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | if ( is_bool($opts) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $self->_lib->node_console_log( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | '[nunjucks] Warning: you passed a boolean as the second ' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | . 'argument to FileSystemLoader, but it now takes an options ' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | . 'object. See http://mozilla.github.io/nunjucks/api.html#filesystemloader' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $opts = $rt->_truthy($opts) ? $opts : {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $self->paths_to_names( {} ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $self->no_cache( $rt->_truthy( $rt->member_lookup( $opts, 'noCache' ) ) ? true : false ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | if ( $rt->_truthy($search_paths) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | $search_paths = $self->_lib->is_array($search_paths) ? $search_paths : [$search_paths]; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # For windows, convert to forward slashes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $self->search_paths( [ map { $self->_lib->node_path_normalize($_) } @$search_paths ] ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $self->search_paths( ['.'] ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( $rt->_truthy( $rt->member_lookup( $opts, 'watch' ) ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | # Watch all the templates in the paths and fire an event when | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | # they change -- through chokidar, which has no Perl image: the port is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # upstream without it installed, so its require() fails and upstream's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # catch throws (ADR 0018). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | die $self->_error_class->new('watch requires chokidar to be installed'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 163 | ▶ | sub get_source { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_source: called CC=5 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my ( $self, $name ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_source: not called CC=5 SCAR=20.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | my $fullpath = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | my $paths = $self->search_paths; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | for ( my $i = 0; $i < @$paths; $i++ ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | my $base_path = $self->_lib->node_path_resolve( $paths->[$i] ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | my $p = $self->_lib->node_path_resolve( $paths->[$i], $name ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | # Only allow the current directory and anything | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | # underneath it to be searched | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | # A string-prefix test, as upstream's p.indexOf(basePath) === 0: a sibling | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | # whose name extends the search path's passes it ('views' admits | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | # '../views2/x.njk'). Ported as is (ADR 0004). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ( index( $p, $base_path ) == 0 && $self->_lib->node_fs_exists_sync($p) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)
index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)
MC/DC100%2/2
index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)
index($p, $base_path) == 0
$self->_lib->node_fs_exists_sync($p)
MC/DC0%0/2
index($p, $base_path) == 0 and $self->_lib->node_fs_exists_sync($p)
index($p, $base_path) == 0
$self->_lib->node_fs_exists_sync($p)
Truth table100%3/3
A and B
A= index($p, $base_path) == 0B= $self->_lib->node_fs_exists_sync($p)
Truth table0%0/3
A and B
A= index($p, $base_path) == 0B= $self->_lib->node_fs_exists_sync($p)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | $fullpath = $p; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | ▶ | if ( !$fullpath ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $self->paths_to_names->{$fullpath} = $name; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | my $source = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | src => $self->_lib->node_fs_read_file_sync_utf8($fullpath), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | path => $fullpath, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | noCache => $self->no_cache, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $self->emit( 'load', $name, $source ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | return $source; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||