Skip to content

Instantly share code, notes, and snippets.

@m-r-r
Created May 4, 2012 17:55
Show Gist options
  • Save m-r-r/2596574 to your computer and use it in GitHub Desktop.
Save m-r-r/2596574 to your computer and use it in GitHub Desktop.
man-html-export
#!/usr/bin/env perl
# -*- coding: utf-8 -*- #
use strict;
use warnings;
use 5.010;
use Getopt::Long;
use File::Spec;
sub warning {
our %opts;
if ($opts{verbose}) {
say "warning: ", shift;
}
}
sub cmd {
my $cmd = join(' ', @_);
say ">> $cmd";
my $ret = system($cmd);
not $ret or die("exit code $ret\n");
}
sub show_version {
print "man-html-export.pl v0.1\n";
exit 0;
}
sub export_manpages {
my ($in_path, $out_path) = @_;
my ($filename, $in_file, $out_file, $sec, $gz);
unless (-d $out_path) {
cmd "mkdir -p $out_path";
}
opendir (DH, $in_path ) || die "cannot open $in_path\n";
while(($filename = readdir(DH))) {
if (substr($filename, 0, 1) eq '.') { next; }
$in_file = File::Spec->catfile($in_path, $filename);
($filename, $sec, $gz) = split(/\./, $filename, 3);
$out_file = File::Spec->catfile($out_path, "$filename.html");
if ($gz) {
cmd "gunzip -c $in_file | groff -T html -mandoc > $out_file";
}
else{
cmd "cat $in_file | groff -T html -mandoc > $out_file";
}
}
closedir(DH);
}
sub main {
our %opts;
my ($mp, $se, $path);
$opts{verbose} = 0;
$opts{sections} = ['1', '2', '3', '4', '5', '6', '7', '8'];
$opts{output} = 'man-html',
$opts{manpath} = ['/usr/share/man', '/usr/local/share/man'];
GetOptions(
'v|verbose!' => \$opts{verbose},
's|sections=s@' => \$opts{sections},
'o|output=s' => \$opts{output},
'm|manpath=s@' => \$opts{manpath},
'version' => \&show_version,
) or die("usage:\n\t$0 [opts]\n");
foreach $mp (@{$opts{manpath}}) {
foreach $se (@{$opts{sections}}) {
$path = File::Spec->catdir($mp, "man$se");
if (-d $path) {
say "Scanning $path ...";
export_manpages($path, "$opts{output}/man$se");
}
else {
warning "$path : not a directory";
}
}
}
}
unless(caller) {
main;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment