Text-KwikiFormatish
view release on metacpan or search on metacpan
lib/Text/KwikiFormatish.pm view on Meta::CPAN
sub code_format {
my ( $self, $text ) = @_;
$self->_code_postformat( $self->_code_preformat($text) );
}
sub _code_preformat {
my ( $self, $text ) = @_;
my ($indent) = sort { $a <=> $b } map {length} $text =~ /^( *)\S/mg;
$text =~ s/^ {$indent}//gm;
#return $self->escape_html($text); ## already done in process order
return $text;
}
sub _code_postformat {
my ( $self, $text ) = @_;
return "<pre>$text</pre>\n";
}
=item * lists - itemized or enumerated lists
=cut
sub lists {
my ( $self, $text ) = @_;
my $switch = 0;
return map {
my $level = 0;
my @tag_stack;
if ( $switch++ % 2 ) {
my $text = '';
my @lines = /(.*\n)/g;
for my $line (@lines) {
$line =~ s/^([0\*]+) //;
my $new_level = length($1);
my $tag = ( $1 =~ /0/ ) ? 'ol' : 'ul';
if ( $new_level > $level ) {
for ( 1 .. ( $new_level - $level ) ) {
push @tag_stack, $tag;
$text .= "<$tag>\n";
}
$level = $new_level;
}
elsif ( $new_level < $level ) {
for ( 1 .. ( $level - $new_level ) ) {
$tag = pop @tag_stack;
$text .= "</$tag>\n";
}
$level = $new_level;
}
$text .= "<li>$line</li>";
}
for ( 1 .. $level ) {
my $tag = pop @tag_stack;
$text .= "</$tag>\n";
}
$_ = $self->lists_format($text);
}
$_;
}
split m!(^[0\*]+ .*?\n)(?=(?:[^0\*]|$))!ms, $text;
}
=item * lists_format - how to format the lists
=cut
sub lists_format {
my ( $self, $text ) = @_;
return $text;
}
=item * paragraph - normal, boring paragraphs
=cut
sub paragraph {
my ( $self, $text ) = @_;
my $switch = 0;
return map {
unless ( $switch++ % 2 )
{
$_ = $self->paragraph_format($_);
}
$_;
}
split m!(\n\s*\n)!ms, $text;
}
=item * paragraph_format - how to format paragraphs as XHTML
=cut
sub paragraph_format {
my ( $self, $text ) = @_;
return '' if $text =~ /^[\s\n]*$/;
return $text if $text =~ /^<(o|u)l>/i;
return "<p>\n$text\n</p>\n";
}
=item * horizontal_line - horizontal rules
=cut
sub horizontal_line {
my ( $self, $text ) = @_;
$self->split_method( $text, qr{^(----+)\s*$}m, 'horizontal_line_format',
);
}
=item * horizontal_line_format - horizontal rules as XHTML
=cut
sub horizontal_line_format {
my ($self) = @_;
my $text = "<hr/>\n";
return $text;
}
=item * mdash - long horizontal dashes
=cut
sub mdash {
my ( $self, $text ) = @_;
$text =~ s/([$WORD])-{3}([$WORD])/$1—$2/g;
return $text;
}
=item * comment - text that doesn't show up in the final markup
=cut
sub comment {
my ( $self, $text ) = @_;
$self->split_method( $text, qr{^\#\#(.*)$}m, 'comment_line_format', );
}
=item * comment_line_format - make XML comments out of 'em
=cut
sub comment_line_format {
my ( $self, $text ) = @_;
return "<!-- $text -->\n";
}
( run in 2.067 seconds using v1.01-cache-2.11-cpan-71847e10f99 )