File Coverage

blib/lib/EBook/Ishmael/HTML.pm
Criterion Covered Total %
statement 28 28 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 36 36 100.0


line stmt bran cond sub pod time code
1             package EBook::Ishmael::HTML;
2 17     17   249 use 5.016;
  17         46  
3             our $VERSION = '2.05';
4 17     17   66 use strict;
  17         23  
  17         285  
5 17     17   60 use warnings;
  17         23  
  17         739  
6              
7 17     17   74 use Exporter 'import';
  17         21  
  17         6639  
8             our @EXPORT_OK = qw(prepare_html text2html);
9              
10             sub text2html {
11              
12 12     12 1 1187 my $text = shift;
13              
14 12         81 $text =~ s/&/&/g;
15 12         380 $text =~ s/
16 12         462 $text =~ s/>/>/g;
17              
18 12         20587 my @paras = split /(\s*\n){2,}/, $text;
19              
20             my $html = join '',
21 1392         2561 map { "

\n" . $_ . "

\n" }
22 12         65 grep { /\S/ }
  2775         3226  
23             @paras;
24              
25 12         587 return $html;
26              
27             }
28              
29             my %STRIP_NODES = map { $_ => 1 } qw(img style);
30             # local-name() hack lets us ignore namespace prefixes
31             my $STRIP_XPATH = join ' | ', map { "//*[local-name() = '$_']" } keys %STRIP_NODES;
32              
33             sub prepare_html {
34              
35 62     62 1 125 my @nodes = @_;
36              
37 62         83 my $stripped = 0;
38              
39 62         107 for my $n (@nodes) {
40 62         250 my @children = $n->findnodes("//comment() | $STRIP_XPATH");
41 62         11545 for my $c (@children) {
42 20         90 $c->unbindNode;
43             }
44 62         141 $stripped += scalar @children;
45             }
46              
47 62         170 return $stripped;
48              
49             }
50              
51             1;
52              
53             =head1 NAME
54              
55             EBook::Ishmael::HTML - Misc. HTML utilities
56              
57             =head1 SYNOPSIS
58              
59             use EBook::Ishmael::HTML qw(text2html prepare_html);
60              
61             my $html = text2html(<<"HERE");
62             A paragraph.
63              
64             Another paragraph!
65             HERE
66              
67             prepare_html(@nodes);
68              
69             =head1 DESCRIPTION
70              
71             B is a module that provides various utilities for HTML
72             handling. This is a private module, please consult the L manual for
73             user documentation.
74              
75             =head1 SUBROUTINES
76              
77             =head2 $html = text2html($text)
78              
79             Converts the given string C<$text> to HTML, returning the HTML string.
80              
81             =head2 $stripped = prepare_html(@nodes)
82              
83             Prepares the give list of L nodes for dumping by removing
84             unnecessary elements. Returns the number of nodes removed.
85              
86             =head1 AUTHOR
87              
88             Written by Samuel Young, Esamyoung12788@gmail.comE.
89              
90             This project's source can be found on its
91             L. Comments and pull
92             requests are welcome!
93              
94             =head1 COPYRIGHT
95              
96             Copyright (C) 2025-2026 Samuel Young
97              
98             This program is free software: you can redistribute it and/or modify
99             it under the terms of the GNU General Public License as published by
100             the Free Software Foundation, either version 3 of the License, or
101             (at your option) any later version.
102              
103             =head1 SEE ALSO
104              
105             L, L
106              
107             =cut