App-DocKnot

 view release on metacpan or  search on metacpan

lib/App/DocKnot/Spin/Pointer.pm  view on Meta::CPAN

use Encode qw(decode);
use File::BaseDir qw(config_files);
use IPC::System::Simple qw(capturex);
use Path::Tiny qw(path);
use Pod::Thread 3.01 ();
use POSIX qw(strftime);
use Template ();
use YAML::XS ();

# The URL to the software page for this package, used to embed a link to the
# software that generated the page.
my $URL = 'https://www.eyrie.org/~eagle/software/docknot/';

##############################################################################
# Format conversions
##############################################################################

# Convert a Markdown file to HTML.
#
# $data_ref - Data from the pointer file
#   path  - Path to the Markdown file to convert
#   style - Style sheet to use
#   title - Title of the page
# $base     - Base path of pointer file (for relative paths)
# $output   - Path to the output file
#
# Throws: Text exception on conversion failure
sub _spin_markdown {
    my ($self, $data_ref, $base, $output) = @_;
    my $source = path($data_ref->{path})->absolute($base);

    # Do the Markdown conversion using pandoc.
    my $html = capturex(
        $self->{pandoc_path}, '--wrap=preserve', '-f', 'markdown',
        '-t', 'html', $source,
    );

    # Pull the title out of the contents of the <h1> header if not set.
    my $title = $data_ref->{title};
    if (!defined($title)) {
        ($title) = $html =~ m{ <h1 [^>]+ > (.*?) </h1> }xms;
    }

    # Construct the template variables.
    my ($links, $navbar, $style);
    if ($self->{sitemap}) {
        my $page = $output->relative($self->{output});
        my @links = $self->{sitemap}->links($page);
        if (@links) {
            $links = join(q{}, @links);
        }
        my @navbar = $self->{sitemap}->navbar($page);
        if (@navbar) {
            $navbar = join(q{}, @navbar);
        }
    }
    if ($data_ref->{style}) {
        $style = $self->{style_url} . $data_ref->{style} . '.css';
    }
    my %vars = (
        docknot_url => $URL,
        html        => decode('utf-8', $html),
        links       => $links,
        modified    => strftime('%Y-%m-%d', gmtime($source->stat()->[9])),
        navbar      => $navbar,
        now         => strftime('%Y-%m-%d', gmtime()),
        style       => $style,
        title       => $title,
    );

    # Construct the output page from those template variables.
    my $result;
    $self->{template}->process($self->{template_path}, \%vars, \$result)
      or croak($self->{template}->error());

    # Write the result to the output file.
    $output->spew_utf8($result);
    return;
}

# Convert a POD file to HTML.
#
# $data_ref - Data from the pointer file
#   options - Hash of conversion options
#     contents - Whether to add a table of contents
#     navbar   - Whether to add a navigation bar
#   path    - Path to the POD file to convert
#   style   - Style sheet to use
#   title   - Title of the page
# $base     - Base path of pointer file (for relative paths)
# $output   - Path to the output file
#
# Throws: Text exception on conversion failure
sub _spin_pod {
    my ($self, $data_ref, $base, $output) = @_;
    my $source = path($data_ref->{path})->absolute($base);

    # Construct the Pod::Thread formatter object.
    my %options = (
        contents => $data_ref->{options}{contents},
        style    => $data_ref->{style} // 'pod',
        title    => $data_ref->{title},
    );
    if (exists($data_ref->{options}{navbar})) {
        $options{navbar} = $data_ref->{options}{navbar};
    } else {
        $options{navbar} = 1;
    }
    my $podthread = Pod::Thread->new(%options);

    # Convert the POD to thread.
    my $data;
    $podthread->output_string(\$data);
    $podthread->parse_file("$source");
    $data = decode('utf-8', $data);

    # Spin that page into HTML.
    $self->{thread}->spin_thread_output($data, $source, 'POD', $output);
    return;
}



( run in 0.772 second using v1.01-cache-2.11-cpan-39bf76dae61 )