blib/lib/Yukki/Web/Plugin/SyntaxHighlight.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 80 SCAR 24.8
CC
3
Cov
0%
highlight_syntax
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 12.0
1 package Yukki::Web::Plugin::SyntaxHighlight;
2 $Yukki::Web::Plugin::SyntaxHighlight::VERSION = '0.991_002'; # TRIAL
3  
4 $Yukki::Web::Plugin::SyntaxHighlight::VERSION = '0.991002';use v5.24;
5 use utf8;
6 use Moo;
7  
8 use Types::Standard qw( HashRef Str );
9  
10 use namespace::clean;
11  
12 extends 'Yukki::Web::Plugin';
13  
14 # ABSTRACT: add syntax highlighting to wiki pages
15  
16 use Syntax::Highlight::Engine::Kate;
17 use Syntax::Highlight::Engine::Kate::All;
18 use Syntax::Highlight::Engine::Kate::Perl6;
19  
20  
21 has format_helpers => (
22 is => 'ro',
23 isa => HashRef[Str],
24 required => 1,
25 default => sub { +{
26 'highlight' => 'highlight_syntax',
27 'highlight-div' => 'highlight_syntax',
28 'highlight-span' => 'highlight_syntax',
29 } },
30 );
31  
32 with 'Yukki::Web::Plugin::Role::FormatHelper';
33  
34  
35 sub highlight_syntax {
36 my ($self, $params) = @_;
37  
38 my $arg = $params->{arg};
39 my ($language, $text) = split /:/, $arg, 2;
40  
41 my $engine = Syntax::Highlight::Engine::Kate->new(
42 substitutions => {
43 "<" => "&#x3c;",
44 ">" => "&#x3e;",
45 "&" => "&#x26;",
46 " " => " ",
47 "\t" => " " x 4,
48 "\n" => "\n",
49 },
50 format_table => {
51 Alert => [q[<span class="syntax-alert">], q[</span>]],
52 BaseN => [q[<span class="syntax-base-n">], q[</span>]],
53 BString => [q[<span class="syntax-b-string">], q[</span>]],
54 Char => [q[<span class="syntax-char">], q[</span>]],
55 Comment => [q[<span class="syntax-comment">], q[</span>]],
56 DataType => [q[<span class="syntax-data-type">], q[</span>]],
57 DecVal => [q[<span class="syntax-dec-val">], q[</span>]],
58 Error => [q[<span class="syntax-error">], q[</span>]],
59 Float => [q[<span class="syntax-float">], q[</span>]],
60 Function => [q[<span class="syntax-function">], q[</span>]],
61 IString => [q[<span class="syntax-i-string">], q[</span>]],
62 Keyword => [q[<span class="syntax-keyword">], q[</span>]],
63 Normal => [q[], q[] ],
64 Operator => [q[<span class="syntax-operator">], q[</span>]],
65 Others => [q[<span class="syntax-others">], q[</span>]],
66 RegionMarker => [q[<span class="syntax-region-marker">], q[</span>]],
67 Reserved => [q[<span class="syntax-reserved">], q[</span>]],
68 String => [q[<span class="syntax-string">], q[</span>]],
69 Variable => [q[<span class="syntax-variable">], q[</span>]],
70 Warning => [q[<span class="syntax-warning">], q[</span>]],
71 },
72 );
73  
74 # HACK BECAUSE KATE HIGHLIGHTER SUCKS
75 $engine->{'syntaxes'}{'Perl6'} = 'Perl6';
76  
77 $engine->language($language);
78  
79 my $highlighted_text = $engine->highlightText($text);
80  
81 if ($params->{helper_name} =~ /highlight-(div|span)/) {
82 my $element = $1;
83 $element = 'pre' if $element eq 'div';
84 $highlighted_text
85 = qq[<$element class="syntax-highlight language-$language">]
86 . $engine->highlightText($text)
87 . qq[</$element>];
88 }
89  
90 return $highlighted_text;
91 }
92  
93 1;
94  
95 __END__
96  
97 =pod
98  
99 =encoding UTF-8
100  
101 =head1 NAME
102  
103 Yukki::Web::Plugin::SyntaxHighlight - add syntax highlighting to wiki pages
104  
105 =head1 VERSION
106  
107 version 0.991_002
108  
109 =head1 SYNOPSIS
110  
111 <div>{{highlight:Perl:
112 use v5.14;
113 use Moo;
114  
115 has something => ( is => 'ro' );
116 }}</div>
117  
118 <span>{{highlight:JavaScript:window.location.hash = '#foo'}}</span>
119  
120 {{highlight-div:Perl:
121 use v5.14;
122 use Moo;
123  
124 has something => ( is => 'ro' );
125 }}
126  
127 {{highlight-span:JavaScript:window.location.hash = '#foo'}}
128  
129 =head1 DESCRIPTION
130  
131 Performs syntax highlighting of text. This is able to highlight all the file types listed here with teh L<Syntax::Highlight::Engine::Kate> module:
132  
133 =over
134  
135 =item *
136  
137 L<https://metacpan.org/module/Syntax::Highlight::Engine::Kate#PLUGINS>
138  
139 =back
140  
141 =head1 ATTRIBUTES
142  
143 =head2 format_helpers
144  
145 Sets up the "highlight" helper.
146  
147 =head1 METHODS
148  
149 =head2 highlight_syntax
150  
151 This is used to format the double-curly brace C<< {{highlight:...}} >>. Do not use.
152  
153 =head2 highlight_syntax_asis
154  
155 This is used to format the double-curly brace C<< {{highlight-asis:...}} >>. Do not use.
156  
157 =head1 AUTHOR
158  
159 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
160  
161 =head1 COPYRIGHT AND LICENSE
162  
163 This software is copyright (c) 2017 by Qubling Software LLC.
164  
165 This is free software; you can redistribute it and/or modify it under
166 the same terms as the Perl 5 programming language system itself.
167  
168 =cut