blib/lib/Formatter/HTML/Textile.pm

filter: Statementstmt 80.0% 32 / 40 Branchbran 50.0% 4 / 8 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.8% 8 / 9 Podpod 100% 5 / 5 totaltotal 75.0% 51 / 68 CC 7 SCAR 9.7
CC
7
Cov
73%
document
6.0
format
3.1
links
3.0
CRAP 2.6
↑ Summary
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<Formatter> API specification, version 1.0.
9  
10 =head1 SYNOPSIS
11  
12 my $textile = <<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<meta> 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<Formatter>, L<Text::Textile>
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 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 6 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
89  
90 6 our $VERSION = 1.02;
91  
92 6 ▶use base qw( Text::Textile );
Subroutine BEGIN: called CC=1 SCAR=0.0
93  
94 6 ▶sub format {
Subroutine format: called CC=3 SCAR=11.4
Pod documented
95 6 my $class = shift;
96 6 ▶ my $self = ref($class) ? $class : $class->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $class ? : 0 6 6
97 6 ▶ $self->{_text} = shift || "";
Condition50%1/2
shift() || ''
l!l
60
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
98 6 return $self;
99 }
100  
101 0 ▶sub document {
Subroutine document: not called CC=2 SCAR=17.9
Pod documented
102 0 my $self = shift;
103 0 my $charset = shift;
104 # TODO - holy cow this is a horrible hack. Make work, damnit. Needs docstrings,
105 # etc, etc, etc.
106 0 my $out = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n<html><head>";
107 0 ▶ if ($charset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset) 0 0 0
108 0 $out .= '<meta http-equiv="Content-Type" content="text/html; charset='.$charset.'">';
109 }
110 0 $out .= '<title>'
111 .$self->title
112 .'</title></head><body>'
113 .$self->fragment
114 .'</body></html>';
115 0 return $out;
116 }
117  
118  
119 8 ▶sub fragment {
Subroutine fragment: called CC=1 SCAR=0.0
Pod documented
120 8 my $self = shift;
121 8 return $self->process($self->{_text});
122 }
123  
124  
125  
126 1 ▶sub links {
Subroutine links: called CC=3 SCAR=11.0
Pod documented
127 1 my $self = shift;
128 1 my @arr;
129 1 require HTML::TokeParser;
130 1 my $p = HTML::TokeParser->new(\$self->fragment);
131  
132 1 ▶ while (my $token = $p->get_tag("a")) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $p->get_tag("a")) 1 1 2
133 1 ▶ my $url = $token->[1]{href} || "-";
Condition50%1/2
$token->[1]{'href'} || '-'
l!l
10
MC/DC0%0/1
$token->[1]{'href'} || '-'
$token->[1]{'href'}
Truth table50%1/2
A
A= $token->[1]{'href'}
Aresult
11
00
134 1 my $text = $p->get_trimmed_text("/a");
135 1 push(@arr, {url => $url, title => $text});
136 }
137 1 return \@arr;
138 }
139  
140  
141 1 ▶sub title {
Subroutine title: called CC=2 SCAR=7.6
Pod documented
142 1 my $self = shift;
143 1 ▶ if ( $self->{_text} =~ /^h1\.\s*(.*)$/im ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_text"} =~ /^h1\.\s*(.*)$/im) 1 0 1
144 1 return $1;
145 }
146 0 return undef;
147 }
148  
149  
150 6 1;
151  
↑ Summary