| blib/lib/Text/Annotate/HTMLWriter.pm | |||
|---|---|---|---|
| Criterion | Covered | Total | % |
| statement | 50 | 50 | 100.0 |
| branch | 12 | 22 | 54.5 |
| condition | 3 | 9 | 33.3 |
| subroutine | 8 | 8 | 100.0 |
| pod | 0 | 7 | 0.0 |
| total | 73 | 96 | 76.0 |
| line | stmt | bran | cond | sub | pod | time | code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | package Text::Annotate::HTMLWriter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | 1 | 5 | use strict; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1 | 2 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1 | 921 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | our $VERSION; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | $VERSION = 0.01_2; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | sub format_annotations { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 1 | 1 | 0 | 3 | my ($class, $h) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | 50 | 4 | $h = [$h] if (ref $h eq "HASH"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | 2 | my $out = ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | 4 | my $linkstyle = $class->css_link; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | 4 | my $parastyle = $class->css_para_class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | 3 | foreach my $e (@$h) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | 7 | $class->process_link ($e); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | 6 | my $link = $e->{link}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 2 | 4 | my $summary = $e->{summary}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | 3 | my $title = $e->{title}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 2 | 50 | 33 | 19 | next unless (defined $link && defined $summary && defined $title); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | # TODO: escaping? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | 11 | $out .= qq( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | $title |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | $summary); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | 4 | return $class->wrap_output($out); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # Application of CSS properly would remove the need for this. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | sub wrap_output { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | 1 | 0 | 2 | my ($self, $content) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 1 | 50 | 4 | return $content unless $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | 12 | return ''.$content.''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | 1 | 0 | 2 | sub css_para_class { 'class="annotation"' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | 1 | 0 | 2 | sub css_link { 'class="autogenerated"' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | sub process_link { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | 2 | 0 | 3 | my ($self, $h) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | 50 | 6 | unless (defined $h->{summary}) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # ordering fudge here. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 2 | 6 | %$h = ($self->html_to_brief_text ($h->{html_content}), %$h); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # ad-hoc fudge to summarise content. Should be done better - and should | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # be possible to add "plugins" which summarise known kinds of URL. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | sub html_to_brief_text { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | 2 | 0 | 3 | my ($self, $content) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | 50 | 6 | return undef unless defined $content; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 2 | 2 | my $title; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | # might bork hopelessly with XHTML | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 2 | 50 | 6 | $title = $1 if ($content =~ s/ |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 2 | 3 | $content =~ s/\r//; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 2 | 3 | $content =~ s/\s+\n\s+/\n/sg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | 9 | my @paras = split /\ /i, $content; |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | 4 | foreach my $para (@paras) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 2 | 4 | $para =~ s/\<.+?\>//sg; # remove HTML tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 2 | 5 | $para =~ s/\s+\n\s+/\n/sg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | 3 | my $out = ""; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | 3 | my $lim = 100; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | 2 | while (1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 4 | 100 | 11 | last if (!@paras); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | 50 | 6 | last if (length $out > $lim); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | 50 | 11 | $out .= ($out ? " " : "").(shift @paras); |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 2 | 38 | $out =~ s/^(.{$lim}).+$/$1\.\.\./s; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | 50 | 6 | $out = " $title ".$out if $title; |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 2 | 17 | (summary => $out, title => $title); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | sub wrap_wiki_content { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | 1 | 0 | 2 | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | 2 | my $vars = shift; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | 2 | $vars->{original_display} = $vars->{display}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | 50 | 33 | 7 | if ($vars->{display} && $vars->{annotations}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | 2 | my $display = $vars->{display}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | 3 | my $annotations = $vars->{annotations}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 1 | 5 | $vars->{display} = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | qq( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 |
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 1; |