blib/lib/EBook/Ishmael/HTML.pm

filter: Statementstmt 100% 35 / 35 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 100% 43 / 43 CC 3 SCAR 5.6
CC
3
Cov
100%
prepare_html
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.8
« blib/lib/EBook/Ishmael/EBook.pm ↑ Summary blib/lib/EBook/Ishmael/Dir.pm »
1 package EBook::Ishmael::HTML;
2 17 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 our $VERSION = '2.04';
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 our @EXPORT_OK = qw(prepare_html text2html);
9  
10 sub text2html {
11  
12 12 ▶ my $text = shift;
Subroutine text2html: called CC=1 SCAR=0.0
Pod documented
13  
14 12 $text =~ s/&/&/g;
15 12 $text =~ s/</&lt;/g;
16 12 $text =~ s/>/&gt;/g;
17  
18 12 my @paras = split /(\s*\n){2,}/, $text;
19  
20 my $html = join '',
21 1392 map { "<p>\n" . $_ . "</p>\n" }
22 12 grep { /\S/ }
23 @paras;
24  
25 12 return $html;
26  
27 }
28  
29 17 my %STRIP_NODES = map { $_ => 1 } qw(img style);
30 17 my $STRIP_XPATH = join ' | ', map { "//$_" } keys %STRIP_NODES;
31  
32 sub prepare_html {
33  
34 62 ▶ my @nodes = @_;
Subroutine prepare_html: called CC=3 SCAR=11.0
Pod documented
35  
36 62 my $stripped = 0;
37  
38 62 for my $n (@nodes) {
39 62 my @children = $n->findnodes("//comment() | $STRIP_XPATH");
40 62 for my $c (@children) {
41 15 $c->unbindNode;
42 }
43 62 $stripped += scalar @children;
44 }
45  
46 62 return $stripped;
47  
48 }
49  
50 17 1;
51  
52 =head1 NAME
53  
54 EBook::Ishmael::HTML - Misc. HTML utilities
55  
56 =head1 SYNOPSIS
57  
58 use EBook::Ishmael::HTML qw(text2html prepare_html);
59  
60 my $html = text2html(<<"HERE");
61 A paragraph.
62  
63 Another paragraph!
64 HERE
65  
66 prepare_html(@nodes);
67  
68 =head1 DESCRIPTION
69  
70 B<EBook::Ishmael::HTML> is a module that provides various utilities for HTML
71 handling. This is a private module, please consult the L<ishmael> manual for
72 user documentation.
73  
74 =head1 SUBROUTINES
75  
76 =head2 $html = text2html($text)
77  
78 Converts the given string C<$text> to HTML, returning the HTML string.
79  
80 =head2 $stripped = prepare_html(@nodes)
81  
82 Prepares the give list of L<XML::LibXML::Node> nodes for dumping by removing
83 unnecessary elements. Returns the number of nodes removed.
84  
85 =head1 AUTHOR
86  
87 Written by Samuel Young, E<lt>samyoung12788@gmail.comE<gt>.
88  
89 This project's source can be found on its
90 L<Codeberg Page|https://codeberg.org/1-1sam/ishmael>. Comments and pull
91 requests are welcome!
92  
93 =head1 COPYRIGHT
94  
95 Copyright (C) 2025-2026 Samuel Young
96  
97 This program is free software: you can redistribute it and/or modify
98 it under the terms of the GNU General Public License as published by
99 the Free Software Foundation, either version 3 of the License, or
100 (at your option) any later version.
101  
102 =head1 SEE ALSO
103  
104 L<XML::LibXML>, L<XML::LibXML::Node>
105  
106 =cut
« blib/lib/EBook/Ishmael/EBook.pm ↑ Summary blib/lib/EBook/Ishmael/Dir.pm »