blib/lib/Kwiki/PageTemplate.pm

filter: Statementstmt 25.5% 33 / 129 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 37.9% 11 / 29 Podpod n/a totaltotal 25.1% 44 / 175 CC 10 SCAR 14.7
CC
10
Cov
23%
new_from_page_template
12.0
render_edit_form
12.0
preview
6.0
CRAP 4.3
↑ Summary
1 package Kwiki::PageTemplate;
2 1 ▶use Kwiki::Plugin -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use mixin 'Kwiki::Installer';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 our $VERSION = '0.04';
5  
6 1 ▶use YAML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 const class_id => 'page_template';
10 const cgi_class => 'Kwiki::PageTemplate::CGI';
11 const config_file => 'page_template.yaml';
12 const css_file => 'page_template.css';
13  
14 field fields => {};
15 field previewed => 0;
16  
17 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
18 0 my $reg = shift;
19 0 $reg->add(action => 'new_from_page_template');
20 0 $reg->add(action => 'edit_page_template_content');
21 0 $reg->add(wafl => field => 'Kwiki::PageTemplate::FieldPhrase');
22 0 $reg->add(wafl => page_template => 'Kwiki::PageTemplate::TemplateBlock');
23 0 $reg->add(wafl => page_template_display => 'Kwiki::PageTemplate::DisplayBlock');
24 0 $reg->add(wafl => page_template_fields => 'Kwiki::PageTemplate::FieldBlock');
25 0 $reg->add(wafl => page_template_content => 'Kwiki::PageTemplate::ContentBlock');
26 }
27  
28 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
29 0 super;
30 0 $self->use_class('formatter');
31 }
32  
33 0 ▶sub new_from_page_template {
Subroutine new_from_page_template: not called CC=3 SCAR=24.8
34 0 my $fields = $self->fields_of($self->cgi->template_page);
35 0 $self->fields($fields);
36 0 my %raw = $self->cgi->vars;
37 0 my %values;
38 0 $values{$_} = $raw{"field_$_"} for keys %$fields;
39 0 my $tpinfo = {template => $self->cgi->template_page, values => \%values};
40 0 my $waflblock = $self->make_page_template_content($tpinfo);
41 0 ▶ $self->cgi->button eq $self->config->page_template_save_button_text ?
Branch: 1 branch
BranchTrueFalseTotal
$self->cgi->button eq $self->config->page_template_save_button_text ? : 0 0 0
42 $self->save_new_page($waflblock): $self->preview($waflblock,$tpinfo);
43 }
44  
45 0 ▶sub preview {
Subroutine preview: not called CC=2 SCAR=17.9
46 0 my ($waflblock,$tpinfo) = @_;
47 0 my $preview = $self->formatter->text_to_html($waflblock);
48 0 $preview =~ s{</?(form|input)[^>]*?>}{}gs;
49 0 ▶ my $page = $self->cgi->defined('save_to_page') ?
Branch: 1 branch
BranchTrueFalseTotal
$self->cgi->defined('save_to_page') ? : 0 0 0
50 $self->cgi->save_to_page : '';
51 0 $self->previewed(1);
52 0 $self->render_edit_form($tpinfo,$page,$preview);
53 }
54  
55 0 ▶sub save_new_page {
Subroutine save_new_page: not called CC=1 SCAR=6.9
56 0 my $content = shift;
57 0 my $page = $self->get_page;
58 0 $page->content($content);
59 0 $page->store;
60 0 $self->redirect($page->id);
61 }
62  
63 0 ▶sub make_page_template_content {
Subroutine make_page_template_content: not called CC=1 SCAR=6.9
64 0 my $mark = ".page_template_content\n";
65 0 $mark.YAML::Dump(shift)."\n$mark";
66 }
67  
68 0 ▶sub extract_page_template_content {
Subroutine extract_page_template_content: not called CC=1 SCAR=6.9
69 0 my $page_content = shift;
70 0 $page_content =~ s{(\.page_template_content)\n(.+)\n\1}{$2}sg;
71 0 YAML::Load($page_content);
72 }
73  
74 0 ▶sub render_edit_form {
Subroutine render_edit_form: not called CC=3 SCAR=24.8
75 0 my ($content,$page_id,$preview) = @_;
76 0 my $tp = $self->pages->new_page($content->{template})->content;
77 0 ▶ while(my ($k,$v) = each %{$content->{values}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$content{"values"};}) 0 0 0
78 # XXX: $self->uri_escape() somehow doesn't work well
79 0 $v =~ s/%/%25/g; $v =~ s/\n/%0A/g; $v =~ s/\r/%0D/g;
80 0 $tp =~ s/{field:\s*$k(.*?)?}/{field: $k , value:$v}/sg;
81 }
82 0 my $html = $self->formatter->text_to_html($tp);
83 0 ▶ my $save_to = $page_id ?
Branch: 1 branch
BranchTrueFalseTotal
$page_id ? : 0 0 0
84 qq{<input type="hidden" name="save_to_page" value="$page_id" />} :
85 "<!-- No save_to_page -->";
86 0 $html =~ s{(<input type="hidden" name="template_page" value=)".+?" />}
87 {$1"$content->{template}" />$save_to}s;
88 0 $self->render_screen(
89 content_pane => 'page_template_edit_content.html',
90 preview => $preview,
91 page_template_content => $html
92 );
93 }
94  
95 0 ▶sub edit_page_template_content {
Subroutine edit_page_template_content: not called CC=1 SCAR=6.9
96 0 my $page_id = $self->cgi->page_id;
97 0 my $ptc = $self->hub->pages->new_page($page_id)->content;
98 0 my $content = $self->extract_page_template_content($ptc);
99 0 $self->render_edit_form($content,$page_id);
100 }
101  
102 0 ▶sub fields_of {
Subroutine fields_of: not called CC=1 SCAR=6.9
103 0 my $content = $self->hub->pages->new_page(shift)->content;
104 0 my $mark = ".page_template_fields";
105 0 $content =~ /$mark\n(.+?)$mark/s;
106 0 YAML::Load($1);
107 }
108  
109 0 ▶sub get_page {
Subroutine get_page: not called CC=2 SCAR=17.9
110 0 ▶ $self->cgi->defined('save_to_page') ?
Branch: 1 branch
BranchTrueFalseTotal
$self->cgi->defined('save_to_page') ? : 0 0 0
111 $self->pages->new_page($self->cgi->save_to_page) :
112 $self->create_page($self->cgi->page_id_prefix) ;
113 }
114  
115 0 ▶sub create_page {
Subroutine create_page: not called CC=1 SCAR=6.9
116 0 my $prefix = shift;
117 0 my $num = 1 + max(map {s/^.*?(\d+)$/$1/; $_} grep { /^$prefix/ }
118 $self->hub->pages->all_ids_newest_first);
119 0 $self->pages->new_page("${prefix}${num}");
120 }
121  
122 0 ▶sub extract_display_block {
Subroutine extract_display_block: not called CC=2 SCAR=17.9
123 0 my $content = shift;
124 0 ▶ if( $content =~ /^\.page_template_display$/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^\.page_template_display$/m) { } 0 0 0
125 0 $content =~ s{.*(\.page_template_display)\s+(.*)\s+\1.*}{$2}s;
126 } else {
127 0 $content =~ s{.*(\.page_template)\s+(.*)\s+\1.*}{$2}s;
128 }
129 0 $content;
130 }
131  
132 0 ▶sub render {
Subroutine render: not called CC=2 SCAR=17.9
133 0 my ($template_page,$values) = @_;
134 0 my $c = $self->extract_display_block
135 ($self->pages->new_page($template_page)->content);
136 0 $c =~ s/{field:\s*$_\s*.*}/$values->{$_}/ for keys %$values;
137 0 $self->template_process(
138 'page_template_content.html',
139 content => $self->formatter->text_to_html($c)
140 );
141 }
142  
143 package Kwiki::PageTemplate::FieldBlock;
144 1 ▶use base 'Spoon::Formatter::WaflBlock';
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 0 ▶sub to_html {
Subroutine to_html: not called CC=1 SCAR=6.9
147 0 $self->hub->page_template->fields(YAML::Load($self->block_text));
148 0 "";
149 }
150  
151 package Kwiki::PageTemplate::ContentBlock;
152 1 ▶use base 'Spoon::Formatter::WaflBlock';
Subroutine BEGIN: called CC=1 SCAR=0.0
153  
154 0 ▶sub to_html {
Subroutine to_html: not called CC=1 SCAR=6.9
155 0 my $p = YAML::Load($self->block_text);
156 0 $self->hub->page_template->render($p->{template},$p->{values});
157 }
158  
159 package Kwiki::PageTemplate::DisplayBlock;
160 1 ▶use base 'Spoon::Formatter::WaflBlock';
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 0 ▶sub to_html { '' }
Subroutine to_html: not called CC=1 SCAR=6.9
163  
164 package Kwiki::PageTemplate::TemplateBlock;
165 1 ▶use base 'Spoon::Formatter::WaflBlock';
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 0 ▶sub to_html {
Subroutine to_html: not called CC=2 SCAR=17.9
168 0 my $plugin = $self->hub->page_template;
169 my $prefix = $plugin->fields->{page_id_prefix}
170 0 ▶ || $self->hub->config->page_template_page_id_prefix;
Condition0%0/3
$plugin->fields->{'page_id_prefix'} || $self->hub->config->page_template_page_id_prefix
l!l&&r!l&&!r
000
MC/DC0%0/2
$plugin->fields->{'page_id_prefix'} || $self->hub->config->page_template_page_id_prefix
$plugin->fields->{'page_id_prefix'} $self->hub->config->page_template_page_id_prefix
Truth table0%0/3
A || B
A= $plugin->fields->{'page_id_prefix'}B= $self->hub->config->page_template_page_id_prefix
ABresult
1X1
011
000
171 0 $plugin->template_process(
172 'page_template_form.html',
173 template_page => $plugin->pages->current->id,
174 content => $self->hub->formatter->text_to_html($self->block_text) ,
175 page_template_page_id_prefix => $prefix
176 );
177 }
178  
179 package Kwiki::PageTemplate::CGI;
180 1 ▶use base 'Kwiki::CGI';
Subroutine BEGIN: called CC=1 SCAR=0.0
181  
182 cgi 'template_page';
183 cgi 'page_id';
184 cgi 'page_id_prefix';
185 cgi 'button' => '-utf8';
186 cgi 'save_to_page';
187  
188 package Kwiki::PageTemplate::FieldPhrase;
189 use base 'Spoon::Formatter::WaflPhrase';
190  
191 # Without default: {field: foo}
192 # Default value: {field: foo, value:bar}
193 # the value of "value:" should be URI-escaped.
194 # Because wafl-phrase can only in a single line.
195 sub to_html {
196 my $fields = $self->hub->page_template->fields;
197 my ($name,$defvalue) = split/\s+,\s*value:\s*/,$self->arguments;
198 my $type = $fields->{$name};
199 $defvalue = $self->uri_unescape($defvalue||'');
200 $name = "field_$name";
201 if($type eq 'textarea') {
202 return qq{<textarea name="$name">$defvalue</textarea>};
203 } elsif (ref($type) eq 'ARRAY') {
204 my $ret = qq{<select name="$name">};
205 my $selected = '';
206 for(@$type) {
207 $selected = 'selected' if($defvalue eq $_);
208 $ret .= qq{<option value="$_" $selected >$_</option>};
209 }
210 $ret .= "</select>";
211 return $ret;
212 }
213 qq{<input type="text" name="$name" value="$defvalue"/>};
214 }
215  
216 package Kwiki::PageTemplate;
217  
218 1;
219  
220 =head1 NAME
221  
222 Kwiki::PageTemplate - pre-fill kwiki page with this template
223  
224 =head1 SYNOPSIS
225  
226 Paste this into your SandBox and visit the SandBox.
227  
228 .page_template_fields
229 page_id_prefix: Resume
230 name: text
231 gender:
232 - Woman
233 - Woman-in-man
234 bio: textarea
235 .page_template_fields
236  
237 .page_template
238 = Resume form
239  
240 My name: {field:name}
241  
242 Email: {field:name}
243  
244 Biograph:
245 {field:bio}
246 .page_template
247  
248 Fill the above form and you will probabally get the job.
249  
250 =head1 DESCRIPTION
251  
252 This purpose of this plugin is to let your Kwiki User edit
253 pages even more easily. They only have to type some characters
254 into a given form, submit it, and done. Not even Kwiki formatter
255 syntax knowledged required.
256  
257 The basic idea is from mac.com hompage editing, they provide a nearly
258 WYSIWYG web interface to edit your homepage, because the have many
259 pr-defined HTML templates, which are a big form, after you submit that
260 form, what you just inputed replace the original input fields, becomes
261 the content of the generated page.
262  
263 The "page_template_fields" wafl block is a YAML block where you can
264 define your form variables, and their input types, if the type is a
265 array, it'll become a pull-down select menu. After user submit the
266 form, this plugin will generate a page prefixed with the value
267 "page_template_page_id_prefix", default to "PageTemplateGenerated" in
268 your config/page_template.yaml, but you may specify "page_id_prefix"
269 in the page_template_fields wafl block to override this. The example
270 given in SYNOPSIS demostrate this feature, let the form generate a
271 page named like "Resume3", the number afterwards are increased
272 automatically each time somebody submit the form.
273  
274 By default, the generated page will preserve same look as in the
275 "page_template" block. But if you want to display the generated page
276 in another look, you may write the template code in
277 "page_template_display" wafl block.
278  
279 This plugin is still in it's early development and currently,
280 re-editing the generated page is not implemented, and something may
281 break in the future. So use it at your on risk.
282  
283 =head1 COPYRIGHT
284  
285 Copyright 2004 by Kang-min Liu <gugod@gugod.org>.
286  
287 This program is free software; you can redistribute it and/or modify
288 it under the same terms as Perl itself.
289  
290 See <http://www.perl.com/perl/misc/Artistic.html>
291  
292 =cut
293  
294 __DATA__
↑ Summary