File Coverage

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/(.+)\<\/title\/?>//si); </td> </tr> <tr> <td class="h" > <a name="52">52</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 3 </td> <td class="s"> $content =~ s/\r//; </td> </tr> <tr> <td class="h" > <a name="53">53</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 3 </td> <td class="s"> $content =~ s/\s+\n\s+/\n/sg; </td> </tr> <tr> <td class="h" > <a name="54">54</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 9 </td> <td class="s"> my @paras = split /\<p\>/i, $content; </td> </tr> <tr> <td class="h" > <a name="55">55</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 4 </td> <td class="s"> foreach my $para (@paras) { </td> </tr> <tr> <td class="h" > <a name="56">56</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 4 </td> <td class="s"> $para =~ s/\<.+?\>//sg; # remove HTML tags </td> </tr> <tr> <td class="h" > <a name="57">57</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 5 </td> <td class="s"> $para =~ s/\s+\n\s+/\n/sg; </td> </tr> <tr> <td class="h" > <a name="58">58</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="59">59</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="60">60</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 3 </td> <td class="s"> my $out = ""; </td> </tr> <tr> <td class="h" > <a name="61">61</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="62">62</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 3 </td> <td class="s"> my $lim = 100; </td> </tr> <tr> <td class="h" > <a name="63">63</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 2 </td> <td class="s"> while (1) { </td> </tr> <tr> <td class="h" > <a name="64">64</a> </td> <td class="c3" > 4 </td> <td class="c3" > <a href="blib-lib-Text-Annotate-HTMLWriter-pm--branch.html#64-1"> 100 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td > 11 </td> <td class="s"> last if (!@paras); </td> </tr> <tr> <td class="h" > <a name="65">65</a> </td> <td class="c3" > 2 </td> <td class="c0" > <a href="blib-lib-Text-Annotate-HTMLWriter-pm--branch.html#65-1"> 50 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td > 6 </td> <td class="s"> last if (length $out > $lim); </td> </tr> <tr> <td class="h" > <a name="66">66</a> </td> <td class="c3" > 2 </td> <td class="c0" > <a href="blib-lib-Text-Annotate-HTMLWriter-pm--branch.html#66-1"> 50 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td > 11 </td> <td class="s"> $out .= ($out ? "<br>" : "").(shift @paras); </td> </tr> <tr> <td class="h" > <a name="67">67</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="68">68</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 38 </td> <td class="s"> $out =~ s/^(.{$lim}).+$/$1\.\.\./s; </td> </tr> <tr> <td class="h" > <a name="69">69</a> </td> <td class="c3" > 2 </td> <td class="c0" > <a href="blib-lib-Text-Annotate-HTMLWriter-pm--branch.html#69-1"> 50 </a> </td> <td >   </td> <td >   </td> <td >   </td> <td > 6 </td> <td class="s"> $out = "<br><b><em>$title</em></b><br>".$out if $title; </td> </tr> <tr> <td class="h" > <a name="70">70</a> </td> <td class="c3" > 2 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 17 </td> <td class="s"> (summary => $out, title => $title); </td> </tr> <tr> <td class="h" > <a name="71">71</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="72">72</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="73">73</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> sub wrap_wiki_content { </td> </tr> <tr> <td class="h" > <a name="74">74</a> </td> <td class="c3" > 1 </td> <td >   </td> <td >   </td> <td class="c3" > <a href="blib-lib-Text-Annotate-HTMLWriter-pm--subroutine.html#74-1"> 1 </a> </td> <td class="c0" > <a href="blib-lib-Text-Annotate-HTMLWriter-pm--subroutine.html#74-1"> 0 </a> </td> <td > 2 </td> <td class="s"> my $class = shift; </td> </tr> <tr> <td class="h" > <a name="75">75</a> </td> <td class="c3" > 1 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 2 </td> <td class="s"> my $vars = shift; </td> </tr> <tr> <td class="h" > <a name="76">76</a> </td> <td class="c3" > 1 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 2 </td> <td class="s"> $vars->{original_display} = $vars->{display}; </td> </tr> <tr> <td class="h" > <a name="77">77</a> </td> <td class="c3" > 1 </td> <td class="c0" > <a href="blib-lib-Text-Annotate-HTMLWriter-pm--branch.html#77-1"> 50 </a> </td> <td class="c0" > <a href="blib-lib-Text-Annotate-HTMLWriter-pm--condition.html#77-1"> 33 </a> </td> <td >   </td> <td >   </td> <td > 7 </td> <td class="s"> if ($vars->{display} && $vars->{annotations}) { </td> </tr> <tr> <td class="h" > <a name="78">78</a> </td> <td class="c3" > 1 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 2 </td> <td class="s"> my $display = $vars->{display}; </td> </tr> <tr> <td class="h" > <a name="79">79</a> </td> <td class="c3" > 1 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 3 </td> <td class="s"> my $annotations = $vars->{annotations}; </td> </tr> <tr> <td class="h" > <a name="80">80</a> </td> <td class="c3" > 1 </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td > 5 </td> <td class="s"> $vars->{display} = </td> </tr> <tr> <td class="h" > <a name="81">81</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> qq( </td> </tr> <tr> <td class="h" > <a name="82">82</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> <table> </td> </tr> <tr> <td class="h" > <a name="83">83</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> <tr><td valign="top"> </td> </tr> <tr> <td class="h" > <a name="84">84</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $display </td> </tr> <tr> <td class="h" > <a name="85">85</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> </td> </td> </tr> <tr> <td class="h" > <a name="86">86</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> <td valign="top"> </td> </tr> <tr> <td class="h" > <a name="87">87</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> $annotations </td> </tr> <tr> <td class="h" > <a name="88">88</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> </td> </td> </tr> <tr> <td class="h" > <a name="89">89</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> </table>); </td> </tr> <tr> <td class="h" > <a name="90">90</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="91">91</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="92">92</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="93">93</a> </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td >   </td> <td class="s"> 1; </td> </tr> </table> </body> </html>