File Coverage

blib/lib/Formatter/HTML/Preformatted.pm
Criterion Covered Total %
statement 36 37 97.3
branch 1 2 50.0
condition 1 3 33.3
subroutine 9 10 90.0
pod 5 5 100.0
total 52 57 91.2


line stmt bran cond sub pod time code
1             package Formatter::HTML::Preformatted;
2              
3 2     2   34349 use 5.006;
  2         8  
  2         90  
4 2     2   12 use strict;
  2         4  
  2         109  
5 2     2   13 use warnings;
  2         15  
  2         88  
6 2     2   2029 use URI::Find::Simple qw( list_uris change_uris );
  2         62766  
  2         931  
7              
8              
9             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
 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 API specification, version 0.95:
33              
34             =over
35              
36             =item C
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             sub format {
45 1     1 1 805 my $that = shift;
46 1   33     10 my $class = ref($that) || $that;
47 1         3 my $self = {
48             _text => shift,
49             };
50 1         3 bless($self, $class);
51 1         3 return $self;
52             }
53              
54             =item C
55              
56             To get only the text enclosed in the minimal C
 element, you will 
57             call this method. It returns a string with the HTML fragment.
58              
59             =cut
60              
61             sub fragment {
62 2     2 1 543 my $self = shift;
63 2         11 my $raw = $self->{_text};
64             # Escaping the stuff that needs escaping.
65 2         6 $raw =~ s/&/&/g;
66 2         5 $raw =~ s/\>/>/g;
67 2         6 $raw =~ s/\
68            
69 2     4   14 return "
\n" . change_uris($raw, sub { "$_[0]" }) . "\n
\n";
  4         38632  
70             }
71              
72             =item C
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 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             sub document {
83 1     1 1 817 my $self = shift;
84 1         3 my $result = '' . "\n\n";
85 1         3 my $charset = shift;
86 1 50       7 if ($charset) {
87 1         6 $result .= "\n" . '' . "\n\n";
88             }
89 1         4 $result .= "\n" . $self->fragment . "\n\n\n";
90 1         96 return $result;
91             }
92              
93              
94             =item C
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.
98              
99             =cut
100              
101             sub links {
102 1     1 1 1089 my $self = shift;
103 1         4 my @arr;
104 1         8 foreach (list_uris($self->{_text})) {
105 2         711 push(@arr, {url => $_, title => ''});
106             }
107 1         8 return \@arr;
108             }
109              
110             =item C </td> </tr> <tr> <td class="h" > <a name="111">111</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="112">112</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> Since this formatter has no way of finding the title of the document </td> </tr> <tr> <td class="h" > <a name="113">113</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> this method will always return C<undef>. </td> </tr> <tr> <td class="h" > <a name="114">114</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="115">115</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> =cut </td> </tr> <tr> <td class="h" > <a name="116">116</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="117">117</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="118">118</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> sub title { </td> </tr> <tr> <td class="h" > <a name="119">119</a> </td> <td class="c0" > 0 </td> <td >   </td> <td >   </td> <td class="c0" > <a href="blib-lib-Formatter-HTML-Preformatted-pm--subroutine.html#119-1"> 0 </a> </td> <td class="c3" > <a href="blib-lib-Formatter-HTML-Preformatted-pm--subroutine.html#119-1"> 1 </a> </td> <td >   </td> <td class="s"> return undef; </td> </tr> <tr> <td class="h" > <a name="120">120</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> } </td> </tr> <tr> <td class="h" > <a name="121">121</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="122">122</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="123">123</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="124">124</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="125">125</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s">   </td> </tr> <tr> <td class="h" > <a name="126">126</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> 1; </td> </tr> <tr> <td class="h" > <a name="127">127</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> __END__ </td> </tr> </table> </body> </html>