File Coverage

blib/lib/Formatter/HTML/Textile.pm
Criterion Covered Total %
statement 30 38 78.9
branch 2 6 33.3
condition 2 4 50.0
subroutine 8 9 88.8
pod 5 5 100.0
total 47 62 75.8


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Formatter::HTML::Textile - Formatter to make HTML from Textile
4              
5             =head1 DESCRIPTION
6              
7             This module will format Textile input to HTML. It conforms
8             with the L API specification, version 1.0.
9              
10             =head1 SYNOPSIS
11              
12             my $textile = <
13             h1. textile document
14            
15             this is a "textile":http://textism.com/tools/textile/ document
16             TEXTILE
17              
18             my $formatter = Formatter::HTML::Textile->format( $textile );
19              
20             print "title is ".$formatter->title."\n";
21             print $formatter->document;
22            
23             my @links = @{ $formatter->links };
24             print "Links urls: ";
25             print join ", " map { $_->{url} } @links;
26             print "\n";
27              
28             =head1 METHODS
29              
30             =over 4
31              
32             =item format($string)
33              
34             This is a constructor method and initializes the formatter with the
35             passed text.
36              
37             This method returns a Formatter::HTML::Textile object.
38              
39             =item document()
40              
41             It returns a full HTML document, comprising the formatted textile
42             source converted to HTML. You may specify an optional C<$charset>
43             parameter. This will include a HTML C element with the chosen
44             character set. It will still be your responsibility to ensure that the
45             document served is encoded with this character set.
46              
47             =item fragment()
48              
49             returns a minimal HTML chunk as textile.
50              
51             =item links()
52              
53             Returns all the links found in the document, as a listref of hashrefs,
54             with keys 'title', which is the title of the link, and 'url', which is
55             the link.
56              
57             =item title()
58              
59             Returns the title of the document
60              
61             =back
62              
63             =head1 SEE ALSO
64              
65             L, L
66              
67             =head1 AUTHOR
68              
69             Originally written by Tom Insam, maintained by Kjetil Kjernsmo from
70             2005-11-19.
71              
72             =head1 COPYRIGHT
73              
74             Copyright 2005 Tom Insam tom@jerakeen.org, 2005, 2009 Kjetil Kjernsmo,
75             kjetilk@cpan.org.
76              
77             This library is free software; you can redistribute it and/or modify
78             it under the same terms as Perl itself, either Perl version 5.8.4 or,
79             at your option, any later version of Perl 5 you may have available.
80              
81              
82             =cut
83              
84              
85             package Formatter::HTML::Textile;
86 6     6   124328 use warnings;
  6         16  
  6         279  
87 6     6   33 use strict;
  6         11  
  6         6697  
88 6     6   68 use Carp qw( croak );
  6         12  
  6         650  
89              
90             our $VERSION = 1.02;
91              
92 6     6   38 use base qw( Text::Textile );
  6         10  
  6         9777  
93              
94             sub format {
95 6     6 1 6044 my $class = shift;
96 6 50       77 my $self = ref($class) ? $class : $class->new;
97 6   50     1541 $self->{_text} = shift || "";
98 6         30 return $self;
99             }
100              
101             sub document {
102 0     0 1 0 my $self = shift;
103 0         0 my $charset = shift;
104             # TODO - holy cow this is a horrible hack. Make work, damnit. Needs docstrings,
105             # etc, etc, etc.
106 0         0 my $out = "\n";
107 0 0       0 if ($charset) {
108 0         0 $out .= '';
109             }
110 0         0 $out .= '' </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"> .$self->title </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"> .''
113             .$self->fragment
114             .'';
115 0         0 return $out;
116             }
117              
118              
119             sub fragment {
120 8     8 1 4444 my $self = shift;
121 8         61 return $self->process($self->{_text});
122             }
123              
124              
125              
126             sub links {
127 1     1 1 4249 my $self = shift;
128 1         3 my @arr;
129 1         1072 require HTML::TokeParser;
130 1         6086 my $p = HTML::TokeParser->new(\$self->fragment);
131              
132 1         754 while (my $token = $p->get_tag("a")) {
133 1   50     89 my $url = $token->[1]{href} || "-";
134 1         6 my $text = $p->get_trimmed_text("/a");
135 1         58 push(@arr, {url => $url, title => $text});
136             }
137 1         51 return \@arr;
138             }
139              
140              
141             sub title {
142 1     1 1 5152 my $self = shift;
143 1 50       13 if ( $self->{_text} =~ /^h1\.\s*(.*)$/im ) {
144 1         7 return $1;
145             }
146 0           return undef;
147             }
148              
149              
150             1;
151