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

filter: Statementstmt 0.0% 0 / 97 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 7 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 138 SCAR 47.0
CC
10
Cov
0%
yukkilink
56.0
yukkiplugin
12.0
yukkitext
6.0
CRAP 110.0
1 package Yukki::Web::Plugin::YukkiText;
2 $Yukki::Web::Plugin::YukkiText::VERSION = '0.991_002'; # TRIAL
3  
4 $Yukki::Web::Plugin::YukkiText::VERSION = '0.991002';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__
226  
227 =pod
228  
229 =encoding UTF-8
230  
231 =head1 NAME
232  
233 Yukki::Web::Plugin::YukkiText - format text/yukki files using markdown, etc.
234  
235 =head1 VERSION
236  
237 version 0.991_002
238  
239 =head1 SYNOPSIS
240  
241 # Plugins are not used directly...
242  
243 my $repo = $self->model('Repository', { name => 'main' });
244 my $file = $repo->file({ full_path => "some-file.yukki' });
245 my $html = $file->fetch_formatted($ctx);
246  
247 =head1 DESCRIPTION
248  
249 Yukkitext formatting is based on Multi-Markdown, which is an extension to regular markdown that adds tables, metadata, and a few other tidbits. In addition to this, yukkitext adds linking using double-bracket notation:
250  
251 [[ A Link ]]
252 [[ ./A Sub-Page Link ]]
253 [[ ./A Sub-Dir/Sub-Page Link ]]
254 [[ ./a-sub-dir/sub-page-link.pdf | Sub-Page PDF ]]
255  
256 This link format is based loosely upon the format used by MojoMojo, which I was using prior to developing Yukki.
257  
258 It also adds support for format helpers usinga double-curly brace notation:
259  
260 {{attachment:Path/To/Attachment.pdf}}
261 {{=:5 + 5}}
262  
263 =head1 ATTRIBUTES
264  
265 =head2 html_formatters
266  
267 This returns the yukkitext formatter for "text/yukki".
268  
269 =head2 markdown
270  
271 This is the L<Text::MultiMarkdown> object for rendering L</yukkitext>. Do not
272 use.
273  
274 Provides a C<format_markdown> method delegated to C<markdown>. Do not use.
275  
276 =head1 METHODS
277  
278 =head2 yukkilink
279  
280 Used to help render yukkilinks. Do not use.
281  
282 =head2 yukkiplugin
283  
284 Used to render plugged in markup. Do not use.
285  
286 =head2 yukkitext
287  
288 my $html = $view->yukkitext({
289 context => $ctx,
290 repository => $repository_name,
291 page => $page,
292 file => $file,
293 });
294  
295 Yukkitext is markdown plus some extra stuff. The extra stuff is:
296  
297 [[ main:/link/to/page.yukki | Link Title ]] - wiki link
298 [[ /link/to/page.yukki | Link Title ]] - wiki link
299 [[ /link/to/page.yukki ]] - wiki link
300  
301 {{attachment:file.pdf}} - attachment URL
302  
303 =head1 AUTHOR
304  
305 Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
306  
307 =head1 COPYRIGHT AND LICENSE
308  
309 This software is copyright (c) 2017 by Qubling Software LLC.
310  
311 This is free software; you can redistribute it and/or modify it under
312 the same terms as the Perl 5 programming language system itself.
313  
314 =cut