| 1 | package Formatter::HTML::Preformatted; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 2 | ▶ | use URI::Find::Simple qw( list_uris change_uris ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 2 | our $VERSION = '0.95'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | =head1 NAME | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | Formatter::HTML::Preformatted - Absolute minimal HTML formatting of pure text | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | use Formatter::HTML::Preformatted; | ||||||||||||||||||||
| 18 | my $formatter = Formatter::HTML::Preformatted->format($data); | ||||||||||||||||||||
| 19 | print $formatter->fragment; | ||||||||||||||||||||
| 20 | my @links = $text->links; | ||||||||||||||||||||
| 21 | print ${$links}[0]->{url}; | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | This module will simply take any text-string and put it in a HTML | ||||||||||||||||||||
| 26 | C<pre> element. It will escape tags and entities. It will also look | ||||||||||||||||||||
| 27 | through it to see if there are any URIs, and they will be turned into | ||||||||||||||||||||
| 28 | hyperlinks. | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | =head1 METHODS | ||||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | This module conforms with the L<Formatter> API specification, version 0.95: | ||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | =over | ||||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | =item C<format($string)> | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | The format function that you call to initialise the formatter. It | ||||||||||||||||||||
| 39 | takes the plain text as a string argument and returns an object of | ||||||||||||||||||||
| 40 | this class. | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | =cut | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | 1 | ▶ | sub format { | ||||||||||||||||||
|
Subroutine
format: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 45 | 1 | my $that = shift; | |||||||||||||||||||
| 46 | 1 | ▶ | my $class = ref($that) || $that; | ||||||||||||||||||
|
Condition33%1/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table33%1/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||
| 47 | 1 | my $self = { | |||||||||||||||||||
| 48 | _text => shift, | ||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||
| 50 | 1 | bless($self, $class); | |||||||||||||||||||
| 51 | 1 | return $self; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =item C<fragment> | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | To get only the text enclosed in the minimal C<pre> element, you will | ||||||||||||||||||||
| 57 | call this method. It returns a string with the HTML fragment. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =cut | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | 2 | ▶ | sub fragment { | ||||||||||||||||||
|
Subroutine
fragment: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 62 | 2 | my $self = shift; | |||||||||||||||||||
| 63 | 2 | my $raw = $self->{_text}; | |||||||||||||||||||
| 64 | # Escaping the stuff that needs escaping. | ||||||||||||||||||||
| 65 | 2 | $raw =~ s/&/&/g; | |||||||||||||||||||
| 66 | 2 | $raw =~ s/\>/>/g; | |||||||||||||||||||
| 67 | 2 | $raw =~ s/\</</g; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 2 | ▶ | return "<pre>\n" . change_uris($raw, sub { "<a href=\"$_[0]\">$_[0]</a>" }) . "\n</pre>\n"; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | =item C<document([$charset])> | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | Will add a document type declaration and some minimal markup, to | ||||||||||||||||||||
| 75 | return a full, valid, HTML document. You may specify an optional | ||||||||||||||||||||
| 76 | C<$charset> parameter. This will include a HTML C<meta> element with | ||||||||||||||||||||
| 77 | the chosen character set. It will still be your responsibility to | ||||||||||||||||||||
| 78 | ensure that the document served is encoded with this character set. | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =cut | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 1 | ▶ | sub document { | ||||||||||||||||||
|
Subroutine
document: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 83 | 1 | my $self = shift; | |||||||||||||||||||
| 84 | 1 | my $result = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">' . "\n<html>\n"; | |||||||||||||||||||
| 85 | 1 | my $charset = shift; | |||||||||||||||||||
| 86 | 1 | ▶ | if ($charset) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 87 | 1 | $result .= "<head>\n" . '<meta http-equiv="Content-Type" content="text/html; charset=' . $charset . '">' . "\n</head>\n"; | |||||||||||||||||||
| 88 | } | ||||||||||||||||||||
| 89 | 1 | $result .= "<body>\n" . $self->fragment . "\n</body>\n</html>\n"; | |||||||||||||||||||
| 90 | 1 | return $result; | |||||||||||||||||||
| 91 | } | ||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =item C<links> | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | Will return all links found the input plain text string. They will be | ||||||||||||||||||||
| 97 | found in an arrayref where each element has a key C<url>. | ||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 1 | ▶ | sub links { | ||||||||||||||||||
|
Subroutine
links: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 102 | 1 | my $self = shift; | |||||||||||||||||||
| 103 | 1 | my @arr; | |||||||||||||||||||
| 104 | 1 | foreach (list_uris($self->{_text})) { | |||||||||||||||||||
| 105 | 2 | push(@arr, {url => $_, title => ''}); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | 1 | return \@arr; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =item C<title> | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | Since this formatter has no way of finding the title of the document | ||||||||||||||||||||
| 113 | this method will always return C<undef>. | ||||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | =cut | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 0 | ▶ | sub title { | ||||||||||||||||||
|
Subroutine
title: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 119 | 0 | return undef; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | 2 | 1; | |||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||