Mojito/Page/Render.pm

filter: Statementstmt 31.2% 46 / 147 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 52.0% 13 / 25 Podpod 87.5% 7 / 8 totaltotal 31.4% 66 / 210 CC 15 SCAR 23.8
CC
15
Cov
27%
render_page
30.0
intro_text
30.0
render_section
12.0
CRAP 10.8
« Mojito/Auth/Deep.pm ↑ Summary Mojito/Page/CRUD/Deep.pm »
1 3 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Mojito::Page::Render;
3 3 $Mojito::Page::Render::VERSION = '0.25';
4 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Mojito::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Mojito::Filter::MojoMojo::Converter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Text::Textile;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Text::MultiMarkdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Text::WikiCreole;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Pod::Simple::XHTML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use HTML::Strip;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 with('Mojito::Filter::Shortcuts');
16 3 with('Mojito::Role::Config');
17  
18 =head1 Name
19  
20 Mojito::Page::Render - turn a parsed page into html
21  
22 =cut
23  
24 has tmpl => (
25 is => 'ro',
26 lazy => 1,
27 # pass the config
28 0 ▶ 'default' => sub { Mojito::Template->new(config => shift->config) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
29 3 );
30 has textile => (
31 is => 'ro',
32 lazy => 1,
33 0 ▶ 'default' => sub { Text::Textile->new },
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 3 );
35 has markdown => (
36 is => 'ro',
37 lazy => 1,
38 0 ▶ 'default' => sub { return Text::MultiMarkdown->new }
Subroutine __ANON__: not called CC=1 SCAR=6.9
39 3 );
40  
41 3 has base_url => ( is => 'rw', );
42  
43 3 has 'stripper' => (
44 is => 'ro',
45  
46 # isa => 'HTML::Strip';
47 lazy => 1,
48 builder => '_build_stripper',
49 );
50  
51 has 'to_format' => (
52 is => 'rw',
53 1 ▶ 'default' => sub { 'HTML' },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 3 );
55  
56 =head2 render_sections
57  
58 Turn the sections into something viewable in a HTML browser.
59 Be mindful of the section class which we'll use for formatting purposes.
60  
61 =cut
62  
63 0 ▶sub render_sections {
Subroutine render_sections: not called CC=2 SCAR=17.9
Pod documented
64 0 my ( $self, $doc ) = @_;
65  
66 0 my ( @formatted_document_sections );
67 0 foreach my $section ( @{ $doc->{sections} } ) {
68 0 push @formatted_document_sections, $self->render_section($section, $doc->{default_format});
69 }
70  
71 0 return \@formatted_document_sections;
72 }
73  
74 0 ▶sub render_section {
Subroutine render_section: not called CC=3 SCAR=24.8
Pod undocumented
75 0 my ($self, $section, $default_format) = @_;
76
77 # The class of section is used to determine what the source code format is for the section.
78 # We want to use this knowledge when we render to HTML.
79 my $from_format = ($section->{class} && ($section->{class} ne 'Implicit'))
80 ? $section->{class}
81 0 ▶ : $default_format;
Branch: 1 branch
BranchTrueFalseTotal
$section->{'class'} && $section->{'class'} ne 'Implicit' ? : 0 0 0
Condition0%0/3
$section->{"class"} and $section->{"class"} ne "Implicit"
!ll&&!rl&&r
000
MC/DC0%0/2
$section->{"class"} and $section->{"class"} ne "Implicit"
$section->{"class"} $section->{"class"} ne "Implicit"
Truth table0%0/3
A and B
A= $section->{"class"}B= $section->{"class"} ne "Implicit"
ABresult
0X0
100
111
82 0 return $self->render_content( $section->{content}, $from_format, $self->to_format );
83 }
84  
85 =head2 render_page
86  
87 Make a page for viewing in the browser.
88  
89 =cut
90  
91 0 ▶sub render_page {
Subroutine render_page: not called CC=5 SCAR=34.0
Pod documented
92 0 my ( $self, $doc ) = @_;
93  
94 0 ▶ return 'page is not available' if !$doc;
Branch: 1 branch
BranchTrueFalseTotal
unless $doc 0 0 0
95 # Give the tmpl object a base url first before asking for the html template.
96 0 my $base_url = $self->base_url;
97 0 $self->tmpl->base_url($base_url);
98 # Give the template a page id if it exists
99 0 $self->tmpl->page_id($doc->{'_id'});
100 0 my $page = $self->tmpl->template;
101  
102 0 ▶ if (my $title = $doc->{title}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $title = $doc->{"title"}) 0 0 0
103 0 $page =~ s/<title>.*?<\/title>/<title>${title}<\/title>/si;
104 }
105  
106 0 my $rendered_body = $self->render_body($doc);
107 # Remove edit area
108 0 $page =~ s/(<section id="edit_area"[^>]*>).*?(<\/section>)//si;
109 # Insert rendered page into view area
110 0 $page =~ s/(<section id="view_area"[^>]*>).*?(<\/section>)/$1${rendered_body}$2/si;
111  
112 0 ▶ if ( my $id = $doc->{'_id'}||$doc->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $doc->{"_id"} || $doc->{"id"}) 0 0 0
Condition0%0/3
$doc->{'_id'} || $doc->{'id'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$doc->{'_id'} || $doc->{'id'}
$doc->{'_id'} $doc->{'id'}
Truth table0%0/3
A || B
A= $doc->{'_id'}B= $doc->{'id'}
ABresult
1X1
011
000
113 0 $page =~ s/(<nav id="edit_link"[^>]*>).*?(<\/nav>)/$1<a href="${base_url}page\/${id}\/edit">Edit<\/a>$2/sig;
114 }
115  
116 0 return $page;
117 }
118  
119 =head2 render_body
120  
121 Turn the raw into something distilled.
122 TODO: Do we really need to return two things when only one is used?
123  
124 =cut
125  
126 0 ▶sub render_body {
Subroutine render_body: not called CC=2 SCAR=17.9
Pod documented
127 0 my ( $self, $doc ) = @_;
128  
129 0 my $rendered_sections = $self->render_sections($doc);
130 0 my $rendered_body = join "\n", @{$rendered_sections};
131  
132 0 $rendered_body = $self->expand_shortcuts($rendered_body);
133 # Convert MojoMojo content if needed
134 0 ▶ if ($self->config->{convert_mojomojo}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->config->{"convert_mojomojo"}) 0 0 0
135 0 $rendered_body = Mojito::Filter::MojoMojo::Converter->new( content => $rendered_body )->convert_content;
136 }
137 0 return $rendered_body;
138 }
139  
140 =head2 render_content
141  
142 Given some $content, a source and target format, convert the source to the target.
143 Example: textile => HTML
144  
145 =cut
146  
147 0 ▶sub render_content {
Subroutine render_content: not called CC=3 SCAR=24.8
Pod documented
148 0 my ( $self, $content, $from_format, $to_format ) = @_;
149
150 0 ▶ if ( !$content ) { die "Error: no content going from: $from_format to: $to_format"; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($content) 0 0 0
151 0 my $formatted_content;
152
153 # TODO: This should be a dispatch table
154 # In addition to HTML we'd like epub and PDF outputs
155 0 ▶ if ( $to_format eq 'HTML' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_format eq "HTML") 0 0 0
156 0 $formatted_content = $self->format_for_web( $content, $from_format );
157 }
158  
159 0 return $formatted_content;
160 }
161  
162 =head2 format_for_web
163  
164 Given some content and its format, let's convert it to HTML.
165  
166 =cut
167  
168 0 ▶sub format_for_web {
Subroutine format_for_web: not called CC=1 SCAR=5.7
Pod documented
169 0 my ( $self, $content, $from_language ) = @_;
170  
171 # smartmatch throws warnings on 5.18.0+, suppress them
172 3 ▶ no if $] >= 5.018000, warnings => qw(experimental::smartmatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
173 0 my $formatted_content = $content;
174 0 given ($from_language) {
175 0 when (/^HTML$/i) {
176 # pass HTML through as is
177 }
178  
179 0 when (/^POD$/i) {
180 0 $formatted_content = $self->pod2html($content);
181 }
182 0 when (/^textile$/i) {
183 0 $formatted_content = $self->textile->process($content);
184 }
185 0 when (/^markdown$/i) {
186 0 $formatted_content = $self->markdown->markdown($content);
187 }
188 0 when (/^creole$/i) {
189 0 $formatted_content = creole_parse($content);
190 }
191  
192 0 when (/^h$/i) {
193  
194 # Let's do some highlighting
195 0 $formatted_content = "<pre class='prettyprint'>${content}</pre>";
196 }
197 # More highlighting - language specific
198 0 when (/^perl$/i) {
199 0 $formatted_content = "<pre class='sh_perl'>$content</pre>";
200 }
201 0 when (/^js$/i) {
202 0 $formatted_content = "<pre class='sh_javascript'>$content</pre>";
203 }
204 0 when (/^css$/i) {
205 0 $formatted_content = "<pre class='sh_css'>$content</pre>";
206 }
207 0 when (/^sql$/i) {
208 0 $formatted_content = "<pre class='sh_sql'>$content</pre>";
209 }
210 0 when (/^sh$/i) {
211 0 $formatted_content = "<pre class='sh_sh'>$content</pre>";
212 }
213 0 when (/^diff$/i) {
214 0 $formatted_content = "<pre class='sh_diff'>$content</pre>";
215 }
216 0 when (/^(haskell|hs)$/i) {
217 0 $formatted_content = "<pre class='sh_haskell'>$content</pre>";
218 }
219 0 when (/^sh_html$/i) {
220 0 $formatted_content = "<pre class='sh_html'>$content</pre>";
221 }
222 0 when (/^note$/i) {
223 0 $formatted_content = "<div class='note'>$content</div>";
224 }
225 0 default {
226 # pass HTML through as is
227 }
228 }
229 0 return $formatted_content;
230 }
231  
232 =head2 pod2html
233  
234 Turn POD into HTML
235  
236 =cut
237  
238 0 ▶sub pod2html {
Subroutine pod2html: not called CC=1 SCAR=6.9
Pod documented
239 0 my ( $self, $content ) = @_;
240  
241 0 my $converter = Pod::Simple::XHTML->new;
242  
243 # We just want the body content
244 0 $converter->html_header('');
245 0 $converter->html_footer('');
246 0 $converter->output_string( \my $html );
247 0 $converter->parse_string_document($content);
248  
249 0 return $html;
250 }
251  
252 =head2 intro_text
253  
254 Extract the beginning text substring.
255  
256 =cut
257  
258 0 ▶sub intro_text {
Subroutine intro_text: not called CC=5 SCAR=34.0
Pod documented
259 0 my ( $self, $html ) = @_;
260  
261 0 my $title_length_limit = 64;
262 0 my ($title) = $html =~ m/(.*)?\n?/;
263 0 ▶ return '' if !$title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 0 0
264 0 $title = $self->stripper->parse($title);
265 0 ▶ if (length($title) > $title_length_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $title > $title_length_limit) 0 0 0
266 0 my @words = split /\s+/, $title;
267 0 my @title_words;
268 0 my $title_length = 0;
269 0 foreach my $word (@words) {
270 0 ▶ if ($title_length + length($word) <= $title_length_limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title_length + length($word) <= $title_length_limit) { } 0 0 0
271 0 push @title_words, $word;
272 0 $title_length += length($word);
273 }
274 else {
275 0 last;
276 }
277 }
278 0 $title = join ' ', @title_words;
279 0 $title .= ' ...';
280 }
281  
282 0 return $title;
283 }
284  
285 0 ▶sub _build_stripper {
Subroutine _build_stripper: not called CC=1 SCAR=6.9
286 0 my $self = shift;
287  
288 0 return HTML::Strip->new();
289 }
290  
291 3 1;
« Mojito/Auth/Deep.pm ↑ Summary Mojito/Page/CRUD/Deep.pm »