#!/usr/bin/perl
use strict;
use warnings;
use File::Spec;
use Getopt::Long;
use POSIX qw(:errno_h);
my $verbose=0;
my $charset='UTF-8';
my $output_base=File::Spec->curdir();
my $input_base=File::Spec->curdir();
my @modules=();
my @env_dirs=();
my @files=();
$charset=$ENV{MM_CHARSET} if($ENV{MM_CHARSET});
$charset=$ENV{HTML_CHARSET} if($ENV{HTML_CHARSET});
GetOptions (
"charset=s" => \$charset,
"outdir|dir=s" => \$output_base,
"basedir=s" => \$input_base,
"env=s" => \@env_dirs,
"module=s" => \@modules,
"file=s" => \@files,
"help" => sub{ usage(); },
"verbose" => \$verbose,
) || usage('Unkown option',0,EINVAL());
@modules=@ARGV if(@ARGV);
unshift(@INC,@env_dirs);
usage('No Modul !',0,EINVAL()) unless(@modules);
$input_base=File::Spec->rel2abs($input_base);
@files=map{ File::Spec->rel2abs($_,$input_base) }@files;
$Pod::Simple::HTML::Content_decl = qq{};
########################################################################
for my $path (@files)
{
unless(convert_file($path))
{ usage(qq(File "$path" not found!),1,ENOENT()); }
}
for my $module (@modules)
{
my $path=get_filename($module);
unless(convert_file($path,$module))
{ usage(qq(File for Module "$module" not found!),1,ENOENT()); }
}
########################################################################
# Funktionen
########################################################################
sub convert_file
{
my $path=shift;
my $module=shift;
if($path && -f $path )
{
print qq|Read Pod from $path\n| if($verbose);
my $pod=Pod::Simple::HTML::Local->new();
my $html='';
$pod->output_string(\$html);
$pod->set_module_match_ref(\@modules);
$pod->set_file_match_ref(\@files);
$pod->set_source( $path );
$pod->set_base_dir( $input_base );
$pod->run;
if($html)
{
print "HTML created \n" if($verbose);
my $outputname='';
if($module)
{ $outputname=File::Spec->join($output_base,Pod::Simple::HTML::Local::convert_module_name($module)); }
else
{ $outputname=File::Spec->join($output_base,Pod::Simple::HTML::Local::convert_file_name($path)); }
print qq(Write HTML to file "$outputname" \n) if($verbose);
create_path($outputname) or usage(qq|ERROR create "$outputname" ($!)\n|,1,EACCES());
open(my $fh, '>', $outputname) or usage(qq|ERROR create "$outputname" ($!)\n|,1,EACCES());
print $fh $html;
close($fh);
}
else
{ print "NO HTML created\n" if($verbose); }
return 1;
}
return 0;
}
sub usage
{
my $msg=shift || '';
my $nohelp=shift || 0;
my $exit=shift || 0;
my $out=\*STDOUT;
if($msg)
{
$out=\*STDERR;
print $out "ERROR: $msg\n";
$exit=255 unless($exit);
}
print $out <] [-c ] [-d