App-DocKnot

 view release on metacpan or  search on metacpan

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

# baseline, be more accepting about headers.
#
# If we're inside a contents block, be even more careful and disallow numbered
# things that look like a heading unless they're outdented.
#
# Unlike most of the classification functions, this is a regular method, since
# it needs access to the parsing state.
#
# $paragraph - Paragraph to classify
#
# Returns: True if a heading, false otherwise
sub _is_heading {
    my ($self, $paragraph) = @_;
    $paragraph = _unescape($paragraph);
    my $indent = indent($paragraph);
    my $nobase = !defined($self->{baseline});
    my $outdented = defined($self->{baseline}) && $indent < $self->{baseline};

    # Numbered lines inside the contents section are definitely not headings.
    my $numbered = $paragraph =~ m{ \A [\d.]+[.\)] \s }xms;
    return if !$outdented && $self->{contents} && $numbered;

    # Outdented single lines are headings as long as they're either short or
    # contain at least two words.
    if ($outdented && lines($paragraph) == 1) {
        return 1 if $paragraph =~ m{ \S \s \S }xms;
        return 1 if length($paragraph) < 30;
    }

    # Indented lines are never headings.
    return if defined($INDENT) && $indent > $INDENT;

    # Lines of at most 31 characters ending in a word character or closing
    # quote or paren are headings if they're underlined.
    return 1 if $paragraph =~ m{
        \A \s*
        [ \w\"\(\),:./&-]{0,30} [\w\"\)] \s* \n
        [-=~]+ \s*
        \z
    }xms;

    # All-uppercase lines of at most 31 characters ending in an uppercase
    # character, digit, or closing quote or paren are headings.
    return 1 if $paragraph =~ m{
        \A \s*
        [ [:upper:]\d\"\(\),:./&-]{0,30} [[:upper:]\d\"\)]
        \s* \n
        \z
    }xms;

    # If there is no baseline, assume single lines of at most 34 characters
    # with no unexpected characters are headings.
    return $nobase && $paragraph =~ m{
        \A \s*
        [ \w\"\(\),:./&-]{0,33} [\w\"\)]
        \s* \n
        \z
    }xms;
}

# Whether a line is an RCS/CVS Id string that has been expanded.
#
# $line - Line to classify
#
# Returns: True if so, false otherise
sub _is_id {
    my ($line) = @_;
    return $line =~ m{ \A \s* [\$]Id: \N+ [\$] \s* \z }xms;
}

# Whether a paragraph should be a literal paragraph, decided based on whether
# it has internal whitespace.
#
# $paragraph - Paragraph to classify
#
# Returns: True if so, false otherwise
sub _is_literal {
    my ($paragraph) = @_;
    return $paragraph =~ m{
        \A [ \t]*
        \S \N*
        (?: [^.?!\"\)\]:*_\n] [ ] [ ] | [ ] [ ] [ ] | \t )
        \S
    }xms;
}

# Whether a paragarph is part of a numbered list.
#
# $paragraph - Paragraph to classify
#
# Returns: The number if the paragraph is a numbered list element
#          undef otherwise
sub _is_numbered {
    my ($paragraph) = @_;
    if ($paragraph =~ m{ \A \s* (\d\d?) [.\)] \s }xms) {
        return $1;
    } else {
        return undef;
    }
}

# Whether a paragraph has inconsistent indentation.
#
# $paragraph - Paragraph to classify
#
# Returns: True if so, false otherwise
sub _is_offset {
    my ($paragraph) = @_;

    # Strip off a leading bullet or number and consider it whitespace in
    # making this check.
    $paragraph =~ s{ \A (\s* (?: \d\d? ) [.\)] \s) }{ q{ } x length($1) }xmse;
    $paragraph =~ s{ \A (\s* [-*o] \s) }{ q{ } x length($1) }xmse;

    # Now, return true if the indentation isn't consistent.
    return $paragraph !~ m{ \A (\s*) \S \N* \n (\1 \S \N* \n)* \s* \z }xms;
}

# Whether a paragraph is quoted.  Requires the paragraph be at least two
# lines, since otherwise we cannot detect a common prefix.
#



( run in 0.626 second using v1.01-cache-2.11-cpan-5623c5533a1 )