Template::Plugin::PwithBR - TT Plugin that changes paragraph and newline into p with br.


Template-Plugin-PwithBR documentation Contained in the Template-Plugin-PwithBR distribution.

Index


Code Index:

NAME

Top

Template::Plugin::PwithBR - TT Plugin that changes paragraph and newline into p with br.

SYNOPSIS

Top

  [% USE PwithBR %]
  [% FILTER p_with_br %]
  foo
  bar

  hoge
  [% END %]

Output:

  <p>
  foo<br />
  bar
  </p>

  <p>
  hoge</p>

DESCRIPTION

Top

Template::Plugin::PinBR is TT plugin.When this plugin is used, <p> and <br> are appropriately output.

It is not possible to achieve it with the filter html_para and html_break / html_para_break or html_line_break.

SEE ALSO

Top

Template, Template::Plugin

AUTHOR

Top

Daisuke Komatsu, <taro@cpan.org>

COPYRIGHT AND LICENSE

Top


Template-Plugin-PwithBR documentation Contained in the Template-Plugin-PwithBR distribution.

package Template::Plugin::PwithBR;

use strict;
use warnings;

use base qw( Template::Plugin );
use Template::Plugin;

our $FILTER_NAME = 'p_with_br';

our $VERSION = '0.04';

sub new {
    my($class, $context, @args) = @_;
    my $name = $args[0] || $FILTER_NAME;
    $context->define_filter($name, \&p_with_br, 0);
    return $class;
}

sub p_with_br {
    my $text = shift;
    $text =~ s/\x0D\x0A/\n/g;
    $text =~ tr/\x0D\x0A/\n\n/; 

    $text =~ s{(?<!\n)(\n)(?!\n)}{<br />\n}g;
    $text = "<p>\n"
        . join("\n</p>\n\n<p>\n", split(/(?:\r?\n){2,}/, $text))
        . "</p>\n";
    $text;
}

1;
__END__