Peace/dscripts/generate_documentation.pl

97 lines
2.2 KiB
Perl
Raw Normal View History

#!/usr/bin/env perl
use v5.30.0;
use strict;
use warnings;
2022-03-17 18:07:01 +01:00
use Data::Dumper;
use Path::Tiny qw/path/;
2022-03-17 18:07:01 +01:00
use Mojo::Template;
2022-03-17 18:52:24 +01:00
use Mojo::DOM;
use File::pushd;
my $current_dir = path(__FILE__)->parent->parent->child('lib');
$current_dir->visit(
sub {
my ( $path, $state ) = @_;
if ( -f $path ) {
return if $path !~ /\.pm$/;
system mkdir => '-pv', 'doc/' . $path->parent;
2022-03-17 18:07:01 +01:00
system pod2html => $path,
2022-03-17 18:52:24 +01:00
'-o', 'doc/' . $path . '.html', '--recurse';
}
},
{ recurse => 1 }
);
2022-03-17 18:07:01 +01:00
my $documentation_path = path(__FILE__)->parent->parent->child('doc');
my %modules;
$documentation_path->visit(
sub {
my ( $path, $state ) = @_;
if ( !-f $path ) {
return;
}
if ( $path !~ /\.pm\.html$/ ) {
return;
}
$modules{ $path =~ s{^.*/lib/}{}r =~ s/\.pm\.html$//r =~ s{/}{::}gr } =
2022-03-17 18:52:24 +01:00
$path
},
{ recurse => 1 },
);
$documentation_path->visit(
sub {
my ( $path, $state ) = @_;
if ( !-f $path ) {
return;
}
if ( $path !~ /\.pm\.html$/ ) {
return;
}
my $html = Mojo::DOM->new($path->slurp_utf8);
for my $a ($html->find('a')->each) {
next if $a->{href};
if (exists $modules{$a->text}) {
$a->{href} = relativize_path($path, $modules{$a->text});
next;
}
$a->{href} = "https://metacpan.org/pod/@{[$a->text]}";
}
$path->spew_utf8(''.$html);
2022-03-17 18:07:01 +01:00
},
{ recurse => 1 },
);
my $mojo_template = Mojo::Template->new;
my $html = $mojo_template->render(<<'EOF', \%modules);
% my ($modules) = @_;
2022-03-17 18:52:24 +01:00
% use Path::Tiny;
2022-03-17 18:07:01 +01:00
<html>
<head>
</head>
<body>
<h1>Welcome to the Peace documentation</h1>
<ul>
% for my $module (sort { $a cmp $b } keys %$modules) {
<li>
2022-03-17 18:52:24 +01:00
<a href="<%=path($modules->{$module})->relative('doc')%>"><%=$module%></a>
2022-03-17 18:07:01 +01:00
</li>
% }
</ul>
</body>
</html>
EOF
$documentation_path->child('index.html')->spew_utf8($html);
2022-03-17 18:52:24 +01:00
sub relativize_path {
my $current_path = $_[0];
my $foreign_path = $_[1];
return $foreign_path->relative($current_path->parent);
}