ConvertText2/Plugin/Text.pm

filter: Statementstmt 90.0% 18 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.5% 25 / 27 CC 1 SCAR 0.4
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« ConvertText2/Plugin/Barcode.pm ↑ Summary ConvertText2/Support.pm »
1  
2 =head1 NAME
3  
4 App::Basis::ConvertText2::Plugin::Text
5  
6 =head1 SYNOPSIS
7  
8 Handle a few simple text code blocks
9  
10 my $obj = App::Basis::ConvertText2::Plugin::Text->new() ;
11 my $content = "" ;
12 my $params = { } ;
13 # new page
14 my $out = $obj->process( 'page', $content, $params) ;
15  
16 # yamlasjson
17 $content = "list:
18 - array: [1,2,3,7]
19 channel: BBC3
20 date: 2013-10-20
21 time: 20:30
22 - array: [1,2,3,9]
23 channel: BBC4
24 date: 2013-11-20
25 time: 21:00
26 " ;
27 $out = $obj->process( 'yamlasjson', $content, $params) ;
28  
29 # table
30 $content = "row1,entry 1,cell2
31 row2,cell1, entry 2
32 " ;
33 $out = $obj->process( 'table', $content, $params) ;
34  
35 # version
36 $content = "0.1 2014-04-12
37 * removed ConvertFile.pm
38 * using Path::Tiny rather than other things
39 * changed to use pandoc fences ~~~~{.tag} rather than xml format <tag>
40 0.006 2014-04-10
41 * first release to github" ;
42 $out = $obj->process( 'table', $content, $params) ;
43  
44 $content = "BBC | http://bbc.co.uk
45 DocumentReference | #docreference
46 27escape | https://github.com/27escape" ;
47 $out = $obj->process( 'table', $content, $params) ;
48  
49 =head1 DESCRIPTION
50  
51 Various simple text transformations
52  
53 =cut
54  
55 # ----------------------------------------------------------------------------
56  
57 package App::Basis::ConvertText2::Plugin::Text;
58 $App::Basis::ConvertText2::Plugin::Text::VERSION = '0.4';
59 1 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 1 ▶use YAML qw(Load);
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 0 ▶use App::Basis::ConvertText2::Support;
Subroutine BEGIN: called CC=1 SCAR=2.6
67 use namespace::clean;
68  
69 has handles => (
70 is => 'ro',
71 init_arg => undef,
72 default => sub { [qw{yamlasjson table version page links}] }
73 );
74  
75 # ----------------------------------------------------------------------------
76  
77 =item yamlasjson
78  
79 Convert a YAML block into a JSON block
80  
81 parameters
82  
83 =cut
84  
85 sub yamlasjson {
86 my $self = shift;
87 my ( $tag, $content, $params, $cachedir ) = @_;
88  
89 # make sure we have an extra linefeed at the end to make sure
90 # YAML is correct
91 $content .= "\n\n" ;
92  
93 $content =~ s/~~~~{\.yaml}//gsm;
94 $content =~ s/~~~~//gsm;
95  
96 my $data = Load($content);
97 return "\n~~~~{.json}\n" . to_json( $data, { utf8 => 1, pretty => 1 } ) . "\n~~~~\n\n";
98 }
99  
100 # ----------------------------------------------------------------------------
101  
102 sub _split_csv_data {
103 my ( $data, $separator ) = @_;
104 my @d = ();
105  
106 $separator ||= ',';
107  
108 my $j = 0;
109 foreach my $line ( split( /\n/, $data ) ) {
110 last if ( !$line );
111 my @row = split( /$separator/, $line );
112  
113 for ( my $i = 0; $i <= $#row; $i++ ) {
114 undef $row[$i] if ( $row[$i] eq 'undef' );
115  
116 # dont' bother with any zero values either
117 undef $row[$i] if ( $row[$i] =~ /^0\.?0?$/ );
118 push @{ $d[$j] }, $row[$i];
119 }
120 $j++;
121 }
122  
123 return @d;
124 }
125  
126 # ----------------------------------------------------------------------------
127  
128 =item table
129  
130 create a basic html table
131  
132 parameters
133 data - comma separated lines of table data
134  
135 hashref params of
136 class - HTML/CSS class name
137 id - HTML/CSS class
138 width - width of the table
139 style - style the table if not doing anything else
140 legends - flag to indicate that the top row is the legends
141 separator - characters to be used to separate the fields
142  
143 =cut
144  
145 sub table {
146 my $self = shift;
147 my ( $tag, $content, $params, $cachedir ) = @_;
148  
149 $params->{title} ||= "";
150  
151 $content =~ s/^\n//gsm;
152 $content =~ s/\n$//gsm;
153  
154 # open the csv file, read contents, calc max, add into data array
155 my @data = _split_csv_data( $content, $params->{separator} );
156  
157 my $out = "<table ";
158 $out .= "class='$params->{class}' " if ( $params->{class} );
159 $out .= "id='$params->{id}' " if ( $params->{id} );
160 $out .= "width='$params->{width}' " if ( $params->{width} );
161 $out .= "class='$params->{style}' " if ( $params->{style} );
162 $out .= ">\n";
163  
164 for ( my $i = 0; $i < scalar(@data); $i++ ) {
165 $out .= "<tr>";
166  
167 # decide if the top row has the legends
168 my $tag = ( !$i && $params->{legends} ) ? 'th' : 'td';
169 map { $out .= "<$tag>$_</$tag>"; } @{ $data[$i] };
170 $out .= "</tr>\n";
171 }
172  
173 $out .= "</table>\n";
174 return $out;
175 }
176  
177 # ----------------------------------------------------------------------------
178  
179 =item version
180  
181 create a version table
182  
183 parameters
184 data - sections of version information
185 version YYYY-MM-DD
186 change text
187 more changes
188  
189  
190 hashref params of
191 class - HTML/CSS class name
192 id - HTML/CSS class
193 width - width of the table
194 style - style the table if not doing anything else
195 separator - characters to be used to separate the fields
196  
197 =cut
198  
199 sub version {
200 my $self = shift;
201 my ( $tag, $content, $params, $cachedir ) = @_;
202  
203 $content =~ s/^\n//gsm;
204 $content =~ s/\n$//gsm;
205  
206 my $out = "<table ";
207 $out .= "class='$params->{class}' " if ( $params->{class} );
208 $out .= "id='$params->{id}' " if ( $params->{id} );
209 $out .= "width='$params->{width}' " if ( $params->{width} );
210 $out .= "class='$params->{style}' " if ( $params->{style} );
211 $out .= ">\n";
212  
213 $out .= "<tr><th>Version</th><th>Date</th><th>Changes</th></tr>\n";
214  
215 my $section = '^(.*?)\s+(\d{2,4}[-\/]\d{2}[-\/]\d{2,4})' ;
216  
217 my @data = split( /\n/, $content );
218 for ( my $i = 0; $i < scalar(@data); $i++ ) {
219 if ( $data[$i] =~ /$section/ ) {
220 my $vers = $1;
221 my $date = $2;
222 $i++;
223 my $c = "";
224  
225 # get all the lines in this section
226 while ( $i < scalar(@data) && $data[$i] !~ /$section/ ) {
227 $c .= "$data[$i]\n";
228 $i++;
229 }
230 $out .= "<tr><td valign='top'>$vers</td><td valign='top'>$date</td><td valign='top'>$c</td></tr>\n";
231 # adjust $i back so we are either at the wnd correctly or on the next section
232 $i-- ;
233 }
234 }
235  
236 $out .= "</table>\n";
237 return $out;
238 }
239  
240 # ----------------------------------------------------------------------------
241  
242 # start a new HTML page
243  
244 sub page {
245 my $self = shift;
246 my ( $tag, $content, $params, $cachedir ) = @_;
247  
248 return "<div style='page-break-before: always;'></div>" ;
249 }
250  
251  
252 # ----------------------------------------------------------------------------
253  
254 =item ~~~~{.links }
255  
256 create a list of website links
257 links are one per line and the link name is separated from the link with a
258 pipe '|' symbol
259  
260 parameters
261 class - name of class for the list, defaults to weblinks
262  
263 =cut
264  
265 sub links {
266 my $self = shift;
267 my ( $tag, $content, $params, $cachedir ) = @_;
268  
269 # strip any ending linefeed
270 chomp $content;
271 return "" if ( !$content );
272  
273 $params->{class} ||= "weblinks";
274 my $references = "";
275 my $ul = "<ul class='$params->{class}'>\n";
276 my %refs = ();
277 my %uls = ();
278  
279 foreach my $line ( split( /\n/, $content ) ) {
280 my ( $ref, $link ) = split( /\|/, $line );
281 next if ( !$link );
282  
283 # trim the items
284 $ref =~ s/^\s+//;
285 $link =~ s/^\s+//;
286 $ref =~ s/\s+$//;
287 $link =~ s/\s+$//;
288  
289 # if there is nothing to link to ignore this
290 next if ( !$ref || !$link );
291  
292 $references .= "[$ref]: $link\n";
293  
294 # links that reference inside the document do not get added to the
295 # list of weblinks
296 if ( $link !~ /^#/ ) {
297 $uls{ lc($ref) } = "<li><a href='$link'>$ref</a><ul><li>$link</li></ul></li>\n";
298 }
299 }
300  
301 # make them nice and sorted
302 map { $ul .= $uls{$_} } sort keys %uls;
303 $ul .= "</ul>\n";
304  
305 return "\n" . $references . "\n" . $ul . "\n";
306 }
307  
308 # ----------------------------------------------------------------------------
309 # decide which simple hanlder should process this request
310  
311 sub process {
312 my $self = shift;
313 my ( $tag, $content, $params, $cachedir ) = @_;
314  
315 if ( $self->can($tag) ) {
316 return $self->$tag(@_);
317 }
318 return undef;
319 }
320  
321 # ----------------------------------------------------------------------------
322  
323 1;
324  
« ConvertText2/Plugin/Barcode.pm ↑ Summary ConvertText2/Support.pm »