App-DocKnot

 view release on metacpan or  search on metacpan

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

# Convert some particular text formats into HTML.
#
# This program is an ad hoc set of heuristics and tricks, attempting to
# convert a few text file formats that I commonly use into reasonable HTML.
# General text to XHTML conversions is impossible due to the wildly differing
# formats used by people when writing text, so this module doesn't try to
# solve the general problem.  It's good enough to turn the FAQs I maintain
# into HTML documents, which is all that I need of it.
#
# SPDX-License-Identifier: MIT

##############################################################################
# Modules and declarations
##############################################################################

package App::DocKnot::Spin::Text v8.0.1;

use 5.024;
use autodie;
use warnings FATAL => 'utf8';

use vars qw($INDENT @INDENT);

use App::DocKnot;
use App::DocKnot::Util qw(print_fh);
use Path::Tiny qw(path);
use POSIX qw(strftime);

# Replace with the month names you want to use, if you don't want English.
our @MONTHS = qw(January February March April May June July August September
                 October November December);

##############################################################################
# Utility functions
##############################################################################

# Turns section numbers at the beginning of lines in a paragraph into links.
#
# $text - Text to format
#
# Returns: Text formatted as links to section numbers given by the numbers at
#          the start of each line.
sub _format_contents {
    my ($text) = @_;
    $text =~ s{
        ^
        (\s* ([\d.]+) [.\)] \s+ )
        (.*?)
        ([ \t]*\n)
    }{$1<a href="#S$2">$3</a>$4}xmsg;
    return $text;
}

