| 1 | package MojoMojo::Formatter::Text; | ||||||||||
| 2 | |||||||||||
| 3 | 26 | ▶ | use base qw/MojoMojo::Formatter/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 26 | ▶ | use URI::Find; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | |||||||||||
| 7 | =head1 NAME | ||||||||||
| 8 | |||||||||||
| 9 | MojoMojo::Formatter::Text - format plain text as xhtml | ||||||||||
| 10 | |||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||
| 12 | |||||||||||
| 13 | This formatter will format content between {{txt}} and {{end}} as | ||||||||||
| 14 | XHTML) | ||||||||||
| 15 | |||||||||||
| 16 | It is based on Angerwhale/Format/PlainText.pm | ||||||||||
| 17 | |||||||||||
| 18 | =head1 METHODS | ||||||||||
| 19 | |||||||||||
| 20 | =over 4 | ||||||||||
| 21 | |||||||||||
| 22 | =item format_content_order | ||||||||||
| 23 | |||||||||||
| 24 | Format order can be 1-99. The Text formatter runs on 10 | ||||||||||
| 25 | |||||||||||
| 26 | =cut | ||||||||||
| 27 | |||||||||||
| 28 | 1116 | ▶ | sub format_content_order { 10 } | ||||||||
|
Subroutine
format_content_order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | |||||||||||
| 30 | =item format_content | ||||||||||
| 31 | |||||||||||
| 32 | calls the formatter. Takes a ref to the content as well as the | ||||||||||
| 33 | context object. | ||||||||||
| 34 | |||||||||||
| 35 | =cut | ||||||||||
| 36 | |||||||||||
| 37 | 124 | ▶ | sub format_content { | ||||||||
|
Subroutine
format_content: called CC=5 SCAR=19.9
Pod
documented
| |||||||||||
| 38 | 124 | my ( $class, $content, $c ) = @_; | |||||||||
| 39 | |||||||||||
| 40 | 124 | my @lines = split /\n/, $$content; | |||||||||
| 41 | 124 | my $txt; | |||||||||
| 42 | 124 | $$content = ""; | |||||||||
| 43 | 124 | my $start_re=$class->gen_re(qr/txt/); | |||||||||
| 44 | 124 | my $end_re=$class->gen_re(qr/end/); | |||||||||
| 45 | 124 | foreach my $line (@lines) { | |||||||||
| 46 | 659 | ▶ | if ($txt) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 0 | ▶ | if ( $line =~ m/^(.*)$end_re(.*)$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 48 | 0 | $$content .= MojoMojo::Formatter::Text->to_xhtml( $xhtml ); | |||||||||
| 49 | 0 | $txt = ""; | |||||||||
| 50 | } | ||||||||||
| 51 | 0 | else { $txt .= $line . "\n"; } | |||||||||
| 52 | } | ||||||||||
| 53 | else { | ||||||||||
| 54 | 659 | ▶ | if ( $line =~ m/^(.*)$start_re(.*)$/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 0 | $$content .= $1; | |||||||||
| 56 | 0 | $txt = " ".$2; # make it true :) | |||||||||
| 57 | } | ||||||||||
| 58 | 659 | else { $$content .= $line . "\n"; } | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | |||||||||||
| 63 | =item to_xhtml <txt> | ||||||||||
| 64 | |||||||||||
| 65 | takes some text and renders it as XHTML. | ||||||||||
| 66 | |||||||||||
| 67 | =cut | ||||||||||
| 68 | |||||||||||
| 69 | 2 | ▶ | sub to_xhtml { | ||||||||
|
Subroutine
to_xhtml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 70 | 2 | my ( $class, $text ) = @_; | |||||||||
| 71 | 2 | my $result; | |||||||||
| 72 | |||||||||||
| 73 | 2 | $text =~ s/&/&/g; | |||||||||
| 74 | 2 | $text =~ s/>/>/g; | |||||||||
| 75 | 2 | $text =~ s/</</g; | |||||||||
| 76 | 2 | $text =~ s/'/'/g; | |||||||||
| 77 | 2 | $text =~ s/"/"/g; | |||||||||
| 78 | |||||||||||
| 79 | # find URIs | ||||||||||
| 80 | my $finder = URI::Find->new( | ||||||||||
| 81 | 2 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 82 | 2 | my($uri, $orig_uri) = @_; | |||||||||
| 83 | 2 | return qq|<a href="$uri">$orig_uri</a>|; | |||||||||
| 84 | 2 | }); | |||||||||
| 85 | 2 | $finder->find(\$text); | |||||||||
| 86 | |||||||||||
| 87 | # fix paragraphs | ||||||||||
| 88 | 2 | my @paragraphs = split /\n+/m, $text; | |||||||||
| 89 | 2 | @paragraphs = grep { $_ !~ /^\s*$/ } @paragraphs; | |||||||||
| 90 | 2 | $result = join( ' ', map { "<p>$_</p>" } @paragraphs ); | |||||||||
| 91 | 2 | return qq{<div class="formatter_txt">\n$result</div>}; | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | |||||||||||
| 95 | =back | ||||||||||
| 96 | |||||||||||
| 97 | =head1 SEE ALSO | ||||||||||
| 98 | |||||||||||
| 99 | L<MojoMojo>,L<Module::Pluggable::Ordered> | ||||||||||
| 100 | |||||||||||
| 101 | =head1 AUTHORS | ||||||||||
| 102 | |||||||||||
| 103 | Daniel Brosseau <dab@catapulse.org> | ||||||||||
| 104 | |||||||||||
| 105 | =head1 LICENSE | ||||||||||
| 106 | |||||||||||
| 107 | This module is licensed under the same terms as Perl itself. | ||||||||||
| 108 | |||||||||||
| 109 | =cut | ||||||||||
| 110 | |||||||||||
| 111 | 26 | 1; | |||||||||