| 1 | package BarefootJS::Backend::Xslate; | ||||||||||||||||||||
| 2 | 1 | our $VERSION = "0.41.3"; | |||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 1 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 1 | ▶ | use feature 'signatures'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | ▶ | use Text::Xslate (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use JSON::PP (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 13 | # Text::Xslate (Kolon) rendering backend for the BarefootJS runtime. | ||||||||||||||||||||
| 14 | # --------------------------------------------------------------------------- | ||||||||||||||||||||
| 15 | # | ||||||||||||||||||||
| 16 | # The engine-agnostic runtime logic — the JS-compat value helpers, array/string | ||||||||||||||||||||
| 17 | # methods, hydration markers, child rendering — lives in BarefootJS | ||||||||||||||||||||
| 18 | # (@barefootjs/perl). This backend supplies the four engine-specific operations | ||||||||||||||||||||
| 19 | # the runtime delegates to, targeting Text::Xslate's Kolon syntax: | ||||||||||||||||||||
| 20 | # | ||||||||||||||||||||
| 21 | # encode_json($data) -> JSON string (injectable encoder) | ||||||||||||||||||||
| 22 | # mark_raw($str) -> Text::Xslate raw value (no re-escaping) | ||||||||||||||||||||
| 23 | # materialize($value) -> resolve a captured-children value to a string | ||||||||||||||||||||
| 24 | # render_named($name, $bf, \%v) -> render `<name>.tx` with `bf` + vars bound | ||||||||||||||||||||
| 25 | # | ||||||||||||||||||||
| 26 | # Pair it with the @barefootjs/xslate compile-time adapter, which emits Kolon | ||||||||||||||||||||
| 27 | # `.tx` templates that call the runtime as a `$bf` object: `<: $bf.scope_attr() | ||||||||||||||||||||
| 28 | # :>`, `<: $bf.json($x) :>`, `<: $bf.spread_attrs($bag) :>`. Kolon auto-escapes | ||||||||||||||||||||
| 29 | # `<: ... :>` interpolations (`type => 'html'`); helpers that emit markup return | ||||||||||||||||||||
| 30 | # `mark_raw` values (or the template adds `| mark_raw`), mirroring Mojo EP's | ||||||||||||||||||||
| 31 | # `<%==` vs `<%=` distinction. | ||||||||||||||||||||
| 32 | # | ||||||||||||||||||||
| 33 | # Unlike the Mojo backend, this has no dependency on a web framework: a plain | ||||||||||||||||||||
| 34 | # Text::Xslate instance renders templates from a path, so it runs under any | ||||||||||||||||||||
| 35 | # PSGI / Plack app (or none at all). | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 3 | ▶ | sub new ($class, %args) { | ||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=17.1
Pod
documented
| |||||||||||||||||||||
| 38 | 3 | ▶ | my $json_encoder = $args{json_encoder} // do { | ||||||||||||||||||
|
Condition33%1/3
$args{"json_encoder"} // do {
my $j = "JSON::PP"->new->canonical->allow_nonref;
sub ($data) {
return $j->encode($data);
}
}
MC/DC0%0/2
$args{"json_encoder"} // do {
my $j = "JSON::PP"->new->canonical->allow_nonref;
sub ($data) {
return $j->encode($data);
}
}
$args{"json_encoder"}
do {
my $j = "JSON::PP"->new->canonical->allow_nonref;
sub ($data) {
return $j->encode($data);
}
}
Truth table33%1/3
A // B
A= $args{"json_encoder"}B= do {
my $j = "JSON::PP"->new->canonical->allow_nonref;
sub ($data) {
return $j->encode($data);
}
}
| |||||||||||||||||||||
| 39 | # Default pure-Perl encoder. `canonical` keeps key order deterministic | ||||||||||||||||||||
| 40 | # (matching the runtime's sorted-key SSR policy); `allow_nonref` lets | ||||||||||||||||||||
| 41 | # scalars / undef encode as `"x"` / `null`. Swap via `json_encoder` | ||||||||||||||||||||
| 42 | # for a faster XS implementation. | ||||||||||||||||||||
| 43 | 3 | my $j = JSON::PP->new->canonical->allow_nonref; | |||||||||||||||||||
| 44 | 3 | ▶ | sub ($data) { return $j->encode($data) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 45 | }; | ||||||||||||||||||||
| 46 | |||||||||||||||||||||
| 47 | # Accept a pre-built Text::Xslate instance, or build one from `path` | ||||||||||||||||||||
| 48 | # (a dir of `.tx` templates) plus any extra `xslate_options`. The adapter | ||||||||||||||||||||
| 49 | # calls every runtime helper as a `$bf` method (`$bf.filter`, `$bf.lc`, …) | ||||||||||||||||||||
| 50 | # or a Kolon builtin (`.join`, `.size`), so no custom `function` map is | ||||||||||||||||||||
| 51 | # needed here — a plain Kolon, html-escaping instance suffices. | ||||||||||||||||||||
| 52 | 3 | my $xslate = $args{xslate}; | |||||||||||||||||||
| 53 | 3 | ▶ | unless ($xslate) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | $xslate = Text::Xslate->new( | ||||||||||||||||||||
| 55 | syntax => 'Kolon', | ||||||||||||||||||||
| 56 | type => 'html', | ||||||||||||||||||||
| 57 | ($args{path} ? (path => $args{path}) : ()), | ||||||||||||||||||||
| 58 | 3 | ▶ | %{ $args{xslate_options} // {} }, | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$args{"xslate_options"} // {}
MC/DC0%0/1
$args{"xslate_options"} // {}
$args{"xslate_options"}
Truth table50%1/2
A
A= $args{"xslate_options"}
| |||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 3 | return bless { xslate => $xslate, json_encoder => $json_encoder }, $class; | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | ▶ | sub xslate ($self) { return $self->{xslate} } | ||||||||||||||||||
|
Subroutine
xslate: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 1 | ▶ | sub encode_json ($self, $data) { | ||||||||||||||||||
|
Subroutine
encode_json: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 68 | 1 | return $self->{json_encoder}->($data); | |||||||||||||||||||
| 69 | } | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | # Mark a string as already-safe so Kolon emits it verbatim (no auto-escape). | ||||||||||||||||||||
| 72 | 1 | ▶ | sub mark_raw ($self, $str) { | ||||||||||||||||||
|
Subroutine
mark_raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 73 | 1 | return Text::Xslate::mark_raw($str); | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # JSX children captured by the adapter (a Kolon macro call yields a rendered | ||||||||||||||||||||
| 77 | # string; some paths may pass a CODE ref) resolve to a string here. | ||||||||||||||||||||
| 78 | 2 | ▶ | sub materialize ($self, $value) { | ||||||||||||||||||
|
Subroutine
materialize: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 79 | 2 | ▶ | return ref($value) eq 'CODE' ? $value->() : $value; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 80 | } | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | # Render `<name>.tx` with `$child_bf` bound as the `bf` object for the nested | ||||||||||||||||||||
| 83 | # render, plus the supplied template vars. No stash juggling is needed: Kolon | ||||||||||||||||||||
| 84 | # resolves `$bf` from the per-render vars, so each child render gets its own | ||||||||||||||||||||
| 85 | # instance directly. | ||||||||||||||||||||
| 86 | 3 | ▶ | sub render_named ($self, $template_name, $child_bf, $vars) { | ||||||||||||||||||
|
Subroutine
render_named: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 87 | 3 | return $self->{xslate}->render("$template_name.tx", { %$vars, bf => $child_bf }); | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 1 | 1; | |||||||||||||||||||
| 91 | __END__ | ||||||||||||||||||||