File Coverage

blib/lib/Text/Highlight/HTML.pm
Criterion Covered Total %
statement 4 24 16.6
branch 0 8 0.0
condition n/a
subroutine 2 6 33.3
pod 0 4 0.0
total 6 42 14.2


line stmt bran cond sub pod time code
1             package Text::Highlight::HTML;
2 1     1   5 use strict;
  1         2  
  1         1882  
3              
4             #TODO:
5             # 1) DONE - Convert some tag list into a hashref syntax tree for sub syntax
6             # 2) More distant goal: rewrite this not using HTML::SyntaxHighlighter
7             # as I'm not much of a fan of its highlighting method. I'd prefer
8             # html tags one color, attrib names another, and attrib vals as
9             # strings. This wouldn't highlight the around the tags. Maybe
10             # this is just how whatever text editors I've used have done it,
11             # but it feels more "right".
12             #
13             # At the very least, I'd keep an option for using it as-is. This
14             # means creating some kind of optional "options" hashref that gets
15             # passed to individual T::H::foo methods. Maybe have one all should
16             # implement that uses the method builtin to T::H itself. Got some
17             # other shit to iron out first.
18             # 3) Looks like this method is html-escaping all text, which may not
19             # be the desired escape method. Something else to look into when
20             # poking here again.
21              
22             #array of types, allows for span tag nesting
23             my @types = (undef);
24              
25             #HTML::SyntaxHighlighter to Text::Highlight class translations
26             my %classes = (
27             'D' => 'key4', #DTD
28             'H' => 'key3', #html/head/body
29             'B' => 'key1', #block
30             'I' => 'key2', #inline
31             'A' => 'string', #attribute values
32             'T' => undef, #text
33             'S' => 'key5', #script/style
34             'C' => 'comment',
35             );
36              
37             sub highlight
38             {
39 0     0 0 0 shift; #class method's class name
40 0         0 my $obj = shift;
41 0         0 my $code = shift;
42              
43 0         0 eval {
44 0         0 require HTML::SyntaxHighlighter;
45 0         0 require HTML::Parser;
46             };
47 0 0       0 if ( $@ ) {
48 0         0 $obj->{_active} = __PACKAGE__->syntax;
49 0         0 $obj->_highlight($code);
50 0         0 return;
51             }
52            
53 0         0 my $out;
54 0         0 my $p = HTML::SyntaxHighlighter->new(br => "", out_func => \$out);
55 0         0 $p->parse($code);
56            
57             #HTML::Entity's decode_entities method doesn't seem to convert nbsp's into spaces
58             # (and some lazy html omits the semi-colon)
59 0         0 $out =~ s/ ?/ /g;
60              
61             HTML::Parser->new(
62             api_version => 3,
63             handlers => {
64             start => [\&start, 'tagname,attr'],
65             end => [\&end, 'tagname'],
66             #colorize the decoded text as the type on top of the @types stack
67 0     0   0 text => [ sub { $obj->_colorize($types[-1], shift) }, 'dtext'],
  0         0  
68              
69             },
70             )->parse($out);
71             }
72              
73             #if it's a span tag, look up the tag's class for its Highlighter type and push it on the stack
74             sub start
75             {
76 0 0   0 0 0 return if shift ne 'span';
77 0         0 my $attr = shift;
78 0 0       0 push @types, exists $classes{$attr->{class}} ? $classes{$attr->{class}} : undef;
79             }
80              
81             #if it's a span tag, pop the top type off the stack
82             sub end
83             {
84 0 0   0 0 0 pop @types if shift eq 'span';
85             }
86              
87             sub syntax {
88             return {
89 1     1 0 743 'name' => 'HTML',
90             'blockCommentOn' => [
91             ''
521             ],
522             'escape' => '\\',
523             'continueQuote' => 0
524             };
525             }
526              
527             1;
528             __END__