our
$VERSION
=
'1.0.21'
;
sub
new {
my
(
$class
,
$args
) =
@_
;
raise Template::Liquid::Error {
type
=>
'Context'
,
template
=> (),
message
=>
'Missing template argument'
,
fatal
=> 1
}
if
!
defined
$args
->{
'template'
};
return
bless
{
template
=>
$args
->{
'template'
},
parent
=>
$args
->{
'template'
}
},
$class
;
}
sub
parse {
my
(
$class
,
$args
,
$tokens
);
(
scalar
@_
== 3 ? (
$class
,
$args
,
$tokens
) : (
$class
,
$tokens
)) =
@_
;
my
$s
=
ref
$class
?
$class
:
$class
->new(
$args
);
my
%_tags
=
$s
->{template}->tags;
NODE:
while
(
defined
(
my
$token
=
shift
@{
$tokens
})) {
if
(
$token
=~
$Template::Liquid::Utility::TagMatch
) {
my
(
$tag
,
$attrs
) = (
split
' '
, $1, 2);
my
$package
=
$_tags
{
$tag
};
my
$call
=
$package
?
$package
->can(
'new'
) : ();
if
(
defined
$call
) {
my
$_tag
=
$call
->(
$package
,
{
template
=>
$s
->{template},
parent
=>
$s
,
tag_name
=>
$tag
,
markup
=>
$token
,
attrs
=>
$attrs
}
);
push
@{
$s
->{
'nodelist'
}},
$_tag
;
if
(
$_tag
->conditional_tag) {
push
@{
$_tag
->{
'blocks'
}},
Template::Liquid::Block->new(
{
tag_name
=>
$tag
,
attrs
=>
$attrs
,
template
=>
$_tag
->{template},
parent
=>
$_tag
}
);
$_tag
->parse(
$tokens
);
{
${
$_tag
->{
'blocks'
}[-1]}{
'nodelist'
}
=
$_tag
->{
'nodelist'
};
$_tag
->{
'nodelist'
} = [];
}
}
elsif
(
$_tag
->end_tag) {
$_tag
->parse(
$tokens
);
}
}
elsif
(
$s
->can(
'end_tag'
) &&
$tag
=~
$s
->end_tag) {
$s
->{
'markup_2'
} =
$token
;
last
NODE;
}
elsif
(
$s
->conditional_tag &&
$tag
=~
$s
->conditional_tag) {
$s
->push_block({
tag_name
=>
$tag
,
attrs
=>
$attrs
,
markup
=>
$token
,
template
=>
$s
->{template},
parent
=>
$s
},
$tokens
);
}
else
{
raise Template::Liquid::Error {
type
=>
'Syntax'
,
template
=>
$s
->{template},
message
=>
'Unknown tag: '
.
$token
};
}
}
elsif
(
$token
=~
$Template::Liquid::Utility::VarMatch
) {
my
(
$variable
,
$filters
) =
split
qr[\s*\|\s*]
o, $1, 2;
my
@filters
;
for
my
$filter
(
split
$Template::Liquid::Utility::FilterSeparator
,
$filters
||
''
) {
my
(
$filter
,
$args
)
=
split
$Template::Liquid::Utility::FilterArgumentSeparator
,
$filter
, 2;
$filter
=~ s[\s*$][]o;
$filter
=~ s[^\s*][]o;
my
@args
= !
defined
$args
? () :
grep
{
defined
$_
}
$args
=~ m[
$Template::Liquid::Utility::VariableFilterArgumentParser
]g;
push
@filters
, [
$filter
, \
@args
];
}
push
@{
$s
->{
'nodelist'
}},
Template::Liquid::Variable->new(
{
template
=>
$s
->{template},
parent
=>
$s
,
markup
=>
$token
,
variable
=>
$variable
,
filters
=> \
@filters
}
);
}
else
{
push
@{
$s
->{
'nodelist'
}},
$token
;
}
{
my
$x
= (
$token
=~ m[(\n)]g);
my
$nl
=
$token
=~
tr
/\n//;
$s
->{template}{line} +=
$nl
;
$s
->{template}{column} =
$nl
?
rindex
$token
,
"\n"
:
$s
->{template}{column} +
length
$token
;
}
}
return
$s
;
}
sub
render {
my
(
$s
) =
@_
;
my
$return
=
''
;
for
my
$node
(@{
$s
->{
'nodelist'
}}) {
my
$rendering
=
ref
$node
?
$node
->render() :
$node
;
$return
.=
defined
$rendering
?
$rendering
:
''
;
}
return
$return
;
}
sub
conditional_tag {
return
$_
[0]->{
'conditional_tag'
} ||
undef
; }
1;