# Turns *some text* into <strong>some text</strong>, while trying to be
# careful to avoid other uses of wildcards.
#
# $string - Text to format
#
# Returns: Text with bold replaced with HTML markup.
sub _format_bold {
    my ($text) = @_;
    $text =~ s{
        (^|\s) [*] ( \w .*? \S ) [*] ([,.!?;\s])
    }{$1<strong>$2</strong>$3}xmsg;
    return $text;

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

sub _next_line {
    my ($self) = @_;
    my $line;
    if (defined($self->{buffer})) {
        $line = $self->{buffer};
        $self->{buffer} = undef;
    } else {
        $line = readline($self->{in_fh});
    }
    return $line;
}

# Read a paragraph, including all of its trailing blank lines.  By default,
# lines with nothing but whitespace are paragraph dividers.
#
# $require_blank - If true, only completely blank lines are dividers
sub _next_paragraph {
    my ($self, $require_blank) = @_;
    my $paragraph = $self->_next_line();
    my $in_fh = $self->{in_fh};
    my $nonblank_line = $require_blank ? qr{ [^\n] }xms : qr{ \S }xms;

    my $line;
    while (defined($line = <$in_fh>) && $line =~ $nonblank_line) {
        $paragraph .= $line;
    }
    if (defined($line)) {
        $paragraph .= $line;
    }
    while (defined($line = <$in_fh>) && $line =~ m{ \A \s* \z }xms) {
        $paragraph .= $line;
    }
    $self->_buffer_line($line);
    return $paragraph;
}

# Read from the input file descriptor, skipping blank lines.
sub _skip_blank_lines {
    my ($self) = @_;
    my $line;
    do {
        $line = $self->_next_line();
    } while (defined($line) && $line !~ m{ \S }xms);
    $self->_buffer_line($line);
}

# Read from the input file descriptor, skipping blank lines and rules.
sub _skip_blank_lines_and_rules {
    my ($self) = @_;
    my $line;
    do {
        $line = $self->_next_line();
    } while (defined($line) && ($line !~ m{ \S }xms || _is_rule($line)));
    $self->_buffer_line($line);
}

##############################################################################
# HTML constructors
##############################################################################

# Output the header of the HTML document.  We claim "transitional" XHTML 1.0
# compliance; we can't claim strict solely because we use the value attribute
# in <li> in the absence of widespread implementation of CSS Level 2.  Assume
# English output.
#
# $header_ref - Additional information from the headers of the text document
sub _output_header {
    my ($self, $header_ref) = @_;
    $self->_output(
        '<?xml version="1.0" encoding="utf-8"?>', "\n",
        '<!DOCTYPE html', "\n",
        '    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"', "\n",
        '    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">', "\n",
        "\n",
        '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">',
        "\n",
        '<head>', "\n",
        q{  }, title($self->{title} // $header_ref->{title} // q{}), "\n",
    );
    if ($self->{style}) {
        $self->_output(q{  }, style($self->{style}), "\n");
    }
    $self->_output(
        q{  },
        '<meta http-equiv="content-type" content="text/html; charset=utf-8"',
        " />\n",
    );
    if ($self->{sitemap}) {
        if (defined($self->{output}) && defined($self->{out_path})) {
            my $page = $self->{out_path}->relative($self->{output});
            $self->_output($self->{sitemap}->links($page));
        }
    }
    $self->_output("</head>\n\n");
    if ($header_ref->{id}) {
        $self->_output(comment($header_ref->{id}), "\n");
    }

    # Add a generator comment.
    my $date = strftime('%Y-%m-%d %T -0000', gmtime());
    my $version = $App::DocKnot::VERSION;
    $self->_output(comment("Converted by DocKnot $version on $date"), "\n\n");
}

# An XML comment.
sub comment {
    my @data = @_;
    my $data = join ('', @data);
    '<!-- ' . $data . ' -->';
}

# A link to a CSS style sheet.
sub style {
    my $style = shift;
    qq(<link rel="stylesheet" href="$style" type="text/css" />);
}

# Wrap a container around data, keeping the tags on the same line.
sub container {
    my ($tag, @data) = @_;
    my $data = join ('', @data);
    $data = '<' . $tag . '>' . $data;
    $tag =~ s/ .*//;
    $data =~ s%(\s*)$%</$tag>$1%;
    $data;
}

# Output a list item.  Takes the indentation, the item, and an optional third
# argument, which if specified is the number to use for the item (using the
# value attribute, which for some reason is deprecated under HTML 4.0 without
# any viable alternative for what I use it for).
sub li {

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

App::DocKnot::Spin::Text understands digest separators (lines of exactly
thirty hyphens, from the minimal digest standard) and will treat a C<Subject>
header immediately after them as a section header.  Beyond that, headings must
either be outdented, underlined on the following line, or in all caps to be
recognized as section headers.  (Outdenting means that the regular text is
indented by a few spaces, but headers start in column 0, or at least in a
column farther to the left than the regular text.)

Section headers that begin with numbers (with any number of periods) will be
given C<< <a id> >> tags containing that number prepended with C<S>.  As a
special case of the parsing, any section with a header containing C<contents>
will have lines beginning with numbers turned into links to the appropriate <a
id> tags in the same document.  You can use this to turn the table of contents
of your minimal digest format FAQ into a real table of contents with links in
the HTML version.

Text with embedded whitespace more than a single space or a couple of spaces
at a sentence boundary or after a colon (and any text with literal tabs) will
be wrapped in C<< <pre> >> tags.  So will any indented text that doesn't look
like English paragraphs.  URLs surrounded by C<< <...> >> or C<< <URL:...> >>
will be turned into links.  Other URLs will not be turned into links, nor is
any effort made to turn random body text into links because it happens to look
like a link.

Bullet lists and numbered lists will be turned into the appropriate HTML
structures.  Some attempt is also made to recognize description lists, but
App::DocKnot::Spin::Text was written by someone who writes a lot of technical
documentation and therefore tends to prefer C<< <pre> >> if unsure whether
something is a description list or preformatted text.  Description lists are
therefore only going to work if the description titles aren't indented
relative to the surrounding text.

Regular indented paragraphs or paragraphs quoted with a consistent
non-alphanumeric quote character are recognized and turned into HTML block
quotes.

It's worthwhile paying attention to the headers at the top of your document so
that App::DocKnot::Spin::Text can get a few things right.  If you use RCS or
CVS, put the RCS C<Id> keyword as the first line of your document; it will be
stripped out of the resulting output and App::DocKnot::Spin::Text will use it
to determine the document revision.  This should be followed by regular
message headers and news.answers subheaders if the document is an actual FAQ,
and App::DocKnot::Spin::Text will use the C<From> and C<Subject> headers to
figure out a title and headings to use.  As a special case, an HTML-title
header in the subheaders will override any other title that
App::DocKnot::Spin::Text thinks it should use for the document.

App::DocKnot::Spin::Text expects your document to have an C<< <h1> >> title,
and will add one from the Subject header if it doesn't find one.  It will also
add subheaders (C<class="subheading">) giving the author (from the C<From>
header) and the last modified time and revision (from the RCS C<Id> string) if
there are no subheadings already.  If there's a subheading that contains RCS
identifiers, it will be replaced by a nicely formatted heading generated from
the RCS C<Id> information in the HTML output.

Text marked as C<*bold*> using the standard asterisk notation will be
surrounded by C<< <strong> >> tags, if the asterisks appear to be marking bold
text rather than serving as wildcards or some other function.

App::DocKnot::Spin::Text produces output (at least in the absence of any
lurking bugs) which complies with the XHTML 1.0 Transitional standard.  The
input and output character set is assumed to be UTF-8.

=head1 CLASS METHODS

=over 4

=item new(ARGS)

Create a new App::DocKnot::Spin::Text object.  A single converter object can
be reused to convert multiple files provided that they have the same options.
ARGS should be a hash reference with one or more of the following keys, all of
which are optional:

=over 4

=item output

The path to the root of the output tree when converting a tree of files.  This
will be used to calculate relative path names for generating inter-page links
using the provided C<sitemap> argument.  If C<sitemap> is given, this option
should also always be given.

=item modified

Add a last modified subheader to the document.  This will always be done if an
RCS C<Id> string is present in the input.  Otherwise, a last modified
subheader based on the last modification date of the input file will be added
if the input is a file and this option is set to a true value.  The default is
false.

=item sitemap

An App::DocKnot::Spin::Sitemap object.  This will be used to create inter-page
links.  For inter-page links, the C<output> argument must also be provided.

=item style

The URL to the style sheet to use.  The appropriate HTML will be added to the
C<< <head> >> section of the resulting document.

=item title

The HTML page title to use.  This will also be used as the C<< <h1> >> heading
if the document doesn't contain one, but will not override a heading found in
the document (only the HTML C<< <title> >> attribute).

=back

=back

=head1 INSTANCE METHODS

=over 4

=item spin_text_file([INPUT[, OUTPUT]])

Convert a single text file to HTML.  INPUT is the path of the input file and
OUTPUT is the path of the output file.  OUTPUT or both INPUT and OUTPUT may be
omitted, in which case standard input or standard output, respectively, will
be used.



( run in 0.757 second using v1.01-cache-2.11-cpan-d7f47b0818f )