Codit/Plugins/Exporter.pm untested

filter: Statementstmt 0.0% 0 / 322 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 465 CC 41 SCAR 45.4
CC
41
Cov
0%
export2png
342.0
export2html
42.0
iprint
30.0
CRAP 94.0
1 package App::Codit::Plugins::Exporter;
2  
3 =head1 NAME
4  
5 App::Codit::Plugins::Exporter - plugin for App::Codit
6  
7 =cut
8  
9 use strict;
10 use warnings;
11 use Carp;
12 use vars qw( $VERSION );
13 $VERSION = '0.19';
14  
15 require Syntax::Kamelon;
16 require Tk::CodeText::Theme;
17 use Imager;
18 use Imager::Color;
19 use Imager::Font;
20 use Imager::Fill;
21  
22 use base qw( Tk::AppWindow::BaseClasses::Plugin );
23  
24 =head1 DESCRIPTION
25  
26 Export your document to HTML or PNG.
27  
28 =head1 DETAILS
29  
30 Plugin Exporter allows you to export the current selected document
31 to HTML or PNG. You will find two entries for this in the File menu:
32  
33 =over 4
34  
35 =item B<Export to HTML>
36  
37 Exporting to html is done using the standard facilities available in
38 <LSyntax::Kamelon>. We are looking to extend that in future versions.
39  
40 A form will pop up where you can set:
41  
42 =over 4
43  
44 =item B<Output file>
45  
46 A default file is suggested. Change it if you like.
47  
48 =item B<Theme>
49  
50 Pick one of the available themes.
51  
52 =item B<Line numbers>
53  
54 Tick this if you want line numbers displayed in the left column.
55  
56 =item B<Fold markers>
57  
58 Tick this if you want fold markers displayed in the left column.
59  
60 =item B<Scrolled pane>
61  
62 Tick this if you want the content to appear in a scrollable pane.
63  
64 =back
65  
66 =item B<Export to PNG>
67  
68 A form will pop up. All your changes except
69 for Output file are saved. You can set:
70  
71 =over 4
72  
73 =item B<Output file>
74  
75 A default file is suggested. Change it if you like.
76  
77 =item B<Background>
78  
79 Background color of your image. By default the background
80 color of the Tk::CodeText widget is set.
81  
82 =item B<Foreground>
83  
84 Default color of the text in your image. By default the foreground
85 color of the Tk::CodeText widget is set.
86  
87 =item B<Theme file>
88  
89 Select the Tk::CodeText theme file (extension '.ctt') to use for syntax highlighting.
90 By default the theme file used in the Tk::CodeText widget is set. You can create your
91 custom theme file through Menu->Codit->Settings and select the highlighting panel.
92  
93 =item B<Font>
94  
95 Set the font used to draw text in your image. By default the font
96 of the Tk::CodeText widget is set.
97  
98 =item B<Line numbers>
99  
100 Tick this if you want line numbers displayed in the left column.
101  
102 =item B<Tab size>
103  
104 Set the size of tabs in characters.
105  
106 =item B<Max width>
107  
108 If set to 0 the image will grow in width whenever needed.
109 Otherwise it will wrap text when the max width (in pixels) has been reached.
110  
111 =item B<Margins>
112  
113 Set the top, left, bottom and right margins around your image.
114  
115 =back
116  
117 Beware! Exporting to PNG is painstakingly slow.
118  
119 =back
120  
121 =cut
122  
123 sub new {
124 my $class = shift;
125 my $self = $class->SUPER::new(@_);
126 return undef unless defined $self;
127
128 $self->{BACKGROUND} = undef;
129 $self->{IMAGE} = undef;
130 $self->{IHEIGHT} = 0;
131 $self->{IWIDTH} = 0;
132 $self->{MAXWIDTH} = 0;
133 $self->{XPOS} = 0;
134 $self->{TABSTRING} = '';
135 $self->{YPOS} = 0;
136 $self->cmdConfig(
137 'export_html' => ['export2html', $self],
138 'export_png' => ['export2png', $self],
139 );
140 return $self;
141 }
142  
143 sub background {
144 my $self = shift;
145 $self->{BACKGROUND} = shift if @_;
146 return $self->{BACKGROUND}
147 }
148  
149 sub export2html {
150 my $self = shift;
151 my $mdi = $self->mdi;
152  
153 #setting initial values
154 my %iv = ();
155 my $name = $mdi->docSelected;
156 return unless defined $name;
157 $iv{'outfile'} = "$name.html";
158 $iv{'theme'} = 'White';
159 $iv{'scrolled'} = '';
160 $iv{'lineoffset'} = 1;
161 $iv{'foldmarkers'} = '';
162  
163 #popping the form
164 my %options = $self->popForm(
165 -acceptempty => 1,
166 -initialvalues => \%iv,
167 -oktext => 'Export',
168 -structure => [
169 '*section' => 'Output',
170 outfile => ['file', 'File'],
171 '*end',
172 '*section' => 'Theme',
173 theme => ['radio', 'Color', -values => [qw/White LightGray Gray DarkGray Black/]],
174 '*end',
175 '*section' => 'Features',
176 scrolled => ['boolean', 'Scrolled'],
177 foldmarkers => ['boolean', 'Fold markers'],
178 '*column',
179 lineoffset => ['boolean', 'Line numbers'],
180 '*end',
181 ],
182 -title => 'Export to HTML',
183 );
184 return unless %options;
185
186 #checking data
187 my $file = delete $options{'outfile'};
188 croak "Outfile not specified" if $file eq '';
189 my %params = ();
190 for (sort keys %options) {
191 my $val = $options{$_};
192 $params{$_} = $val if $val;
193 }
194
195 #exporting
196 my $widg = $mdi->docWidget;
197 my $hl = Syntax::Kamelon->new(
198 syntax => $widg->cget('-syntax'),
199 formatter => ['HTML4', %params],
200 );
201 $hl->Reset;
202 my $text = $widg->get('1.0', 'end - 1c');
203 $hl->Parse($text);
204 if (open my $fh, '>', $file) {
205 print $fh $hl->Format;
206 close $fh
207 } else {
208 $self->logWarning("Can not open '$file'");
209 }
210 }
211  
212 sub export2png {
213 my $self = shift;
214 my $mdi = $self->mdi;
215  
216 #setting initial values
217 my %iv = ();
218 my $name = $mdi->docSelected;
219 return unless defined $name;
220 my $widg = $mdi->docWidget;
221  
222 my $folder = $self->configGet('-configfolder');
223 if (-e "$folder/export2pngrc") {
224 my $cff = $self->extGet('ConfigFolder');
225 %iv = $cff->loadHash('export2pngrc', 'exporterpng');
226 } else {
227 $iv{'background'} = $widg->Subwidget('XText')->cget('-background');
228 $iv{'foreground'} = $widg->Subwidget('XText')->cget('-foreground');
229 $iv{'themefile'} = $self->extGet('ConfigFolder')->ConfigFolder . '/highlight_theme.ctt';
230 $iv{'font'} = $self->configGet('-contentfont');
231 $iv{'linenumbers'} = 1;
232 $iv{'margins'} = 10;
233 $iv{'maxwidth'} = 0;
234 $iv{'tabsize'} = 3;
235 }
236 $iv{'outfile'} = "$name.png";
237  
238 #popping the form
239 my %options = $self->popForm(
240 # -acceptempty => 1,
241 -initialvalues => \%iv,
242 -oktext => 'Export',
243 -structure => [
244 '*section' => 'Output',
245 outfile => ['file', 'File', -width => 50],
246 '*end',
247 '*section' => 'Theme',
248 '*frame',
249 background => ['color', 'Background', -width => 8],
250 '*column',
251 foreground => ['color', 'Foreground', -width => 8],
252 '*end',
253 themefile => ['file', 'Theme file'],
254 font => ['font', 'Font'],
255 '*end',
256 '*section' => 'Features',
257 linenumbers => ['boolean', 'Line numbers'],
258 tabsize => ['spin', 'Tab size', -width => 6],
259 '*column',
260 maxwidth => ['spin', 'Max width', -width => 6],
261 margins => ['spin', 'Margins', -width => 6],
262 '*end',
263 ],
264 -title => 'Export to PNG',
265 );
266 return unless %options;
267  
268 #saving options
269 my $cff = $self->extGet('ConfigFolder');
270 $cff->saveHash('export2pngrc', 'exporterpng', %options);
271  
272 #checking data
273 my $file = delete $options{'outfile'};
274 croak "Outfile not specified" if $file eq '';
275 my $tabstr = '';
276 for (1 .. $options{'tabsize'}) { $tabstr = "$tabstr " }
277 $self->background(Imager::Color->new($options{'background'}));
278 $self->imargin($options{'margins'});
279 $self->linenumbers($options{'linenumbers'});
280 $self->linecolumn(length($widg->linenumber('end - 1c')));
281 $self->maxwidth($options{'maxwidth'});
282 $self->tabstring($tabstr);
283  
284 #converting data
285 my $art = $self->extGet('Art');
286 my $l = $self->Label(-font => $options{'font'});
287 my $f = $l->cget('-font');
288 my $fntnorm = $widg->fontCompose($f, -slant => 'roman', -weight => 'normal');
289 my $fntbold = $widg->fontCompose($f, -slant => 'roman', -weight => 'bold');
290 my $fntbolditalic = $widg->fontCompose($f, -slant => 'italic', -weight => 'bold');
291 my $fntitalic = $widg->fontCompose($f, -slant => 'italic', -weight => 'normal');
292  
293 my $linespace = $self->fontMetrics($f, '-linespace');
294 my $desc = $self->fontMetrics($f, '-descent');
295  
296 $l->destroy;
297  
298 my $foreground = Imager::Color->new($options{'foreground'});
299  
300 $self->xpos($self->imargin);
301 $self->ypos($self->imargin + $linespace - $desc);
302 $self->iwidth($self->imargin * 2);
303 $self->iheight(($self->imargin * 2) + $linespace);
304  
305 $self->image($self->inew($self->iwidth, $self->iheight));
306  
307 #setting up tags
308 my $thmfl = $options{'themefile'};
309 my $theme = Tk::CodeText::Theme->new;
310 unless (-e $thmfl) {
311 $self->logError("Theme file '$thmfl' does not exist");
312 return
313 }
314 $theme->load($thmfl);
315 my %tags = ();
316 for ($theme->tagList) {
317 my $tag = $_;
318 # print "tag: $tag\n";
319 my %options = ();
320 my $bold = $theme->getItem($tag, '-weight');
321 $bold = 0 unless defined $bold;
322 my $slant = $theme->getItem($tag, '-slant');
323 $slant = 0 unless defined $slant;
324 if ($bold and $slant) {
325 # print " setting font bold/italic\n";
326 $options{'-font'} = $fntbolditalic
327 } elsif ($bold) {
328 # print " setting font bold\n";
329 $options{'-font'} = $fntbold
330 } elsif ($slant) {
331 # print " setting font italic\n";
332 $options{'-font'} = $fntitalic
333 } else {
334 # print " normal font remains\n";
335 $options{'-font'} = $fntnorm
336 }
337 my $fg = $theme->getItem($tag, '-foreground');
338 if ($fg ne '') {
339 # print " setting foreground $fg\n";
340 $options{'-foreground'} = Imager::Color->new($fg);
341 } else {
342 # print " default foreground remains\n";
343 $options{'-foreground'} = $foreground;
344 }
345 my $bg = $theme->getItem($tag, '-background');
346 if ($bg ne '') {
347 # print " default background $bg\n";
348 $options{'-background'} = Imager::Color->new($bg);
349 } else {
350 # print " default background remains\n";
351 # $options{'-background'} = $self->background;
352 }
353 $tags{$tag} = \%options;
354 }
355  
356 #setting up highlighter
357 my $syntax = $widg->cget('-syntax');
358 my $hl;
359 if ($syntax ne 'None') {
360 $hl = Syntax::Kamelon->new(
361 syntax => $syntax,
362 formatter => ['Base',
363 # foldingdepth => 'all',
364 ],
365 );
366 $hl->Reset;
367 }
368  
369 #exporting
370 my $last = $widg->linenumber('end - 1c');
371 my $end = $widg->index('end - 1c');
372 my $count = 0;
373 $self->mdi->progressAdd('exporter', 'Exporting', $last, \$count);
374 for (1 .. $last) {
375 my $linenum = $_;
376 my $linestart = "$linenum.0";
377  
378 my $lineend = $widg->index("$linestart lineend + 1c");
379 $lineend = $end if $widg->compare($end,'<',$lineend);
380  
381 $self->iprintln($linenum, $fntnorm, $foreground) if $self->linenumbers;
382  
383 my $line = $widg->get($linestart, $lineend);
384 if ($syntax eq 'None') {
385 $self->iprint($line, $fntnorm, $foreground);
386 } else {
387 my @h = $hl->ParseRaw($line);
388 while (@h) {
389 my $string = shift @h;
390 my $tag = shift @h;
391 my $t = $tags{$tag};
392 $self->iprint($string, $t->{'-font'}, $t->{'-foreground'}, $t->{'-background'});
393 };
394 $self->iprint("\n", $fntnorm, $foreground) if $line =~ /\n$/;
395 }
396 $count++;
397 $self->update;
398 }
399 $self->mdi->progressRemove('exporter');
400
401 my $out = $self->inew($self->iwidth, $self->iheight);
402 $out->paste(src => $self->image);
403 $out->write(file => $file, type => 'png');
404  
405 }
406  
407 sub iheight {
408 my $self = shift;
409 $self->{IHEIGHT} = shift if @_;
410 return $self->{IHEIGHT}
411 }
412  
413 sub image {
414 my $self = shift;
415 $self->{IMAGE} = shift if @_;
416 return $self->{IMAGE}
417 }
418  
419 sub imargin {
420 my $self = shift;
421 $self->{IMARGIN} = shift if @_;
422 return $self->{IMARGIN}
423 }
424  
425  
426 sub inew {
427 my ($self, $width, $height) = @_;
428 my $new = Imager->new(xsize => $width, ysize => $height, channels => 4);
429 $new->flood_fill(x => 0, y => 0, color => $self->background);
430 return $new
431 }
432  
433 sub inewline {
434 my ($self, $font) = @_;
435 my $linespace = $self->fontMetrics($font, '-linespace');
436 my $height = $self->iheight + $linespace;
437 my $new = $self->inew($self->iwidth, $height);
438 $new->paste(src => $self->image);
439 $self->image($new);
440 $self->ypos($self->ypos + $linespace);
441 $self->xpos($self->imargin);
442 $self->iheight($height);
443 }
444  
445 sub iprint {
446 my ($self, $string, $font, $fg, $bg) = @_;
447 my $cur = $self->image;
448 my $ifont = $self->extGet('Art')->imagerFont($font);
449 my $newline = 0;
450 my $part = '';
451 while ($string ne '') {
452 if ($string =~ s/^([^\n]*)\n//) {
453 $newline = 1;
454 $part = $1;
455 } else {
456 $part = $string;
457 $string = '';
458 }
459  
460 #replace tabs
461 my $tab = $self->tabstring;
462 $part =~ s/\t/$tab/g;
463  
464 #wrap if bigger than maxwidth
465 my $partsize = $self->fontMeasure($font, $part);
466 my $linesize = $self->xpos + $partsize + $self->imargin;
467 my $mw = $self->maxwidth;
468 if (($mw > 0) and ($mw < $linesize)) { #bigger than maxwidth
469 while ($mw < $linesize) {
470 my $section = '';
471 while (($self->xpos + $self->fontMeasure($font, $section) + $self->imargin) < $mw) {
472 $section = $section . substr($part, 0, 1);
473 $part = substr($part, 1);
474 }
475 $self->iprints($section, $font, $fg, $bg);
476 $self->inewline($font);
477 $self->iprintln(' ', $font, $fg) if $self->linenumbers;
478 $partsize = $self->fontMeasure($font, $part);
479 $linesize = $self->xpos + $partsize + $self->imargin;
480 }
481 }
482  
483  
484 #output part
485 $self->iprints($part, $font, $fg, $bg);
486  
487 #output newline
488 $self->inewline($font) if ($newline);
489  
490 $part = '';
491 $newline = 0;
492 }
493 }
494  
495 sub iprintln {
496 my ($self, $line, $font, $foreground) = @_;
497 my $lc = $self->linecolumn;
498 while (length($line) < $lc) { $line = " $line" };
499 $self->iprint("$line ", $font, $foreground);
500 }
501  
502 sub iprints {
503 my ($self, $string, $font, $fg, $bg) = @_;
504 my $ifont = $self->extGet('Art')->imagerFont($font);
505 my $partsize = $self->fontMeasure($font, $string);
506 my $linesize = $self->xpos + $partsize + $self->imargin;
507 if ($linesize > $self->iwidth) { #need to enlarge in x direction?
508 my $new = $self->inew($linesize, $self->iheight);
509 $new->paste(src => $self->image);
510 $self->image($new);
511 $self->iwidth($linesize);
512 }
513
514 if (defined $bg) { #paint background
515 my $linespace = $self->fontMetrics($font, '-linespace');
516 my $descent = $self->fontMetrics($font, '-descent');
517 my $x1 = $self->xpos;
518 my $x2 = $x1 + $partsize;
519 my $y2 = $self->ypos + $descent;
520 my $y1 = $y2 - $linespace;
521 $self->image->polygon(
522 points => [[$x1, $y1], [$x2, $y1], [$x2, $y2], [$x1, $y2]],
523 color => $bg,
524 );
525 }
526  
527 $self->image->string(
528 x => $self->xpos,
529 y => $self->ypos,
530 font => $ifont,
531 string => $string,
532 color => $fg,
533 aa => 1,
534 );
535 $self->xpos($self->xpos + $partsize);
536 }
537  
538 sub iwidth {
539 my $self = shift;
540 $self->{IWIDTH} = shift if @_;
541 return $self->{IWIDTH}
542 }
543  
544 sub linecolumn {
545 my $self = shift;
546 $self->{LINECOLUMN} = shift if @_;
547 return $self->{LINECOLUMN}
548 }
549  
550 sub linenumbers {
551 my $self = shift;
552 $self->{LINENUMBERS} = shift if @_;
553 return $self->{LINENUMBERS}
554 }
555  
556 sub maxwidth {
557 my $self = shift;
558 $self->{MAXWIDTH} = shift if @_;
559 return $self->{MAXWIDTH}
560 }
561  
562 sub MenuItems {
563 my $self = shift;
564 return (
565 [ 'menu_normal', 'File::Close', 'Export to ~HTML', 'export_html', 'text-html'],
566 [ 'menu_normal', 'File::Close', 'Export to P~NG', 'export_png', 'image-x-generic'],
567 [ 'menu_separator', 'File::Close', 'F3'],
568 )
569 }
570  
571 sub tabstring {
572 my $self = shift;
573 $self->{TABSTRING} = shift if @_;
574 return $self->{TABSTRING}
575 }
576  
577 sub Unload {
578 my $self = shift;
579 $self->cmdRemove('export_html');
580 $self->cmdRemove('export_png');
581 return $self->SUPER::Unload;
582 }
583  
584 sub xpos {
585 my $self = shift;
586 $self->{XPOS} = shift if @_;
587 return $self->{XPOS}
588 }
589  
590 sub ypos {
591 my $self = shift;
592 $self->{YPOS} = shift if @_;
593 return $self->{YPOS}
594 }
595  
596  
597 =head1 LICENSE
598  
599 Same as Perl.
600  
601 =head1 AUTHOR
602  
603 Hans Jeuken (hanje at cpan dot org)
604  
605 =head1 TODO
606  
607 =over 4
608  
609 =back
610  
611 =head1 BUGS AND CAVEATS
612  
613 If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>.
614  
615 =head1 SEE ALSO
616  
617 =over 4
618  
619 =back
620  
621 =cut
622  
623 1;
624  
625  
626  
627  
628  
629  
630  
631  
632  
633  
634  
635  
636  
637  
638  
639