#!/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 ] [-b ] [-f [-f ...]] [-m ] [ [ ...] ] Convert POD to HTML with subpackages as local files and set links correctly. OPTIONS: -d | -o | --dir | --outputdir Set destination directory for the HTML files. Default directory is the current. -b --basedir Set source directory for the pod files. Default directory is the current. -c | --charset Set the character encoding in HTML document. UTF-8 is set as default. If available the enviroment variable MM_CHARSET or HTML_CHARSET will be used. -m | --module Set one or more modules which POD should be converted -f | --file Set one or more files which POD should be converted -e | --env Set one or more directories where modules can be found -h | --help Show this text -v | --verbose verbose output EOH exit($exit); } sub get_filename { my $module=shift; my $name=undef; if( -f $module) { $name=$module; } else { my @dirs=split('::',$module); my $path=File::Spec->join(@dirs); $path.='.pm'; for my $base (@INC) { my $fullpath=File::Spec->join($base,$path); if(-f $fullpath) { $name=$fullpath; last; } } unless($name) { my $fullpath=File::Spec->rel2abs($path); $name=$fullpath if(-f $fullpath); } } return $name; } sub create_path { my $path=shift; return 0 unless($path); $path=File::Spec->rel2abs($path); return 0 unless($path); my ($run,$directories,undef) = File::Spec->splitpath( $path ); for my $dir (File::Spec->splitdir( $directories )) { $run=File::Spec->join($run,$dir); unless(-d $run) { return 0 unless(mkdir($run)); } } return 1; } ######################################################################## # Pod::Simple::HTML::Local ######################################################################## package Pod::Simple::HTML::Local; use base 'Pod::Simple::HTML'; use URI::Escape; # Links anders verarbeiten # das mache ich mit dem Überschreiben der passenden Funktion in # Pod::Simple::HTML sub set_base_dir { my $self=shift; my $path=shift; return 0 if(!$path); $self->{INPUT_BASE_DIR}=$path; } sub set_module_match_ref { my $self=shift; my $ref=shift; return 0 if(ref($ref) ne 'ARRAY'); $self->{MODULE_MATCH}=$ref; return 1; } sub set_file_match_ref { my $self=shift; my $ref=shift; return 0 if(ref($ref) ne 'ARRAY'); $self->{FILE_MATCH}=$ref; return 1; } sub resolve_pod_link_by_table { my ($self,$it,$section)=@_; $self->{MODULE_MATCH}=[] unless($self->{MODULE_MATCH} && ref($self->{MODULE_MATCH}) eq "ARRAY"); $self->{FILE_MATCH}=[] unless($self->{FILE_MATCH} && ref($self->{FILE_MATCH}) eq "ARRAY"); my $file; if(!$it && $section) { $file='/'.uri_unescape($section); } elsif($it && $section && $it!~/::/) { $file=$it.'/'.uri_unescape($section); } elsif($it && $it!~/::/) { $file=$it; } if($file) { $file=File::Spec->rel2abs($file,$self->{INPUT_BASE_DIR}); } if($file) { # datei in liste if(grep{$_ eq $file}@{$self->{FILE_MATCH}}) { return convert_file_name($file); } # datei mit #pageref for my $path (@{$self->{FILE_MATCH}} ) { if($file=~/^\Q$path\E(.*?)$/) { my $add=$1; $add=~s!^[/#]+!!; return convert_file_name($path).'#'.$add; } } # Die Datei ist ".pod" und bekannt if($file=~m!^(.+?\.pod)((?:[/#].+?)?)$!) { my ($path,$add)=($1,$2); $add=~s!^[/#]+!!; if(-f $path) { push(@{$self->{FILE_MATCH}},$path); return convert_file_name($path).($add?'#'.$add:''); } } } if($section) {$section='#'.$section; } else { $section=''; } # Das Modul ist in der Liste if(grep{$_ eq $it}@{$self->{MODULE_MATCH}}) { return convert_module_name($it).$section; } # Das Modul gehört zum selben Paket if(grep{$it=~m!^\Q$_!}@{$self->{MODULE_MATCH}}) { push(@{$self->{MODULE_MATCH}},$it); return convert_module_name($it).$section; } return undef; } ######################################################################## sub convert_module_name { my $module=shift || ''; $module=~s/::/_/g; $module.='.html'; return $module; } sub convert_file_name { my $file=shift || ''; return $file if($file=~/\.html?$/); $file=~s!^.*?([^\\/]+)\.pod$!$1.html!; return $file; }