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

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 145 CC 10 SCAR 30.8
CC
10
Cov
0%
yukkilink
56.0
yukkiplugin
12.0
yukkitext
6.0
CRAP 21.8
1 package Yukki::Web::Plugin::YukkiText;
2 $Yukki::Web::Plugin::YukkiText::VERSION = '0.991_007'; # TRIAL
3 $Yukki::Web::Plugin::YukkiText::VERSION = '0.991007';
4 use v5.24;
5 use utf8;
6 use Moo;
7  
8 use Type::Utils;
9 use Types::Standard qw( HashRef Str );
10  
11 extends 'Yukki::Web::Plugin';
12  
13 # ABSTRACT: format text/yukki files using markdown, etc.
14  
15 use Text::MultiMarkdown;
16 use Try::Tiny;
17  
18 use namespace::clean;
19  
20  
21 has html_formatters => (
22 is => 'ro',
23 isa => HashRef[Str],
24 required => 1,
25 default => sub { +{
26 'text/yukki' => 'yukkitext',
27 } },
28 );
29  
30 with 'Yukki::Web::Plugin::Role::Formatter';
31  
32  
33 has markdown => (
34 is => 'ro',
35 isa => class_type('Text::MultiMarkdown'),
36 required => 1,
37 lazy => 1,
38 builder => '_build_markdown',
39 handles => {
40 'format_markdown' => 'markdown',
41 },
42 );
43  
44 sub _build_markdown {
45 Text::MultiMarkdown->new(
46 markdown_in_html_blocks => 1,
47 heading_ids => 0,
48 strip_metadata => 1,
49 );
50 }
51  
52  
53 sub yukkilink {
54 my ($self, $params) = @_;
55  
56 my $file = $params->{file};
57 my $ctx = $params->{context};
58 my $repository = $file->repository_name;
59 my $link = $params->{link};
60 my $label = $params->{label};
61  
62 $link =~ s/^\s+//; $link =~ s/\s+$//;
63  
64 my ($repo_name, $local_link) = split /:/, $link, 2 if $link =~ /:/;
65 if (defined $repo_name and defined $self->app->settings->{repositories}{$repo_name}) {
66 $repository = $repo_name;
67 $link = $local_link;
68 }
69  
70 # If we did not get a label, make the label into the link
71 if (not defined $label) {
72 ($label) = $link =~ m{([^/]+)$};
73 $link = $self->app->munge_label($link);
74 }
75  
76 my @base_name;
77 if ($file->full_path) {
78 $base_name[0] = $file->full_path;
79 $base_name[0] =~ s/\.yukki$//g;
80 }
81  
82 $link = join '/', @base_name, $link if $link =~ m{^\./};
83 $link =~ s{^/}{};
84 $link =~ s{/\./}{/}g;
85  
86 $label =~ s/^\s*//; $label =~ s/\s*$//;
87  
88 my $b = sub { $ctx->rebase_url($_[0]) };
89  
90 my $link_repo = $self->model('Repository', { name => $repository });
91 my $link_file = $link_repo->file({ full_path => $link });
92  
93 my $class = $link_file->exists ? 'exists' : 'not-exists';
94 return qq{<a class="$class" href="}.$b->("page/view/$repository/$link").qq{">$label</a>};
95 }
96  
97  
98 sub yukkiplugin {
99 my ($self, $params) = @_;
100  
101 my $ctx = $params->{context};
102 my $plugin_name = $params->{plugin_name};
103 my $arg = $params->{arg};
104  
105 my $text;
106  
107 my @plugins = $self->app->format_helper_plugins;
108 PLUGIN: for my $plugin (@plugins) {
109 my $helpers = $plugin->format_helpers;
110 if (defined $helpers->{ $plugin_name }) {
111 $text = try {
112 my $helper = $helpers->{ $plugin_name };
113 $plugin->$helper({
114 context => $ctx,
115 file => $params->{file},
116 helper_name => $plugin_name,
117 arg => $arg,
118 });
119 }
120  
121 catch {
122 warn "Plugin Error: $_";
123 };
124  
125 last PLUGIN if defined $text;
126 }
127 }
128  
129 $text //= "{{$plugin_name:$arg}}";
130 return $text;
131 }
132  
133  
134 sub yukkitext {
135 my ($self, $params) = @_;
136  
137 my $file = $params->{file};
138 my $position = 0 + ($params->{position} // -1);
139 my $repository = $file->repository_name;
140 my $yukkitext = $file->fetch // '';
141  
142 $yukkitext =~ s[(.{$position}.*?)$][$1<span id="yukkitext-caret"></span>]sm
143 if $position >= 0;
144  
145 # Yukki Links
146 $yukkitext =~ s{
147 (?<!\\) # \ will escape the link
148 \[\[ \s* # [[ to start it
149  
150 (?: ([\w]+) : )? # repository: is optional
151 ([^|\]]+) \s* # link/to/page is mandatory
152  
153 (?: \| # | to split link from label
154 ([^\]]+) # a pretty label (needs trimming)
155 )? # is optional
156  
157 \]\] # ]] to end
158 }{
159 $self->yukkilink({
160 %$params,
161  
162 repository => $1 // $repository,
163 link => $2,
164 label => $3,
165 });
166 }xeg;
167  
168 # Handle escaped links, hide the escape
169 $yukkitext =~ s{
170 \\ # \ will escape the link
171 (\[\[ \s* # [[ to start it
172  
173 (?: [\w]+ : )? # repository: is optional
174 [^|\]]+ \s* # link/to/page is mandatory
175  
176 (?: \| # | to split link from label
177 [^\]]+ # a pretty label (needs trimming)
178 )? # is optional
179  
180 \]\]) # ]] to end
181 }{$1}gx;
182  
183 # Yukki Plugins
184 $yukkitext =~ s{
185 (?<!\\) # \ will escape the plugin
186 \{\{ \s* # {{ to start it
187  
188 ([^:]+) : # plugin_name: is required
189  
190 (.*?) # plugin arguments
191  
192 \}\} # }} to end
193 }{
194 $self->yukkiplugin({
195 %$params,
196  
197 plugin_name => $1,
198 arg => $2,
199 });
200 }xegms;
201  
202 # Handle the escaped plugin thing
203 $yukkitext =~ s{
204 \\ # \ will escape the plugin
205 (\{\{ \s* # {{ to start it
206  
207 [^:]+ : # plugin_name: is required
208  
209 .*? # plugin arguments
210  
211 \}\}) # }} to end
212 }{$1}xgms;
213  
214 my $formatted = '<div>' . $self->format_markdown($yukkitext) . '</div>';
215  
216 # Just in case markdown mangled the caret marker:
217 $formatted =~ s[&lt;span id="yukkitext-caret"&gt;&lt;/span&gt;]
218 [<span id="yukkitext-caret"></span>];
219  
220 return $formatted;
221 }
222  
223 1;
224  
225 __END__