App-DocKnot

 view release on metacpan or  search on metacpan

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

    my $line = $self->_next_line();
    if (_is_id($line)) {
        chomp($line);
        $header{id} = $line;
        $self->_skip_blank_lines();
        $line = $self->_next_line();
    }

    # Check for the type of document.  First, see if it looks like a FAQ with
    # news/mail headers, and if so read those headers and the subheaders.
    # Otherwise, skip over leading blank lines and rules.
    $self->_buffer_line($line);
    if (!$self->{title} && (_is_header($line) || $line =~ m{ \A From }xms)) {
        $self->_handle_faq_headers(\%header);
    }
    $self->_skip_blank_lines_and_rules();

    # See if we have a centered title at the top of the document.  If so,
    # we'll make that the document title unless we also saw a Subject header
    # or a constructor argument.  Titles shouldn't be in all caps, though.
    $line = $self->_next_line();
    if (_is_centered($line)) {
        $header{heading} = whitechomp($line);
        if (!defined($header{title})) {
            $header{title} = $header{heading};
            if (_is_allcaps($header{title})) {
                $header{title} =~ s{ \b ([A-Z]+) \b }{\L\u$1}xmsg;
            }
        }
        $self->_skip_blank_lines_and_rules();
    } else {
        $self->_buffer_line($line);
        $header{heading} = $header{title} // $self->{title};
    }

    # Return the parsed header.
    return \%header;
}

# Parse the subheaders of a text document and generate the subheaders for the
# output document.  The author information from the headers will be included,
# as will the last modified date if configured.  Existing subheadings that
# look like they're just Revision or Date strings will be replaced by a
# nicely-formatted string.
#
# $header_ref - Main headers of the text document
#
# Returns: List of lists of subheaders to put at the top of the output
#          document
sub _parse_subheaders {
    my ($self, $header_ref) = @_;
    my (@subheaders, $modified);

    # Generate a last modified date if we have an RCS/CVS Id string or if a
    # last modified subheader from the file modification time was requested.
    # We'll set $modified back to undef if we push it into the subheaders at
    # any point; otherwise, we'll add it at the end.
    if ($header_ref->{id}) {
        $modified = modified_id($header_ref->{id});
    } elsif ($self->{modified} && defined($self->{in_path})) {
        $modified = modified_timestamp($self->{in_path}->stat()->[9]);
    }

    # Parse subheaders.  The first must be centered; after that, assume
    # everything is a subheading until a blank line.
    my $line;
    while (defined($line = $self->_next_line())) {
        next if _is_rule($line);
        last if $line =~ m{ \A \s* \z }xms;

        # For cases other than a rule or blank line, we have to either be in a
        # subheading or the line must be centered.
        last if !(@subheaders || _is_centered($line));

        # A subheading to add.  Replace Revision and Date keywords with our
        # modified timestamp if we have one.
        if ($modified && $line =~ m{ [\$] (?: Revision | Date ) }xms) {
            push(@subheaders, $modified);
            $modified = undef;
        } else {
            push(@subheaders, _format_urls(escape(whitechomp($line))));
        }
    }
    $self->_buffer_line($line);
    $self->_skip_blank_lines_and_rules();

    # If there is no subheading, but we have an author from the file headings,
    # create a subheading with that information.
    if (!@subheaders && $header_ref->{author}) {
        push(@subheaders, escape($header_ref->{author}));
        if ($header_ref->{original}) {
            push(
                @subheaders,
                '(originally by ' . escape($header_ref->{original}) . ')',
            )
        }
    }

    # If we have modification information and haven't output it yet, add that
    # to the subheading.
    if (defined($modified)) {
        push(@subheaders, $modified);
    }

    # Return what we have.
    return @subheaders;
}

##############################################################################
# Document conversion
##############################################################################

# Convert a document from text to HTML.
#
# $in_fh    - Input file handle
# $in_path  - Input path
# $out_fh   - Output file handle
# $out_path - Output path
sub _convert_document {
    my ($self, $in_fh, $in_path, $out_fh, $out_path) = @_;



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