Jump to content

User:AnomieBOT/source/tasks/SourceUploader/WikiPod.pm

From Wikipedia, the free encyclopedia
package tasks::SourceUploader::WikiPod;

use utf8;
use strict;
use Pod::Simple::Wiki;
use Pod::Simple::Wiki::Mediawiki;
use URI;
use Data::Dumper;

use vars qw(@ISA);
@ISA=qw/Pod::Simple::Wiki::Mediawiki/;

sub new {
    my $class = shift;
    my $self = Pod::Simple::Wiki->new('mediawiki', @_);

    # Set encoding
    $self->encoding( 'utf8' ) if $self->can('encoding');

    # New "A<>" code handling
    $self->{_A_base} = shift;
    $self->accept_codes('A');

    # Override stupid choice of tags
    $self->{_tags} = {
        %{$self->{_tags}},
        '<tt>'   => '<code>',
        '</tt>'  => '</code>',
        '<pre>'  => "<pre>\n",
        '</pre>' => "\n</pre>\n",
    };

    bless $self, $class;
    return $self;
}

# New "A<>" code handling
sub _start_A {
    my $self=shift;
    $self->_append('');
    $self->_output; # Flush text buffer
}

sub _end_A {
    my $self=shift;
    my $link="$self->{_A_base}/$self->{_wiki_text}";
    if($link=~/::/){
        $link=~s!::!/!g;
        $link.='.pm';
    }
    $self->{_wiki_text} = "[[$link|$self->{_wiki_text}]]";
}

# Fix indentation and multiple =items with no intervening paragraph.
sub _indent_item {
    my $self=shift;
    my $item_type    = $_[0];
    my $item_param   = $_[1];
    my $indent_level = $self->{_item_indent};

    $self->_append(':' x ($indent_level-1));
    if ($item_type eq 'bullet') {
        $self->_append('* ');
    } elsif ($item_type eq 'number') {
        $self->_append('# ');
    } elsif ($item_type eq 'text') {
        $self->_append('; ');
    }
}

sub _end_item_text {$_[0]->_output("\n")}

sub _start_Para {
    my $self         = shift;
    my $indent_level = $self->{_item_indent};

    if ($self->{_in_over_block}) {
        $self->{_indent_text} = (':' x $indent_level);
    }

    if ($self->{_in_over_text}) {
        # This line is the only difference from Pod::Simple::Wiki::Mediawiki
        $self->{_indent_text} = (':' x $indent_level);
    }
}

# Fix link formatting
sub _format_link {
    my ($self, $text, $attr) = @_;

    if ($attr->{type} eq 'url') {
        my $link = $attr->{to};
        if($link=~m(^(?:https?|ftp|irc|mailto|svn|git)://)){
            return $link if $attr->{'content-implicit'};
            return "[$link $text]";
        } else {
            $link=~s/_/ /g;
            ($text=$link)=~s/^[^:]*:// if $attr->{'content-implicit'};
            return "[[:$link|$text]]";
        }
    }

    if ($attr->{type} eq 'man') {
        if($attr->{to}=~/^(.+)\(([^)]+)\)$/){
            my ($page,$section)=($1,$2);
            my $src='';
            $src='perldoc' if $page=~/^perl/i || $section eq '3perl';
            return "{{man|$section|$page|$src||inline}}" if(!defined($attr->{section}) && $attr->{'content-implicit'});
            $src='default' if $src eq '';
            my $link="{{man/$src|$section|$page|url}}";
            $link.="#".$attr->{section} if defined $attr->{section};
            return "{{man/format|$section|$page|$link}}" if $attr->{'content-implicit'};
            return "[$link $text]";
        }
    }

    die "Unknown link type $attr->{type}" unless $attr->{type} eq 'pod';

    # Handle a link within this page:
    return "[[#$attr->{section}|$text]]" unless defined $attr->{to};

    # Handle a link to a specific section in another page:
    return "[[$attr->{to}#$attr->{section}|$text]]" if defined $attr->{section};

    return "[[$attr->{to}]]" if $attr->{'content-implicit'};

    return "[[$attr->{to}|$text]]";
}


1;