#!/usr/bin/perl
package MyFormatter;
$VERSION='0.1';
use strict;
use lib "/home/msittig/public_html/wiki" ;
use base qw(
CGI::Kwiki::Formatter
Blockquotes
DefinitionLists
ReplacedObjects
);
use CGI::Kwiki qw(:char_classes);
require( "blog2html.pl" );
sub process_order {
my ($self) = @_;
my @order = $self->SUPER::process_order();
@order = insert_process_method(
"quote", "function", @order
);
@order = insert_process_method(
"definition_lists", "comment", @order
);
@order = insert_process_method(
"replaced_object_link", "named_http_link", @order
);
@order = insert_process_method(
"isbn_link", "inline", @order
);
@order = insert_process_method(
"blah", "wiki_link_forced", @order
);
@order = insert_process_method(
"blah", "wiki_link", @order
);
return @order;
}
sub insert_process_method {
my ($insert, $before, @order) = @_;
my @new_order;
foreach my $method (@order) {
push @new_order, $insert if $method eq $before;
push @new_order, $method;
}
return @new_order;
}
sub format_table {
my ($self, $rows) = @_;
my $cols = 0;
for (@$rows) {
$cols = @$_ if @$_ > $cols;
}
my $table = qq{\n
\n};
for my $row (@$rows) {
$table .= qq{\n};
for (my $i = 0; $i < @$row; $i++) {
my $colspan = '';
if ($i == $#{$row} and $cols - $i > 1) {
$colspan = ' colspan="' . ($cols - $i) . '"';
}
my $cell = $self->escape_html($row->[$i]);
$cell = qq{$cell
\n}
if $cell =~ /\n/;
$cell = ' ' unless length $cell;
$table .= qq{$cell | \n};
}
$table .= qq{
\n};
}
$table .= qq{
\n};
return \$table;
}
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;
my $close_item = 1;
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";
$close_item = 0;
}
$level = $new_level;
}
elsif ($new_level < $level) {
for (1..($level - $new_level)) {
$tag = pop @tag_stack;
$text .= "\n$tag>\n";
$close_item = 1;
}
$level = $new_level;
}
chomp( $line );
$text .= "\n" if $close_item ;
$text .= "\n".&li_format( $line );
$close_item = 1;
}
for (1..$level) {
my $tag = pop @tag_stack;
$text .= "\n$tag>";
}
$_ = $self->lists_format($text);
}
$_;
}
split m!(^[0\*]+ .*?\n)(?=(?:[^0\*]|$))!ms, $text;
}
sub li_format {
my $text = shift;
my $return = '';
my $topic = '';
$topic = $1 if $text =~ s/([^\[]*?) - //;
$text =~ s/^ +//gm;
# $return .= "\n" if $topic;
$text = "$topic\n\n".$text if $topic;
$return .= &blog2html( $text, 1 )."\n";
$return =~ s//
\n\t\t/g;
$return =~ s{
}{\n\t}g;
return $return;
}
sub escape_html {
my ($self, $text) = @_;
$text =~ s/&(?!amp;|#|\w+;)/&/g;
$text =~ s/</g;
$text =~ s/>/>/g;
$text;
}
sub code_postformat {
my ($self, $text) = @_;
chomp($text);
return "$text
\n";
}
sub italic {
my ($self, $text) = @_;
$text =~ s#(?$1#g;
return $text;
}
sub bold {
my ($self, $text) = @_;
$text =~ s#(?$1#g;
return $text;
}
sub wiki_link_format {
my ($self, $text) = @_;
my( $attr ) ;
$attr = qq{ title="}.$self->config->title_prefix.qq{: $text"}
if defined $self->config ;
my $script = $self->script;
my $url = $self->escape($text);
my $wiki_link = qq{$text};
my $excluded = 'RecentChanges' ;
if( defined $self->database and
( not $self->database->exists($text) ) and
$text !~ /$excluded/ ) {
$wiki_link =~ s/is_readable($text)) {
$url = $self->escape($self->loc("KwikiPrivatePage"));
$wiki_link =
qq{$text};
}
return $wiki_link ;
}
sub wiki_link {
my ($self, $text) = @_;
$self->split_method($text,
qr{([$UPPER](?=[$WIKIWORD]*[$UPPER])(?=[$WIKIWORD]*[$LOWER])[$WIKIWORD]+)(?![^\"]*")},
'wiki_link_format',
);
}
sub wiki_link_forced_format {
my ($self, $text) = @_;
my( $attr ) ;
$attr = qq{ title="}.$self->config->title_prefix.qq{: $text"}
if defined $self->config ;
my $script = $self->script;
my $url = $self->escape($text);
$url =~ s/\;\,\.\'\" //g ;
my $wiki_link = qq{$text};
my $excluded = 'RecentChanges' ;
if( defined $self->database and
( not $self->database->exists($text) ) and
$text !~ /$excluded/ ) {
$wiki_link =~ s/is_readable($text)) {
$url = $self->escape($self->loc("KwikiPrivatePage"));
$wiki_link =
qq{$text};
}
return $wiki_link ;
}
sub wiki_link_forced {
my ($self, $text) = @_;
$self->split_method($text,
qr{(?paragraph_format($_);
}
$_;
}
split m!(\n\s*\n)!ms, $text;
}
sub paragraph_format {
my ($self, $text) = @_;
return '' if $text =~ /^[\s\n]*$/;
return $text if $text =~ /^<(o|u|d)l>/i;
# This adds paragraph tags, etc.
return &blog2html( $text ) . "\n";
}
# Fix this to detect images
# Never mind, handling images separately.
# Unchanged
sub named_http_link {
my ($self, $text) = @_;
$self->split_method($text,
qr{(?link_format($text);
}
sub http_link {
my ($self, $text) = @_;
$self->split_method($text,
qr{((?:https?|ftp|irc|file):\S+?(?=[),.:;]?(?:\s|<|$)))}m,
'http_link_format',
);
}
sub http_link_format {
my ($self, $text) = @_;
return $self->link_format($text);
}
sub link_format {
my ($self, $text) = @_;
my( $title, $ext, $internal, %attr ) = ( "", "", "" ) ;
( $text, $title ) = split( /\n/, $text );
$title ||= '';
$title =~ s/"/"/g;
# remove white space at beginning, end, or multiple spaces in between
$title =~ s/(^\s*|\s+(?=\s)|\s$)//sg;
$text =~ s/(^\s*|\s+(?=\s)|\s$)//sg;
# catch the case when the [] only contain a url
my $url = $text;
# remove the url from the text, and hand it to $url
$url = $1 if $text =~ s/(.*?) +//;
if( $title ) {
$attr{'title'} = qq{$title} ;
} else {
$ext = ": ".uc( $1 )
if $url =~ m/\.(png|gif|swf|jpg|jpeg|tiff|bmp)(\?|$)/i ;
if( $url =~ m#http://(?:www.)?([^/]*)(?=\?|/|$)#i ) {
$attr{'title'} = qq{$1$ext} ;
# } elsif( $url =~ m#([^/.]*)\.([^/.]*)\.([^./]*)(?=/|$)#i &&
# ( $title = "$1.$2.$3" ) && $2 =~ /^com?|edu|ac$/) {
# $attr{'title'} = qq{$title$ext} ;
} elsif ( $url =~ m#([^/.]*\.[^./]*)(?=/|$)#i ) {
$attr{'title'} = qq{$1$ext} ;
}
}
# The following check is necessary for Bloxsom integration.
$internal = '^'.$self->config->base_url.'' if( defined $self->config ) ;
$attr{'class'} = qq{external}
if( $url =~ /^http:\/\// && $url !~ /$internal/i ) ;
return qq{$text} ;
}
sub img_format {
my ($self, $url) = @_;
return qq{};
}
sub underscore {
my ($self, $text) = @_;
$text =~ s#(?$1#g;
return $text;
}
# ###### MY OWN ADDITIONS ############ #
sub isbn_link {
my ($self, $text) = @_ ;
$text =~ s#\b(ISBN|isbn):[\s]*([0-9\-xX]+)(?!\])#$1: $2#gi ;
return $text ;
}
1;