Permalink
Browse files

default <:= ... :> tags to html formatting

this can be overriden with [html].tagformat
  • Loading branch information...
tonycoz committed Feb 25, 2013
1 parent 65807fa commit 6f186c3b0d005bd6a4b34ee8981ac842332b6df4
@@ -4,7 +4,24 @@ use Squirrel::Template;
use Carp qw(confess cluck);
use Config ();
-our $VERSION = "1.009";
+our $VERSION = "1.010";
+
+my %formats =
+ (
+ html => sub {
+ require BSE::Util::HTML;
+ return BSE::Util::HTML::escape_html($_[0]);
+ },
+ uri => sub {
+ require BSE::Util::HTML;
+ return BSE::Util::HTML::escape_uri($_[0]);
+ },
+ raw => sub {
+ return $_[0];
+ },
+ );
+$formats{h} = $formats{html};
+$formats{u} = $formats{uri};
sub templater {
my ($class, $cfg, $rsets) = @_;
@@ -26,17 +43,8 @@ sub templater {
template_dir => \@dirs,
utf8 => $cfg->utf8,
charset => $cfg->charset,
- formats =>
- {
- html => sub {
- require BSE::Util::HTML;
- return BSE::Util::HTML::escape_html($_[0]);
- },
- uri => sub {
- require BSE::Util::HTML;
- return BSE::Util::HTML::escape_uri($_[0]);
- },
- },
+ formats => \%formats,
+ def_format => $cfg->entry("html", "tagformat", "html"),
trace_noimpl => scalar($cfg->entry("debug", "trace_noimpl", 0)),
);
if ($cfg->entry("basic", "cache_templates")) {
@@ -20,7 +20,7 @@ BEGIN {
use constant DEBUG_GET_PARMS => 0;
-our $VERSION = "1.024";
+our $VERSION = "1.025";
my %compile_cache;
@@ -34,6 +34,7 @@ sub new {
$opts{param} = [];
$opts{wraps} = [];
$opts{errors} = [];
+ $opts{def_format} ||= "";
return bless \%opts, $class;
}
@@ -644,6 +645,15 @@ by template files. Defaults to C<"utf-8">.
A BSE::Cache object to use for caching compiled templates. Note that
templates are currently only cached by filename.
+=item formats
+
+A hashref of content formatters used for formatting C<< E<lt>=
+... E<gt> >> tags.
+
+=item def_format
+
+The default format for C<< E<lt>= ... E<gt> >> tags.
+
=back
=item show_page()
@@ -2,7 +2,7 @@ package Squirrel::Template::Parser;
use strict;
use Squirrel::Template::Constants qw(:token :node);
-our $VERSION = "1.013";
+our $VERSION = "1.014";
use constant TOK => 0;
use constant TMPLT => 1;
@@ -131,6 +131,7 @@ sub _parse_expr {
my $parsed;
if (eval { $parsed = $parser->parse($expr->[TOKEN_EXPR_EXPR]); 1 }) {
$expr->[NODE_EXPR_EXPR] = $parsed;
+ $expr->[NODE_EXPR_FORMAT] ||= $self->[TMPLT]{def_format};
return $expr;
}
elsif (ref $@) {
View
@@ -239,6 +239,11 @@ If non-zero then any HTML output is validated with HTML::Tidy.
Validation errors and warnings are sent to the audit log. See [html
tidy].
+=item tagformat
+
+The default tag formatting to use for C<< <:= ... :> >> tags.
+Default: C<html>.
+
=back
=head2 [basic]
@@ -9,15 +9,15 @@
<:iterator begin messages:>
<:-.set msgbase = [ state, "msg:bse/admin/imageclean/", "NOMSGID" ][1] -:>
<:- .if state.type eq "stage" -:>
- <h1><:= request.htmlmsg(msgbase _ "stage/" _ state.stage) -:></h1>
+ <h1><:= request.htmlmsg(msgbase _ "stage/" _ state.stage) |raw -:></h1>
<: .elsif state.type eq "substage" -:>
- <h2><:= request.htmlmsg(msgbase _ "substage/" _ state.stage _ "/" _ state.substage) -:></h2>
+ <h2><:= request.htmlmsg(msgbase _ "substage/" _ state.stage _ "/" _ state.substage) |raw -:></h2>
<: .elsif state.type eq "orphanimage" :>
- <div><:= request.htmlmsg(msgbase _ "process/" _ ( acted ? "remove" : "skip" ) _ "image", [ state.image.id, state.image.image ]) :></div>
+ <div><:= request.htmlmsg(msgbase _ "process/" _ ( acted ? "remove" : "skip" ) _ "image", [ state.image.id, state.image.image ]) |raw :></div>
<: .elsif state.type eq "orphanfile" :>
- <div><:= request.htmlmsg(msgbase _ "process/" _ ( acted ? "remove" : "skip" ) _ "file", [ state.file ]) :></div>
+ <div><:= request.htmlmsg(msgbase _ "process/" _ ( acted ? "remove" : "skip" ) _ "file", [ state.file ]) |raw :></div>
<: .elsif state.type eq "error" :>
-<div class="message error"><:= state.error | html :></div>
+<div class="message error"><:= state.error :></div>
<: .end if -:>
<:iterator end messages:>
</div>
@@ -12,13 +12,13 @@
<:- .if state.type eq "stage" -:>
<h1><:= request.catmsg(msgbase _ "stage/" _ state.stage) -:></h1>
<: .elsif state.type eq "substage" -:>
- <h2><:= request.htmlmsg(msgbase _ "substage/" _ state.stage _ "/" _ state.substage) -:></h2>
+ <h2><:= request.htmlmsg(msgbase _ "substage/" _ state.stage _ "/" _ state.substage) |raw :></h2>
<: .elsif state.type eq "orphanimage" :>
<div><input type="checkbox" name="image" value="<:= state.image.id :>" checked="checked" />
- <:= request.htmlmsg(msgbase _ "preview/image", [ state.image.id, state.image.image ]) :></div>
+ <:= request.htmlmsg(msgbase _ "preview/image", [ state.image.id, state.image.image ]) |raw :></div>
<: .elsif state.type eq "orphanfile" :>
<div><input type="checkbox" name="file" value="<:= state.file :>" checked="checked" />
- <:= request.htmlmsg(msgbase _ "preview/file", [ state.file ]) :></div>
+ <:= request.htmlmsg(msgbase _ "preview/file", [ state.file ]) |raw:></div>
<: .elsif state.type eq "error" :>
<div class="message error"><:= state.error | html :></div>
<: .end if -:>
@@ -4,7 +4,7 @@
<:.if request.messages.size != 0:>
<:-.for m in request.messages -:>
-<div class="message <:= m.class:>"><:= m.html :></div>
+<div class="message <:= m.class:>"><:= m.html |raw:></div>
<:.end for-:>
<:.end if:>
@@ -2,7 +2,7 @@
<h1>Logon</h1>
<:.if request.messages.size != 0:>
<:-.for m in request.messages -:>
-<div class="message <:= m.class:>"><:= m.html :></div>
+<div class="message <:= m.class:>"><:= m.html |raw:></div>
<:.end for-:>
<:.end if:>
<form method="post" action="<:adminbase:><:script:>">
@@ -69,7 +69,7 @@ and base pages</a> (<a
href="<:= cfg.admin_url("generate", { "id": "extras", "progress":1 }) | html :>">verbose</a>)</li></ul>
<:or:><:eif:>
-<p><a href="<:= cfg.admin_url("makeIndex") | html :>">Regenerate search index</a> (<a href="<:= cfg.admin_url("makeIndex", { "progress":1 }) | html :>">verbose</a>)</p>
+<p><a href="<:= cfg.admin_url("makeIndex") | html :>">Regenerate search index</a> (<a href="<:= cfg.admin_url("makeIndex", { "verbose":1 }) | html :>">verbose</a>)</p>
<p><a href="<:= cfg.admin_url("backmon") | html :>">Background processing</a></p>
@@ -85,7 +85,7 @@ Page <:= pages.page :> of <:= pages.pagecount :>
<:.define error_img -:>
<:# parameters: field -:>
<:# this implementation should probably change -:>
-<:= ("error_img " _ field).evaltag -:>
+<:= ("error_img " _ field).evaltag |raw -:>
<:.end define -:>
<:.define input -:>
@@ -158,7 +158,7 @@ Page <:= pages.page :> of <:= pages.pagecount :>
<: .if request.messages.size -:>
<div class="messages">
<: .for m in request.messages -:>
- <div class="<:= m.class :>"><:= m.html :></div>
+ <div class="<:= m.class :>"><:= m.html |raw :></div>
<: .end for -:>
</div>
<: .end if -:>
View
@@ -43,7 +43,7 @@ sub dyn_template_test($$$$);
my $parent = bse_make_catalog
(
cfg => $cfg,
- title => "test catalog",
+ title => "test & catalog",
body => "Test catalog for catalog tests",
force_dynamic => 1,
);
@@ -509,8 +509,8 @@ Top Title: [<:= top.title |html :>]
Embedded: [<:= embedded | html :>]
Dynamic: [<:= dynamic | html :>]
TEMPLATE
-Article Title: [test catalog]
-Top Title: [test catalog]
+Article Title: [test &amp; catalog]
+Top Title: [test &amp; catalog]
Embedded: [0]
Dynamic: [1]
EXPECTED

0 comments on commit 6f186c3

Please sign in to comment.