#!/usr/bin/perl

package POD2HTML;

use Pod::Simple::XHTML;
@ISA = qw/Pod::Simple::XHTML/;

sub new {
    bless shift->SUPER::new(@_), __PACKAGE__;
}

sub resolve_pod_page_link {
    (undef, $page, $section) = @_;

    $lnk = {
        'TUWF'           => '/tuwf/man',
        'TUWF::DB'       => '/tuwf/man/db',
        'TUWF::Intro'    => '/tuwf/man/intro',
        'TUWF::Misc'     => '/tuwf/man/misc',
        'TUWF::Request'  => '/tuwf/man/request',
        'TUWF::Response' => '/tuwf/man/response',
        'TUWF::XML'      => '/tuwf/man/xml',
        'TUWF::Validate' => '/tuwf/man/validate',
        ''               => '',
    }->{$page||''} // "https://metacpan.org/pod/$page";
    $lnk .= '#'.($section =~ s/ /-/gr) if $section;
    $lnk
}

sub resolve_man_page_link {
    (undef, $page, undef) = @_;

    my $lnk = {qw{
        globsterctl(1)      /globster/ctl
        globster-launch(1)  /globster/launch
        globster(1)         /globster/daemon
        globster-api(7)     /globster/api
        ncdu(1)             /ncdu/man
    }}->{$page||''} || ($page =~ /(.+)\((.)\)/ and "https://manned.org/$1.$2");
    $lnk
}


$p = POD2HTML->new();
$html = '';
#$p->anchor_items(1);  # pandoc doesn't support this :(
$p->output_string(\$html);
$p->parse_file(\*STDIN);

# Some post-processing to improve the pandoc-generated markdown
$html =~ s/^  //mg;
$html =~ s/<code>  /<code>/g;
$html =~ s/<li><p>/<li>/g;

print $html;
