Text::Smart::HTML - Smart text outputter for HTML


Text-Smart documentation Contained in the Text-Smart distribution.

Index


Code Index:

NAME

Top

Text::Smart::HTML - Smart text outputter for HTML

SYNOPSIS

Top

  use Text::Smart::HTML;

  my $markup = Text::Smart::HTML->new(%params);

DESCRIPTION

Top

METHODS

Top

my $proc = Text::Smart::HTML->new(target => $target);

Creates a new smart text processor which outputs HTML markup. The only target parameter is used to specify the hyperlink window target (via the 'target' attribute on the <a> tag)

my $markup = $proc->generate_divider

Generates a horizontal divider using the <hr> tag.

my $markup = $proc->generate_itemize(@items)

Generates an itemized list of bullet points using the <ul> tag.

my $markup = $proc->generate_enumeration(@items)

Generates an itemized list of numbered points using the <ol> tag

my $markup = $proc->generate_heading($text, $level)

Generates a heading using one of the tags <h1> through <h6>

my $markup = $proc->generate_paragraph($text)

Gnerates a paragraph using the <P> tag.

my $markup = $proc->generate_bold($text)

Generates bold text using the <strong> tag

my $markup = $proc->generate_italic($text)

Generates italic text using the <em> tag.

my $markup = $proc->generate_monospace($text)

Generates monospaced text using the <code> tag.

Generates a hyperlink using the <a> tag.

my $markup = $proc->generate_entity($text)

Generates entities using the &frac12;, &frac14;, &frac34;, &copy;, &reg; and <sup> TM </sup> entities / markup.

my $text = $proc->escape($text)

Escapes the ampersand, and angle bracket characters

AUTHORS

Top

Daniel Berrange <dan@berrange.com>

COPYRIGHT

Top

SEE ALSO

Top

perl(1)


Text-Smart documentation Contained in the Text-Smart distribution.
# -*- perl -*-
#
# Text::Smart::HTML by Daniel Berrange <dan@berrange.com>
#
# Copyright (C) 2000-2004 Daniel P. Berrange <dan@berrange.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# $Id: HTML.pm,v 1.2 2004/12/31 16:00:45 dan Exp $

package Text::Smart::HTML;

use strict;
use warnings;

use Text::Smart;

use vars qw(@ISA);

@ISA = qw(Text::Smart);

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = $class->SUPER::new();
    my %params = @_;

    $self->{target} = exists $params{target} ? $params{target} : undef;

    bless $self, $class;

    return $self;
}

sub generate_divider {
    my $self = shift;

    return "<hr>\n";
}

sub generate_itemize {
    my $self = shift;
    my @items = @_;

    return "<ul>\n" . (join("\n", map { "<li>$_</li>\n" } @items)) . "</ul>\n";
}

sub generate_enumeration {
    my $self = shift;
    my @items = @_;

    return "<ol>\n" . (join("\n", map { "<li>$_</li>\n" } @items)) . "</ol>\n";
}

sub generate_heading {
    my $self = shift;
    local $_ = $_[0];
    my $level = $_[1];

    my %levels = (
		  "title" => "h1",
		  "subtitle" => "h2",
		  "section" => "h3",
		  "subsection" => "h4",
		  "subsubsection" => "h5",
		  "paragraph" => "h6",
		  );

    return "<" . $levels{$level} . ">$_</" . $levels{$level} . ">\n";
}

sub generate_paragraph {
    my $self = shift;
    local $_ = $_[0];

    return "<p>$_</p>\n";
}

sub generate_bold {
    my $self = shift;
    local $_ = $_[0];

    return "<strong>$_</strong>";
}

sub generate_italic {
    my $self = shift;
    local $_ = $_[0];

    return "<em>$_</em>";
}

sub generate_monospace {
    my $self = shift;
    local $_ = $_[0];

    return "<code>$_</code>";
}


sub generate_link {
    my $self = shift;
    my $url = shift;
    local $_ = $_[0];

    if ($self->{target}) {
	return "<a target=\"$self->{target}\" href=\"$url\">$_</a>";
    } else {
	return "<a href=\"$url\">$_</a>";
    }
}


sub generate_entity {
    my $self = shift;
    my $entity = shift;

    my %entities = (
		    fraction12 => "&frac12;",
		    fraction14 => "&frac14;",
		    fraction34 => "&frac34;",
		    copyright => "&copy;",
		    registered => "&reg;",
		    trademark => "<sup>TM</sup>",
		    );

    return exists $entities{$entity} ? $entities{$entity} : $entity;
}

sub escape {
    my $self = shift;
    local $_ = $_[0];

    s/&/&amp;/g;
    s/</&lt;/g;
    s/>/&gt;/g;

    return $_;
}

1 # So that the require or use succeeds.

__END__