line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# -*- perl -*- |
2
|
|
|
|
|
|
|
# |
3
|
|
|
|
|
|
|
# Text::Smart::Plugin by Daniel Berrange |
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# Copyright (C) 2004-2006 Daniel P. Berrange |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# This program is free software; you can redistribute it and/or modify |
8
|
|
|
|
|
|
|
# it under the terms of the GNU General Public License as published by |
9
|
|
|
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
|
|
|
|
# (at your option) any later version. |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful, |
13
|
|
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
|
|
|
|
# GNU General Public License for more details. |
16
|
|
|
|
|
|
|
# |
17
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License |
18
|
|
|
|
|
|
|
# along with this program; if not, write to the Free Software |
19
|
|
|
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20
|
|
|
|
|
|
|
# |
21
|
|
|
|
|
|
|
# $Id: Plugin.pm,v 1.2 2004/05/13 10:42:37 dan Exp $ |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=pod |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head1 NAME |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Text::Smart::Plugin - Template Toolkit plugin for Text::Smart |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 SYNOPSIS |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
When creating the L processing object define a |
32
|
|
|
|
|
|
|
plugin for smart text: |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $tt = new Template({ |
35
|
|
|
|
|
|
|
PLUGINS => { |
36
|
|
|
|
|
|
|
'smarttext' => 'Text::Smart::Plugin' |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
}); |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Then in a template file: |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
[% USE smarttext(type => 'HTML') %] |
43
|
|
|
|
|
|
|
[% FILTER smarttext %] |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
... some smart text markup ... |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
[% END %] |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 DESCRIPTION |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
This module provides a plugin for the Template-Toolkit to |
52
|
|
|
|
|
|
|
enable the use of 'smart text', whose syntax is defined by |
53
|
|
|
|
|
|
|
the L module. See that module's manual pages |
54
|
|
|
|
|
|
|
for details of the markup allowed. See the synopsis section |
55
|
|
|
|
|
|
|
above for how to use this module in combination with the |
56
|
|
|
|
|
|
|
Template toolkit. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 METHODS |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=over 4 |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=cut |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
package Text::Smart::Plugin; |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
1
|
|
1854
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
38
|
|
67
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
1
|
|
5
|
use base qw(Template::Plugin); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
2150
|
|
70
|
|
|
|
|
|
|
|
71
|
1
|
|
|
1
|
|
31211
|
use Text::Smart::HTML; |
|
1
|
|
|
|
|
5832
|
|
|
1
|
|
|
|
|
730
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
our $VERSION = "1.0.1"; |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=item my $plugin = Text::Smart::Plugin->new() |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
This creates a new instance of the plugin. This method is called |
78
|
|
|
|
|
|
|
by the Template Toolkit engine to instantiate the plugin. See the |
79
|
|
|
|
|
|
|
docs for L for details about the contract between the |
80
|
|
|
|
|
|
|
engine and this plugin's constructor. |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
=cut |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub new { |
85
|
0
|
|
|
0
|
1
|
|
my $proto = shift; |
86
|
0
|
|
0
|
|
|
|
my $class = ref($proto) || $proto; |
87
|
0
|
|
|
|
|
|
my $context = shift; |
88
|
0
|
|
|
|
|
|
my $options = shift; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
|
my $filter_factory; |
91
|
|
|
|
|
|
|
my $self; |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my $plugin; |
94
|
0
|
0
|
|
|
|
|
if ($options) { |
95
|
|
|
|
|
|
|
# create a closure to generate filters with additional options |
96
|
|
|
|
|
|
|
$filter_factory = sub { |
97
|
0
|
|
|
0
|
|
|
my $context = shift; |
98
|
0
|
0
|
|
|
|
|
my $filtopt = ref $_[-1] eq 'HASH' ? pop : { }; |
99
|
0
|
|
|
|
|
|
@$filtopt{ keys %$options } = values %$options; |
100
|
|
|
|
|
|
|
return sub { |
101
|
0
|
|
|
|
|
|
_tt_smarttext(@_, $filtopt); |
102
|
0
|
|
|
|
|
|
}; |
103
|
0
|
|
|
|
|
|
}; |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# and a closure to represent the plugin |
106
|
|
|
|
|
|
|
$plugin = sub { |
107
|
0
|
0
|
|
0
|
|
|
my $plugopt = ref $_[-1] eq 'HASH' ? pop : { }; |
108
|
0
|
|
|
|
|
|
@$plugopt{ keys %$options } = values %$options; |
109
|
0
|
|
|
|
|
|
_tt_smarttext(@_, $plugopt); |
110
|
0
|
|
|
|
|
|
}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
else { |
113
|
|
|
|
|
|
|
# simple filter factory closure (no legacy options from constructor) |
114
|
|
|
|
|
|
|
$filter_factory = sub { |
115
|
0
|
|
|
0
|
|
|
my $context = shift; |
116
|
0
|
0
|
|
|
|
|
my $filtopt = ref $_[-1] eq 'HASH' ? pop : { }; |
117
|
|
|
|
|
|
|
return sub { |
118
|
0
|
|
|
|
|
|
_tt_smarttext(@_, $filtopt); |
119
|
0
|
|
|
|
|
|
}; |
120
|
0
|
|
|
|
|
|
}; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# plugin without options can be static |
123
|
0
|
|
|
|
|
|
$plugin = \&_tt_smarttext; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
# now define the filter and return the plugin |
127
|
0
|
|
|
|
|
|
$context->define_filter('smarttext', [ $filter_factory => 1 ]); |
128
|
0
|
|
|
|
|
|
return $plugin; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub _tt_smarttext { |
132
|
0
|
0
|
|
0
|
|
|
my $options = ref $_[-1] eq 'HASH' ? pop : { }; |
133
|
0
|
0
|
|
|
|
|
my $type = defined $options->{type} ? $options->{type} : "HTML"; |
134
|
0
|
0
|
|
|
|
|
if ($type eq 'HTML') { |
135
|
0
|
|
|
|
|
|
my $proc = Text::Smart::HTML->new(); |
136
|
|
|
|
|
|
|
|
137
|
0
|
|
|
|
|
|
return $proc->process(join('', @_)); |
138
|
|
|
|
|
|
|
} else { |
139
|
0
|
|
|
|
|
|
die "Unknown smart text markup type '$type'. Suported types are 'HTML'"; |
140
|
|
|
|
|
|
|
} |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
1 # So that the require or use succeeds. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
__END__ |