RST/Text.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 77 CC 4 SCAR 18.3
CC
4
Cov
0%
raw_html
20.0
new
2.0
text
2.0
CRAP 6.2
1 package ICANN::RST::Text;
2 # ABSTRACT: an object representing Markdown text.
3 use Alien::pandoc;
4 use Digest::SHA qw(sha1_hex);
5 use Encode qw(decode_utf8);
6 use Env qw( @PATH );
7 use File::Slurp;
8 use File::Spec;
9 use IPC::Open2;
10 use Text::Unidecode;
11 use utf8;
12 use strict;
13  
14 unshift(@PATH, Alien::pandoc->bin_dir);
15  
16 my $CACHE;
17  
18 sub new {
19 my ($package, $text) = @_;
20 return bless({'text' => $text}, $package);
21 }
22  
23 sub text { $_[0]->{'text'} }
24  
25 sub html {
26 my ($self, $shift) = @_;
27  
28 return '<div class="markdown-content">'.$self->raw_html($shift).'</div>';
29 }
30  
31 sub raw_html {
32 my ($self, $shift) = @_;
33  
34 my $key = sprintf('%s.%u', sha1_hex(unidecode($self->text)), $shift);
35  
36 if (!defined($CACHE->{$key})) {
37 my $f = File::Spec->catfile(File::Spec->tmpdir, $key.'.html');
38  
39 unless (-e $f) {
40 my @cmd = (qw(pandoc -f markdown -t html -o), $f);
41  
42 push(@cmd, sprintf('--shift-heading-level-by=%u', $shift)) if ($shift > 0);
43  
44 my $pid = open2(undef, my $in, @cmd);
45  
46 $in->binmode(':encoding(UTF-8)');
47 $in->print($self->text),
48 $in->close;
49  
50 waitpid($pid, 0);
51 }
52  
53 $CACHE->{$key} = read_file($f, 'binmode' => ':encoding(UTF-8)');
54 }
55  
56 return $CACHE->{$key};
57 }
58  
59 1;
60  
61 __END__
62  
63 =pod
64  
65 =encoding UTF-8
66  
67 =head1 NAME
68  
69 ICANN::RST::Text - an object representing Markdown text.
70  
71 =head1 VERSION
72  
73 version 0.03
74  
75 =head1 METHODS
76  
77 =head2 new($text)
78  
79 Constructor. C<$text> is a string.
80  
81 =head2 html($heading_shift)
82  
83 Convert to HTML. C<$heading_shift> is an integer which modifies the level of heading elements (eg a shift of 1 turns all C<h1> elements into C<h2>, C<h2> to C<h3> etc).
84  
85 =head1 AUTHOR
86  
87 Gavin Brown <gavin.brown@icann.org>
88  
89 =head1 COPYRIGHT AND LICENSE
90  
91 This software is copyright (c) 2025 by Internet Corporation for Assigned Names and Number (ICANN).
92  
93 This is free software; you can redistribute it and/or modify it under
94 the same terms as the Perl 5 programming language system itself.
95  
96 =cut