Text/Overstrike.pm

filter: Statementstmt 94.2% 66 / 70 Branchbran 70.0% 7 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 92.8% 13 / 14 Podpod 0.0% 0 / 11 totaltotal 81.9% 86 / 105 CC 6 SCAR 7.0
CC
6
Cov
91%
wrap
4.0
heading
3.1
output_code
2.0
CRAP 2.0
« Text/Color.pm ↑ Summary
1 # Convert POD data to formatted overstrike text
2 #
3 # This was written because the output from:
4 #
5 # pod2text Text.pm > plain.txt; less plain.txt
6 #
7 # is not as rich as the output from
8 #
9 # pod2man Text.pm | nroff -man > fancy.txt; less fancy.txt
10 #
11 # and because both Pod::Text::Color and Pod::Text::Termcap are not device
12 # independent.
13 #
14 # SPDX-License-Identifier: GPL-1.0-or-later OR Artistic-1.0-Perl
15  
16 ##############################################################################
17 # Modules and declarations
18 ##############################################################################
19  
20 package Pod::Text::Overstrike v6.1.1;
21  
22 2 ▶use 5.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use parent qw(Pod::Text);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 ##############################################################################
27 # Overrides
28 ##############################################################################
29  
30 # Make level one headings bold, overriding any existing formatting.
31 14 ▶sub cmd_head1 {
Subroutine cmd_head1: called CC=1 SCAR=0.0
Pod undocumented
32 14 my ($self, $attrs, $text) = @_;
33 14 $text =~ s/\s+$//;
34 14 $text = $self->strip_format ($text);
35 14 $text =~ s/(.)/$1\b$1/g;
36 14 return $self->SUPER::cmd_head1 ($attrs, $text);
37 }
38  
39 # Make level two headings bold, overriding any existing formatting.
40 2 ▶sub cmd_head2 {
Subroutine cmd_head2: called CC=1 SCAR=0.0
Pod undocumented
41 2 my ($self, $attrs, $text) = @_;
42 2 $text =~ s/\s+$//;
43 2 $text = $self->strip_format ($text);
44 2 $text =~ s/(.)/$1\b$1/g;
45 2 return $self->SUPER::cmd_head2 ($attrs, $text);
46 }
47  
48 # Make level three headings underscored, overriding any existing formatting.
49 2 ▶sub cmd_head3 {
Subroutine cmd_head3: called CC=1 SCAR=0.0
Pod undocumented
50 2 my ($self, $attrs, $text) = @_;
51 2 $text =~ s/\s+$//;
52 2 $text = $self->strip_format ($text);
53 2 $text =~ s/(.)/_\b$1/g;
54 2 return $self->SUPER::cmd_head3 ($attrs, $text);
55 }
56  
57 # Level four headings look like level three headings.
58 2 ▶sub cmd_head4 {
Subroutine cmd_head4: called CC=1 SCAR=0.0
Pod undocumented
59 2 my ($self, $attrs, $text) = @_;
60 2 $text =~ s/\s+$//;
61 2 $text = $self->strip_format ($text);
62 2 $text =~ s/(.)/_\b$1/g;
63 2 return $self->SUPER::cmd_head4 ($attrs, $text);
64 }
65  
66 # The common code for handling all headers. We have to override to avoid
67 # interpolating twice and because we don't want to honor alt.
68 20 ▶sub heading {
Subroutine heading: called CC=3 SCAR=11.2
Pod undocumented
69 20 my ($self, $text, $indent, $marker) = @_;
70 20 ▶ $self->item ("\n\n") if defined $$self{ITEM};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"ITEM"} 0 20 20
71 20 ▶ $text .= "\n" if $$self{opt_loose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"opt_loose"} 0 20 20
72 20 my $margin = ' ' x ($$self{opt_margin} + $indent);
73 20 $self->output ($margin . $text . "\n");
74 20 return '';
75 }
76  
77 # Fix the various formatting codes.
78 13 ▶sub cmd_b { local $_ = $_[0]->strip_format ($_[2]); s/(.)/$1\b$1/g; $_ }
Subroutine cmd_b: called CC=1 SCAR=0.0
Pod undocumented
79 3 ▶sub cmd_f { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
Subroutine cmd_f: called CC=1 SCAR=0.0
Pod undocumented
80 16 ▶sub cmd_i { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
Subroutine cmd_i: called CC=1 SCAR=0.0
Pod undocumented
81  
82 # Output any included code in bold.
83 0 ▶sub output_code {
Subroutine output_code: not called CC=1 SCAR=6.9
Pod undocumented
84 0 my ($self, $code) = @_;
85 0 $code =~ s/(.)/$1\b$1/g;
86 0 $self->output ($code);
87 }
88  
89 # Strip all of the formatting from a provided string, returning the stripped
90 # version.
91 82 ▶sub strip_format {
Subroutine strip_format: called CC=1 SCAR=0.0
Pod undocumented
92 82 my ($self, $text) = @_;
93 82 $text =~ s/(.)[\b]\1/$1/g;
94 82 $text =~ s/_[\b]//g;
95 82 return $text;
96 }
97  
98 # We unfortunately have to override the wrapping code here, since the normal
99 # wrapping code gets really confused by all the backspaces.
100 109 ▶sub wrap {
Subroutine wrap: called CC=4 SCAR=13.9
Pod undocumented
101 109 my $self = shift;
102 109 local $_ = shift;
103 109 my $output = '';
104 109 my $spaces = ' ' x $$self{MARGIN};
105 109 my $width = $$self{opt_width} - $$self{MARGIN};
106  
107 # Pathological margins make wrapping impossible. In this case, complain to
108 # the user and reset the margin to 0.
109 109 ▶ if ($width <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($width <= 0) 1 108 109
110 1 my $error = 'Margin is wider than the output width';
111 1 $self->whine($self->line_count(), $error);
112 1 $spaces = q{};
113 1 $width = $self->{opt_width};
114 }
115  
116 # This regex represents a single character, that's possibly underlined or
117 # in bold (in which case, it's three characters; the character, a
118 # backspace, and a character). Use [^\n] rather than . to protect against
119 # odd settings of $*.
120 109 my $char = '(?:[^\n][\b])?[^\n]';
121  
122 # Perform the wrapping. After we find a break point, remove any
123 # underlined or bold spaces left over in the input text after the break
124 # point.
125 109 ▶ while (length > $width) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_ > $width) 37 109 146
126 37 ▶ if (s/^((?>$char){0,$width})(?:\Z|[ \t\n]+)//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^((?>$char){0,$width})(?:\Z|[ \t\n]+)//u) { } 37 0 37
127 37 $output .= $spaces . $1 . "\n";
128 37 s/^(?:[\b][ _])?(?:[ ][\b][ ])*//;
129 } else {
130 0 last;
131 }
132 }
133 109 $output .= $spaces . $_;
134 109 $output =~ s/\s+$/\n\n/;
135 109 return $output;
136 }
137  
138 ##############################################################################
139 # Module return value and documentation
140 ##############################################################################
141  
142 2 1;
143 __END__
« Text/Color.pm ↑ Summary