| 1 | package Template::Flute::Filter::Eol; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use base 'Template::Flute::Filter'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Template::Flute::Filter::Eol - Preserving line breaks in HTML output | ||
| 11 | |||
| 12 | =head1 DESCRIPTION | ||
| 13 | |||
| 14 | The EOL (end-of-line) filter turns line breaks into HTML <br> | ||
| 15 | elements. | ||
| 16 | |||
| 17 | =head1 METHODS | ||
| 18 | |||
| 19 | =head2 twig | ||
| 20 | |||
| 21 | Replaces the content of given L<XML::Twig::Elt> element with | ||
| 22 | the text value, preserving line breaks. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | sub twig { | ||
| 27 | my ($self, $elt, $value) = @_; | ||
| 28 | my (@lines, @elts); | ||
| 29 | |||
| 30 | # cut text into lines (set split limit to -1 to catch trailing linebreaks) | ||
| 31 | @lines = split(/\r?\n/, $value, -1); | ||
| 32 | |||
| 33 | for (my $i = 0; $i < @lines; $i++) { | ||
| 34 | # add text element | ||
| 35 | if (length($lines[$i])) { | ||
| 36 | push (@elts, $lines[$i]); | ||
| 37 | } | ||
| 38 | |||
| 39 | # add HTML linebreak | ||
| 40 | push (@elts, XML::Twig::Elt->new(br => '#EMPTY')); | ||
| 41 | } | ||
| 42 | |||
| 43 | # pop last HTML linebreak | ||
| 44 | pop (@elts); | ||
| 45 | |||
| 46 | if (@elts) { | ||
| 47 | $elt->set_content(@elts); | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | $elt->cut_children(); | ||
| 51 | } | ||
| 52 | |||
| 53 | return $elt; | ||
| 54 | } | ||
| 55 | |||
| 56 | =head1 AUTHOR | ||
| 57 | |||
| 58 | Stefan Hornburg (Racke), <racke@linuxia.de> | ||
| 59 | |||
| 60 | =head1 LICENSE AND COPYRIGHT | ||
| 61 | |||
| 62 | Copyright 2011-2016 Stefan Hornburg (Racke) <racke@linuxia.de>. | ||
| 63 | |||
| 64 | This program is free software; you can redistribute it and/or modify it | ||
| 65 | under the terms of either: the GNU General Public License as published | ||
| 66 | by the Free Software Foundation; or the Artistic License. | ||
| 67 | |||
| 68 | See http://dev.perl.org/licenses/ for more information. | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | 1; |