<commit_before>#! perl
# Copyright (C) 2010, Parrot Foundation.
# $Id$

=head1 NAME

tools/docs/make_html_docs.pl - Write HTML documentation

=head1 SYNOPSIS

    % perl tools/docs/make_html_docs.pl --version

=head1 DESCRIPTION

This script writes the HTML documentation for Parrot.

=cut

use 5.12.0;

use warnings;

# as long as we have a separate docs/ makefile, this will be run from
# inside it. So jump up a dir to catch all the files.
chdir '..';

use lib 'lib';
use Fatal qw/open close/;

use File::Basename qw/basename dirname/;
use File::Path qw/make_path/;
use File::Spec;
use Getopt::Long;
use JSON;
use Parrot::Docs::PodToHtml;

my ( $version );

die unless GetOptions( 'version=s' => \$version );

die "Usage: $0 --version\n" unless $version;

my $target_dir = 'docs/html';

my $json = JSON->new();

# Transform the json
my %pages;
my @json_index_files = glob 'docs/index/*.json';
foreach my $index_file (@json_index_files) {
    my $contents;
    open my $fh, '<', $index_file;
    { local $/; $contents = <$fh> }
    my $section = '';
    eval {
        $section = $json->decode($contents);
    };
    if ($@) {
        say STDERR "Error in $index_file:";
        die $@;
    }

    my $outfile = $section->{page} . '.html';
    my $title   = $section->{title};

    foreach my $chunk (@{$section->{content}}) {
        my @raw_sources;
        if (ref $chunk->{source} eq "ARRAY" ) {
            @raw_sources = @{$chunk->{source}};
        }
        else {
            push @raw_sources, $chunk->{source};
        };

        my %sources;
        foreach my $source_elem (@raw_sources) {
            foreach my $file (glob($source_elem)) {
                $sources{$file} = 1;
            }
        }

        # These are only literals, no globs (for now?)
        if (exists $chunk->{exclude}) {
            foreach my $exclusion (@{$chunk->{exclude}}) {
                delete $sources{$exclusion}
            }
        }
        $chunk->{input_files} = [keys %sources];
    }
    $pages{lc $section->{page}} = $section;
}

foreach my $page (keys %pages) {
    $page = $pages{$page};
    foreach my $section (@{$page->{content}}) {
        foreach my $source (@{$section->{input_files}}) {
            if ($source =~ /^:(.*)/) {
                # Indicates link to other page.
                my $link = $1;
                if (! exists $pages{lc $link}) {
                    die "invalid link $source specified.\n";
                }
            }
            else {
                transform_input($source, $page->{page}, $page->{title});
            }
        }
    }
}

my %generated;
sub transform_input {
    my ($input, $parent, $parent_title) = @_;

    if (! -f $input) {
        die "$input not found or not a regular file\n" .
            "You might need to restrict your glob specification.";
    }

    my $formatter = Parrot::Docs::PodToHtml->new();
    $formatter->set_parent($parent, $parent_title);

    # Errata is currently noisy; e.g. complains about U<> even after
    # formatting it as expected. skip it until we can properly quiet it down.
    $formatter->no_errata_section(1);

    my $outfile = File::Spec->catfile($target_dir, $input) . '.html';

    my $dir = File::Path::make_path(File::Basename::dirname($outfile));

    open my $out_fh, '>', $outfile;

    $formatter->output_fh($out_fh);
    $formatter->parse_file($input);
    warn "$input generated no HTML output\n"
        unless $formatter->content_seen;

    $generated{$input} = 1;
}

exit 0;

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:
<commit_msg>Remove unnecessary dependency on perl 5.12<commit_after>#! perl
# Copyright (C) 2010, Parrot Foundation.
# $Id$

=head1 NAME

tools/docs/make_html_docs.pl - Write HTML documentation

=head1 SYNOPSIS

    % perl tools/docs/make_html_docs.pl --version

=head1 DESCRIPTION

This script writes the HTML documentation for Parrot.

=cut

use warnings;

# as long as we have a separate docs/ makefile, this will be run from
# inside it. So jump up a dir to catch all the files.
chdir '..';

use lib 'lib';
use Fatal qw/open close/;

use File::Basename qw/basename dirname/;
use File::Path qw/make_path/;
use File::Spec;
use Getopt::Long;
use JSON;
use Parrot::Docs::PodToHtml;

my ( $version );

die unless GetOptions( 'version=s' => \$version );

die "Usage: $0 --version\n" unless $version;

my $target_dir = 'docs/html';

my $json = JSON->new();

# Transform the json
my %pages;
my @json_index_files = glob 'docs/index/*.json';
foreach my $index_file (@json_index_files) {
    my $contents;
    open my $fh, '<', $index_file;
    { local $/; $contents = <$fh> }
    my $section = '';
    eval {
        $section = $json->decode($contents);
    };
    if ($@) {
        printf STDERR "Error in $index_file:\n";
        die $@;
    }

    my $outfile = $section->{page} . '.html';
    my $title   = $section->{title};

    foreach my $chunk (@{$section->{content}}) {
        my @raw_sources;
        if (ref $chunk->{source} eq "ARRAY" ) {
            @raw_sources = @{$chunk->{source}};
        }
        else {
            push @raw_sources, $chunk->{source};
        };

        my %sources;
        foreach my $source_elem (@raw_sources) {
            foreach my $file (glob($source_elem)) {
                $sources{$file} = 1;
            }
        }

        # These are only literals, no globs (for now?)
        if (exists $chunk->{exclude}) {
            foreach my $exclusion (@{$chunk->{exclude}}) {
                delete $sources{$exclusion}
            }
        }
        $chunk->{input_files} = [keys %sources];
    }
    $pages{lc $section->{page}} = $section;
}

foreach my $page (keys %pages) {
    $page = $pages{$page};
    foreach my $section (@{$page->{content}}) {
        foreach my $source (@{$section->{input_files}}) {
            if ($source =~ /^:(.*)/) {
                # Indicates link to other page.
                my $link = $1;
                if (! exists $pages{lc $link}) {
                    die "invalid link $source specified.\n";
                }
            }
            else {
                transform_input($source, $page->{page}, $page->{title});
            }
        }
    }
}

my %generated;
sub transform_input {
    my ($input, $parent, $parent_title) = @_;

    if (! -f $input) {
        die "$input not found or not a regular file\n" .
            "You might need to restrict your glob specification.";
    }

    my $formatter = Parrot::Docs::PodToHtml->new();
    $formatter->set_parent($parent, $parent_title);

    # Errata is currently noisy; e.g. complains about U<> even after
    # formatting it as expected. skip it until we can properly quiet it down.
    $formatter->no_errata_section(1);

    my $outfile = File::Spec->catfile($target_dir, $input) . '.html';

    my $dir = File::Path::make_path(File::Basename::dirname($outfile));

    open my $out_fh, '>', $outfile;

    $formatter->output_fh($out_fh);
    $formatter->parse_file($input);
    warn "$input generated no HTML output\n"
        unless $formatter->content_seen;

    $generated{$input} = 1;
}

exit 0;

# Local Variables:
#   mode: cperl
#   cperl-indent-level: 4
#   fill-column: 100
# End:
# vim: expandtab shiftwidth=4:

{'fraction_non_alphanumeric': 0.15398327163033046, 'fraction_numerical': 0.005896064719594131, 'mean_word_length': 2.3535632183908044, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5498346', 'n_tokens_mistral': 2459, 'n_tokens_neox': 2336, 'n_words': 659}