File Coverage

blib/lib/Pod/Text/Overstrike.pm
Criterion Covered Total %
statement 65 69 94.2
branch 3 6 50.0
condition n/a
subroutine 15 16 93.7
pod 0 11 0.0
total 83 102 81.3


line stmt bran cond sub pod time code
1             # Pod::Text::Overstrike -- 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             # Created by Joe Smith 30-Nov-2000
15             # (based on Pod::Text::Color by Russ Allbery )
16             # Copyright 2000 Joe Smith .
17             # Copyright 2001, 2004, 2008, 2014 Russ Allbery .
18             #
19             # This program is free software; you may redistribute it and/or modify it
20             # under the same terms as Perl itself.
21              
22             ##############################################################################
23             # Modules and declarations
24             ##############################################################################
25              
26             package Pod::Text::Overstrike;
27              
28 2     2   23434 use 5.006;
  2         6  
29 2     2   27 use strict;
  2         3  
  2         52  
30 2     2   8 use warnings;
  2         4  
  2         99  
31              
32 2     2   12 use vars qw(@ISA $VERSION);
  2         5  
  2         127  
33              
34 2     2   334 use Pod::Text ();
  2         7  
  2         2511  
35              
36             @ISA = qw(Pod::Text);
37              
38             $VERSION = '4.09';
39              
40             ##############################################################################
41             # Overrides
42             ##############################################################################
43              
44             # Make level one headings bold, overridding any existing formatting.
45             sub cmd_head1 {
46 11     11 0 19 my ($self, $attrs, $text) = @_;
47 11         39 $text =~ s/\s+$//;
48 11         31 $text = $self->strip_format ($text);
49 11         198 $text =~ s/(.)/$1\b$1/g;
50 11         49 return $self->SUPER::cmd_head1 ($attrs, $text);
51             }
52              
53             # Make level two headings bold, overriding any existing formatting.
54             sub cmd_head2 {
55 2     2 0 7 my ($self, $attrs, $text) = @_;
56 2         17 $text =~ s/\s+$//;
57 2         6 $text = $self->strip_format ($text);
58 2         61 $text =~ s/(.)/$1\b$1/g;
59 2         18 return $self->SUPER::cmd_head2 ($attrs, $text);
60             }
61              
62             # Make level three headings underscored, overriding any existing formatting.
63             sub cmd_head3 {
64 2     2 0 4 my ($self, $attrs, $text) = @_;
65 2         9 $text =~ s/\s+$//;
66 2         5 $text = $self->strip_format ($text);
67 2         49 $text =~ s/(.)/_\b$1/g;
68 2         8 return $self->SUPER::cmd_head3 ($attrs, $text);
69             }
70              
71             # Level four headings look like level three headings.
72             sub cmd_head4 {
73 2     2 0 9 my ($self, $attrs, $text) = @_;
74 2         18 $text =~ s/\s+$//;
75 2         11 $text = $self->strip_format ($text);
76 2         65 $text =~ s/(.)/_\b$1/g;
77 2         19 return $self->SUPER::cmd_head4 ($attrs, $text);
78             }
79              
80             # The common code for handling all headers. We have to override to avoid
81             # interpolating twice and because we don't want to honor alt.
82             sub heading {
83 17     17 0 52 my ($self, $text, $indent, $marker) = @_;
84 17 50       72 $self->item ("\n\n") if defined $$self{ITEM};
85 17 50       52 $text .= "\n" if $$self{opt_loose};
86 17         106 my $margin = ' ' x ($$self{opt_margin} + $indent);
87 17         88 $self->output ($margin . $text . "\n");
88 17         233 return '';
89             }
90              
91             # Fix the various formatting codes.
92 12     12 0 38 sub cmd_b { local $_ = $_[0]->strip_format ($_[2]); s/(.)/$1\b$1/g; $_ }
  12         224  
  12         46  
93 3     3 0 17 sub cmd_f { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
  3         59  
  3         16  
94 16     16 0 51 sub cmd_i { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
  16         237  
  16         71  
95              
96             # Output any included code in bold.
97             sub output_code {
98 0     0 0 0 my ($self, $code) = @_;
99 0         0 $code =~ s/(.)/$1\b$1/g;
100 0         0 $self->output ($code);
101             }
102              
103             # Strip all of the formatting from a provided string, returning the stripped
104             # version.
105             sub strip_format {
106 77     77 0 129 my ($self, $text) = @_;
107 77         257 $text =~ s/(.)[\b]\1/$1/g;
108 77         174 $text =~ s/_[\b]//g;
109 77         209 return $text;
110             }
111              
112             # We unfortunately have to override the wrapping code here, since the normal
113             # wrapping code gets really confused by all the backspaces.
114             sub wrap {
115 101     101 0 138 my $self = shift;
116 101         134 local $_ = shift;
117 101         126 my $output = '';
118 101         195 my $spaces = ' ' x $$self{MARGIN};
119 101         183 my $width = $$self{opt_width} - $$self{MARGIN};
120 101         343 while (length > $width) {
121             # This regex represents a single character, that's possibly underlined
122             # or in bold (in which case, it's three characters; the character, a
123             # backspace, and a character). Use [^\n] rather than . to protect
124             # against odd settings of $*.
125 32         40 my $char = '(?:[^\n][\b])?[^\n]';
126 32 50       1318 if (s/^((?>$char){0,$width})(?:\Z|\s+)//) {
127 32         210 $output .= $spaces . $1 . "\n";
128             } else {
129 0         0 last;
130             }
131             }
132 101         255 $output .= $spaces . $_;
133 101         620 $output =~ s/\s+$/\n\n/;
134 101         461 return $output;
135             }
136              
137             ##############################################################################
138             # Module return value and documentation
139             ##############################################################################
140              
141             1;
142             __END__