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

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