| 1 | package Text::Nunjucks::Loader; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: 1:1 port of Nunjucks src/loader.js -- the base Loader (resolve/isRelative) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 15 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 15 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | with 'Text::Nunjucks::EmitterObj'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use Text::Nunjucks::Lib; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | # src/loader.js: `module.exports = class Loader extends EmitterObj { resolve; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # isRelative }`. EmitterObj -> the on/emit role (ADR 0002). The concrete | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # loaders extend it in their own modules: src/node-loaders.js's FileSystemLoader | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # is Text::Nunjucks::Loader::FileSystem, src/precompiled-loader.js's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # PrecompiledLoader is Text::Nunjucks::Loader::Precompiled (ADR 0018). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | # `cache` is not declared by loader.js -- Environment attaches `loader.cache = {}` | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # in _initLoaders. Declaring it here (with the same empty-hash default) gives the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # Environment a clean accessor to reach for instead of poking the object's guts. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 5150 | ▶ | has cache => ( is => 'rw', default => sub { {} } ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # loaders opt into async by setting `loader.async = true`; the base is sync. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # Environment reads `loader.async`, so the base answers it (falsey). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 4291 | ▶ | sub async { return } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
async: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 8 | ▶ | sub _lib { 'Text::Nunjucks::Lib' } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_lib: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # resolve(from, to) -- path.resolve(path.dirname(from), to), through node's own | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # path functions in Lib (ADR 0018): lexical, so '..' collapses on the string | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # and the file system is never asked (from 'views/sub/page.njk', '../d.njk' is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # '<cwd>/views/d.njk'), anchored at the working directory only when `from` is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # relative. Environment.resolveTemplate calls it for a relative name, and the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # result is both the loader's cache key and the name getSource receives. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 105 | ▶ | sub resolve { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | my ( $self, $from, $to ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve: not called CC=1 SCAR=1.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | return $self->_lib->node_path_resolve( $self->_lib->node_path_dirname($from), $to ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # isRelative(filename) -- a name starting './' or '../'. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 10931 | ▶ | sub is_relative { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_relative: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | my ( $self, $filename ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_relative: not called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | return ( index( $filename, './' ) == 0 || index( $filename, '../' ) == 0 ) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
index($filename, "./") == 0 or index($filename, "../") == 0
index($filename, "./") == 0 or index($filename, "../") == 0
MC/DC100%2/2
index($filename, "./") == 0 or index($filename, "../") == 0
index($filename, "./") == 0
index($filename, "../") == 0
MC/DC0%0/2
index($filename, "./") == 0 or index($filename, "../") == 0
index($filename, "./") == 0
index($filename, "../") == 0
Truth table100%3/3
A or B
A= index($filename, "./") == 0B= index($filename, "../") == 0
Truth table0%0/3
A or B
A= index($filename, "./") == 0B= index($filename, "../") == 0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||