lib/MojoMojo/Formatter/SyntaxHighlight.pm

filter: Statementstmt 100% 43 / 43 Branchbran 75.0% 9 / 12 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 95.5% 65 / 68 CC 6 SCAR 12.5
CC
6
Cov
95%
format_content
6.0
BEGIN
1.0
BEGIN
1.0
CRAP 3.5
« lib/MojoMojo/WordDiff.pm ↑ Summary lib/MojoMojo/Model/Search.pm »
1 package MojoMojo::Formatter::SyntaxHighlight;
2  
3 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use parent qw/MojoMojo::Formatter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use HTML::Entities;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 26 eval {require Syntax::Highlight::Engine::Kate};
9 26 my $kate_installed = !$@;
10  
11 =head2 module_loaded
12  
13 Return true if the module is loaded.
14  
15 =cut
16  
17 171 ▶sub module_loaded { $kate_installed }
Subroutine module_loaded: called CC=1 SCAR=0.0
Pod documented
18  
19 26 my $main_formatter;
20 26 eval { $main_formatter = MojoMojo->pref('main_formatter'); };
21 26 ▶$main_formatter ||= 'MojoMojo::Formatter::Markdown';
Condition100%2/2
$main_formatter ||= "MojoMojo::Formatter::Markdown"
l!l
1016
MC/DC100%1/1
$main_formatter ||= "MojoMojo::Formatter::Markdown"
$main_formatter
Truth table100%2/2
A
A= $main_formatter
Aresult
11
00
22  
23 =head1 NAME
24  
25 MojoMojo::Formatter::SyntaxHighlight - syntax highlighting for code blocks
26  
27 =head1 DESCRIPTION
28  
29 This formatter performs syntax highlighting on code blocks.
30  
31 =head1 METHODS
32  
33 =head2 format_content_order
34  
35 The syntax highlight formatter is based on C<< <pre> >> tags entered by the
36 user, so it must run before other formatters that produce C<< <pre> >> tags.
37 The earliest such formatter is the main formatter.
38  
39 =cut
40  
41 992 ▶sub format_content_order { 14 }
Subroutine format_content_order: called CC=1 SCAR=0.0
Pod documented
42  
43 =head2 format_content
44  
45 This formatter uses L<Syntax::Highlight::Engine::Kate> to syntax highlight code
46 inside of C<< <pre lang="language"> ... </pre> >> tags:
47  
48 <pre lang="Perl">
49 say "Hello world!";
50 </pre>
51  
52 See L<Syntax::Highlight::Engine::Kate/PLUGINS> for a list of supported
53 languages.
54  
55 =cut
56  
57 # The $kate formatter is scoped outside of format_content. Otherwise, memory
58 # leaks have occurred. This is also faster, as it avoids instantiation for every
59 # request.
60 26 my $kate;
61  
62 130 ▶sub format_content {
Subroutine format_content: called CC=6 SCAR=17.9
Pod documented
63 130 my ( $class, $content ) = @_;
64 130 ▶ return unless $class->module_loaded;
Branch: 1 branch
BranchTrueFalseTotal
unless $class->module_loaded 0 130 130
65  
66 130 my @blocks = ();
67 130 my $ph = 0;
68 130 my $ph_base = __PACKAGE__ . '::PlaceHolder::';
69  
70 # new school - consistent with other new syntax, but broke for me to the point of exhaustion
71 # $$content =~ s/\{\{\s*code\s+lang=""\s*\}\}/<pre>/g;
72 # while ( $$content =~ s/\{\{\s*code(?:\s+lang=['"]*(.*?)['"]*")?\s*\}\}(.*?)\{\{\s*end\s*\}\}/$ph_base$ph/si ) {
73 # drop all lang="" -- mateu
74 130 $$content =~ s/<\s*pre\s+lang=""\s*>/<pre>/g;
75 130 ▶ while ( $$content =~ s/<\s*pre(?:\s+lang=['"]*(.*?)['"]*")?\s*>(.*?)<\s*\/pre\s*>/$ph_base$ph/si ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$content =~ s[<\s*pre(?:\s+lang=['"]*(.*?)['"]*")?\s*>(.*?)<\s*/pre\s*>][$ph_base$ph]is) 37 130 167
76 37 my ( $language, $block ) = ( $1, $2 );
77  
78 # Fix newline issue
79 37 $block =~ s/\r//g;
80  
81 37 ▶ if ($language) {
Branch: 1 branch
BranchTrueFalseTotal
if ($language) 32 5 37
82 eval {
83 32 $kate->language($language);
84 32 ▶ } and do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $kate->language($language) } } and do { $block = $kate->highlightText($block) } 32 0 32
85 32 $block = $kate->highlightText($block);
86 }
87 }
88 37 push @blocks, $block;
89 37 $ph++;
90 }
91  
92 130 ▶ for ( my $i = 0 ; $i < $ph ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $ph) 37 130 167
93 37 $$content =~ s/$ph_base$i/<pre>$blocks[$i]<\/pre>/;
94 }
95  
96 130 return $content;
97 }
98  
99 26 ▶if (module_loaded) {
Branch: 1 branch
BranchTrueFalseTotal
if (module_loaded()) 26 0 26
100 26 $kate = Syntax::Highlight::Engine::Kate->new(
101 language => 'Perl',
102 substitutions => {
103 "<" => "&lt;",
104 ">" => "&gt;",
105 "&" => "&amp;",
106 "^" => "&circ;",
107 " " => "&nbsp;",
108 "\t" => "&nbsp;&nbsp;&nbsp;&nbsp;",
109 "\n" => "\n",
110 },
111 format_table => {
112 Alert => [ q{<span class="kateAlert">}, "</span>" ],
113 BaseN => [ q{<span class="kateBaseN">}, "</span>" ],
114 BString => [ q{<span class="kateBString">}, "</span>" ],
115 Char => [ q{<span class="kateChar">}, "</span>" ],
116 Comment => [ q{<span class="kateComment"><i>}, "</i></span>" ],
117 DataType => [ q{<span class="kateDataType">}, "</span>" ],
118 DecVal => [ q{<span class="kateDecVal">}, "</span>" ],
119 Error => [ q{<span class="kateError"><b><i>}, "</i></b></span>" ],
120 Float => [ q{<span class="kateFloat">}, "</span>" ],
121 Function => [ q{<span class="kateFunction">}, "</span>" ],
122 IString => [ q{<span class="kateIString">}, "" ],
123 Keyword => [ q{<b>}, "</b>" ],
124 Normal => [ q{}, "" ],
125 Operator => [ q{<span class="kateOperator">}, "</span>" ],
126 Others => [ q{<span class="kateOthers">}, "</span>" ],
127 RegionMarker => [ q{<span class="kateRegionMarker"><i>}, "</i></span>" ],
128 Reserved => [ q{<span class="kateReserved"><b>}, "</b></span>" ],
129 String => [ q{<span class="kateString">}, "</span>" ],
130 Variable => [ q{<span class="kateVariable"><b>}, "</b></span>" ],
131 Warning => [ q{<span class="kateWarning"><b><i>}, "</b></i></span>" ],
132 },
133 );
134 }
135  
136 =head1 SEE ALSO
137  
138 L<MojoMojo>, L<Module::Pluggable::Ordered> and L<Syntax::Highlight::Engine::Kate>.
139  
140 =head1 AUTHORS
141  
142 Johannes Plunien E<lt>plu@cpan.orgE<gt>
143  
144 =head1 LICENSE
145  
146 This library is free software. You can redistribute it and/or modify
147 it under the same terms as Perl itself.
148  
149 =cut
150  
151 26 1;
« lib/MojoMojo/WordDiff.pm ↑ Summary lib/MojoMojo/Model/Search.pm »