X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=Debbugs%2FText.pm;h=53ecf04c47fb60019ec09445355e07700eec46cc;hb=466f7faff129a5699c7674f59900a92aa256175d;hp=0f3a5299a226d8647e07aa97d54d3f59de5faf3c;hpb=03820c4df63c2f71623951a5919b072fb9a67d15;p=debbugs.git diff --git a/Debbugs/Text.pm b/Debbugs/Text.pm index 0f3a529..53ecf04 100644 --- a/Debbugs/Text.pm +++ b/Debbugs/Text.pm @@ -16,8 +16,8 @@ Debbugs::Text -- General routines for text templates =head1 SYNOPSIS -use Debbugs::Text qw(:templates); -print fill_in_template(template => 'cgi/foo'); + use Debbugs::Text qw(:templates); + print fill_in_template(template => 'cgi/foo'); =head1 DESCRIPTION @@ -33,7 +33,7 @@ None known. use vars qw($DEBUG $VERSION @EXPORT_OK %EXPORT_TAGS @EXPORT @ISA); -use base qw(Exporter); +use Exporter qw(import); BEGIN { $VERSION = 1.00; @@ -45,12 +45,9 @@ BEGIN { @EXPORT_OK = (); Exporter::export_ok_tags(qw(templates)); $EXPORT_TAGS{all} = [@EXPORT_OK]; - push @ISA,qw(Safe::Hole::User); } -use Safe; -use Safe::Hole; -use Text::Template; +use Text::Xslate qw(html_builder); use Storable qw(dclone); @@ -61,37 +58,84 @@ use Carp; use IO::File; use Data::Dumper; +### for %text_xslate_functions +use POSIX; +use Debbugs::CGI qw(html_escape); +use Scalar::Util; +use Debbugs::Common qw(make_list); +use Debbugs::Status; + our %tt_templates; our %filled_templates; -our $safe; -our $hole = Safe::Hole->new({}); our $language; -# This function is what is called when someone does include('foo/bar') -# {include('foo/bar')} - -sub include { - my $template = shift; - $filled_templates{$template}++; - print STDERR "include template $template language $language safe $safe\n" if $DEBUG; - # Die if we're in a template loop - die "Template loop with $template" if $filled_templates{$template} > 10; - my $filled_tmpl = ''; - eval { - $filled_tmpl = fill_in_template(template => $template, - variables => {}, - language => $language, - safe => $safe, - ); - }; - if ($@) { - print STDERR "failed to fill template $template: $@"; - } - print STDERR "failed to fill template $template\n" if $filled_tmpl eq '' and $DEBUG; - print STDERR "template $template '$filled_tmpl'\n" if $DEBUG; - $filled_templates{$template}--; - return $filled_tmpl; -}; + +sub __output_select_options { + my ($options,$value) = @_; + my @options = @{$options}; + my $output = ''; + while (@options) { + my ($o_value) = shift @options; + if (ref($o_value)) { + for (@{$o_value}) { + unshift @options, + ($_,$_); + } + next; + } + my $name = shift @options; + my $selected = ''; + if (defined $value and $o_value eq $value) { + $selected = ' selected'; + } + $output .= q(\n); + } + return $output; +} + +sub __text_xslate_functions { + return + {gm_strftime => sub {POSIX::strftime($_[0],gmtime)}, + package_links => html_builder(\&Debbugs::CGI::package_links), + bug_links => html_builder(\&Debbugs::CGI::bug_links), + looks_like_number => \&Scalar::Util::looks_like_number, + isstrongseverity => \&Debbugs::Status::isstrongseverity, + secs_to_english => \&Debbugs::Common::secs_to_english, + maybelink => \&Debbugs::CGI::maybelink, + # add in a few utility routines + duplicate_array => sub { + my @r = map {($_,$_)} make_list(@{$_[0]}); + return @r; + }, + output_select_options => html_builder(\&__output_select_options), + make_list => \&make_list, + }; +} +sub __text_xslate_functions_text { + return + {bugurl => + sub{ + return "$_[0]: ". + $config{cgi_domain}.'/'. + Debbugs::CGI::bug_links(bug=>$_[0], + links_only => 1, + ); + }, + }; +} + + + +### this function removes leading spaces from line-start code strings and spaces +### before <:- and spaces after -:> +sub __html_template_prefilter { + my $text = shift; + $text =~ s/^\s+:/:/mg; + $text =~ s/((?:^:[^\n]*\n)?)\s*(<:-)/$1$2/mg; + $text =~ s/(-:>)\s+(^:|)/$1.(length($2)?"\n$2":'')/emg; + return $text; +} =head2 fill_in_template @@ -101,15 +145,14 @@ sub include { language => '..' ); -Reads a template from disk (if it hasn't already been read in) and -fills the template in. +Reads a template from disk (if it hasn't already been read in) andf +ills the template in. =cut - sub fill_in_template{ my %param = validate_with(params => \@_, - spec => {template => SCALAR|HANDLE|SCALARREF, + spec => {template => SCALAR, variables => {type => HASHREF, default => {}, }, @@ -119,132 +162,59 @@ sub fill_in_template{ output => {type => HANDLE, optional => 1, }, - safe => {type => OBJECT, - optional => 1, - }, hole_var => {type => HASHREF, optional => 1, }, + output_type => {type => SCALAR, + default => 'html', + }, }, ); - #@param{qw(template variables language safe output hole_var no_safe)} = @_; - if ($DEBUG) { - print STDERR "fill_in_template "; - print STDERR join(" ",map {exists $param{$_}?"$_:$param{$_}":()} keys %param); - print STDERR "\n"; - } - # Get the text - my $tt_type = ''; - my $tt_source; - if (ref($param{template}) eq 'GLOB' or - ref(\$param{template}) eq 'GLOB') { - $tt_type = 'FILE_HANDLE'; - $tt_source = $param{template}; - } - elsif (ref($param{template}) eq 'SCALAR') { - $tt_type = 'STRING'; - $tt_source = ${$param{template}}; - } - else { - $tt_type = 'FILE'; - $tt_source = _locate_text($param{template},$param{language}); - } - if (not defined $tt_source) { - die "Unable to find template $param{template} with language $param{language}"; - } - - if (defined $param{safe}) { - $safe = $param{safe}; - } - else { - print STDERR "Created new safe\n" if $DEBUG; - $safe = Safe->new() or die "Unable to create safe compartment"; - $safe->permit_only(':base_core',':base_loop',':base_mem', - qw(padsv padav padhv padany), - qw(rv2gv refgen srefgen ref), - qw(caller require entereval), - ); - $safe->share('*STDERR'); - $safe->share('%config'); - $hole->wrap(\&Debbugs::Text::include,$safe,'&include'); - my $root = $safe->root(); - # load variables into the safe - for my $key (keys %{$param{variables}||{}}) { - print STDERR "Loading $key\n" if $DEBUG; - if (ref($param{variables}{$key})) { - no strict 'refs'; - print STDERR $safe->root().'::'.$key,qq(\n) if $DEBUG; - *{"${root}::$key"} = $param{variables}{$key}; - } - else { - no strict 'refs'; - ${"${root}::$key"} = $param{variables}{$key}; - } - } - for my $key (keys %{exists $param{hole_var}?$param{hole_var}:{}}) { - print STDERR "Wraping $key as $param{hole_var}{$key}\n" if $DEBUG; - $hole->wrap($param{hole_var}{$key},$safe,$key); - } - } - $language = $param{language}; + my $output_type = $param{output_type}; + my $language = $param{language}; + my $template = $param{template}; + $template .= '.tx' unless $template =~ /\.tx$/; my $tt; - if ($tt_type eq 'FILE' and - defined $tt_templates{$tt_source} and - (stat $tt_source)[9] > $tt_templates{$tt_source}{mtime} + if (not exists $tt_templates{$output_type}{$language} or + not defined $tt_templates{$output_type}{$language} ) { - $tt = $tt_templates{$tt_source}{template}; - } - else { - if ($tt_type eq 'FILE') { - $tt_templates{$tt_source}{mtime} = - (stat $tt_source)[9]; - } - $tt = Text::Template->new(TYPE => $tt_type, - SOURCE => $tt_source, - ); - if ($tt_type eq 'FILE') { - $tt_templates{$tt_source}{template} = $tt; - } + $tt_templates{$output_type}{$language} = + Text::Xslate->new(# cache in template_cache or temp directory + cache_dir => $config{template_cache} // + File::Temp::tempdir(CLEANUP => 1), + # default to the language, but fallback to en_US + path => [$config{template_dir}.'/'.$language.'/', + $config{template_dir}.'/en_US/', + ], + suffix => '.tx', + ## use html or text specific functions + function => + ($output_type eq 'html' ? __text_xslate_functions() : + __text_xslate_functions_text()), + syntax => 'Kolon', + module => ['Text::Xslate::Bridge::Star', + 'Debbugs::Text::XslateBridge', + ], + type => $output_type, + ## use the html-specific pre_process_handler + $output_type eq 'html'? + (pre_process_handler => \&__html_template_prefilter):(), + ) + or die "Unable to create Text::Xslate"; } - if (not defined $tt) { - die "Unable to create Text::Template for $tt_type:$tt_source"; + $tt = $tt_templates{$output_type}{$language}; + my $ret = + $tt->render($template, + {time => time, + %{$param{variables}//{}}, + config => \%config, + }); + if (exists $param{output}) { + print {$param{output}} $ret; + return ''; } - my $ret = $tt->fill_in(#(defined $param{nosafe} and $param{nosafe})?():(HASH=>$param{variables}), - #(defined $param{nosafe} and $param{nosafe})?():(SAFE=>$safe), - SAFE => $safe, - #(defined $param{nosafe} and $param{nosafe})?(PACKAGE => 'main'):(), - defined $param{output}?(OUTPUT=>$param{output}):(), - ); - if (not defined $ret) { - print STDERR $Text::Template::ERROR; - return ''; - } - if ($DEBUG) { - no strict 'refs'; - no warnings 'uninitialized'; - my $temp = $param{nosafe}?'main':$safe->{Root}; - print STDERR "Variables for $param{template}\n"; - print STDERR "Safe $temp\n"; - print STDERR map {"$_: ".*{$_}."\n"} keys %{"${temp}::"}; - } - return $ret; } -sub _locate_text{ - my ($template,$language) = @_; - $template =~ s/\.tmpl$//g; - # if a language doesn't exist, use the en_US template - if (not -e $config{template_dir}.'/'.$language.'/'.$template.'.tmpl') { - $language = 'en_US'; - } - my $loc = $config{template_dir}.'/'.$language.'/'.$template.'.tmpl'; - if (not -e $loc) { - print STDERR "Unable to locate template $loc\n"; - return undef; - } - return $loc; -} - 1;