Skip to content

Commit

Permalink
Merge pull request #361 from maxmind/greg/core-only-man-page-generation
Browse files Browse the repository at this point in the history
Do not depend on non-core Perl modules
  • Loading branch information
horgh authored Dec 11, 2024
2 parents fff8dae + 20cc7ad commit 39336f0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Initialize CodeQL
uses: github/codeql-action/init@v3

- run: sudo apt install libipc-run3-perl libipc-system-simple-perl libfile-slurp-perl libfile-which-perl pandoc
- run: sudo apt install libipc-run3-perl pandoc
- run: |
./bootstrap
./configure
Expand Down
46 changes: 33 additions & 13 deletions dev-bin/make-man-pages.pl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
use FindBin qw( $Bin );

use File::Path qw( mkpath );
use File::Slurp qw( edit_file read_file );
use File::Which qw( which );

sub main {
my $target = shift || "$Bin/..";

my @translators = qw ( lowdown pandoc );
my $translator;
foreach my $p (@translators) {
if ( defined which($p) ) {
if ( _which($p) ) {
$translator = $p;
last;
}
Expand All @@ -32,6 +30,14 @@ sub main {
_make_man( $translator, $target, 'mmdblookup', 1 );
}

sub _which {
my $program = shift;
for my $path ( split /:/, $ENV{PATH} ) {
return 1 if -x "$path/$program";
}
return 0;
}

sub _make_man {
my $translator = shift;
my $target = shift;
Expand All @@ -53,7 +59,7 @@ sub _make_man {
'-M', "section:$section",
$input,
'-o', $output,
) == 0 or die "Failed to run pandoc: $!";
) == 0 or die "Failed to run pandoc: $?";
_pandoc_postprocess($output);
}
elsif ( $translator eq 'lowdown' ) {
Expand All @@ -66,14 +72,21 @@ sub _make_man {
'-M', "section:$section",
$input,
'-o', $output,
) == 0 or die "Failed to run lowdown: $!";
) == 0 or die "Failed to run lowdown: $?";
}
}

sub _make_lib_man_links {
my $target = shift;

my $header = read_file("$Bin/../include/maxminddb.h");
open my $header_fh, '<', "$Bin/../include/maxminddb.h"
or die "Failed to open header file: $!";
my $header = do { local $/; <$header_fh> };

die "Error reading file header file: $!" unless defined $header;

close $header_fh or die "Failed to close header file: $!";

for my $proto ( $header =~ /^ *extern.+?(MMDB_\w+)\(/gsm ) {
open my $fh, '>', "$target/man/man3/$proto.3"
or die "Failed to open file: $!";
Expand All @@ -88,13 +101,20 @@ sub _make_lib_man_links {
sub _pandoc_postprocess {
my $file = shift;

edit_file(
sub {
s/^\.IP\n\.nf/.IP "" 4\n.nf/gm;
s/(Automatically generated by Pandoc)(.+)$/$1/m;
},
$file,
);
open my $fh, '<', $file or die "Failed to open man file for reading: $!";
my @lines = <$fh>;
die "Error when reading man page: $!" if $!;

close $fh or die "Failed to close file: $!";

for my $line (@lines) {
$line =~ s/^\.IP\n\.nf/.IP "" 4\n.nf/gm;
$line =~ s/(Automatically generated by Pandoc)(.+)$/$1/m;
}

open $fh, '>', $file or die "Failed to open file for writing: $!";
print $fh @lines or die "Failed to write to file: $!";
close $fh or die "Failed to close file: $!";
}

main(shift);

0 comments on commit 39336f0

Please sign in to comment.