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   22117 use 5.006;
  2         5  
29 2     2   8 use strict;
  2         2  
  2         33  
30 2     2   6 use warnings;
  2         3  
  2         64  
31              
32 2     2   7 use vars qw(@ISA $VERSION);
  2         2  
  2         78  
33              
34 2     2   230 use Pod::Text ();
  2         3  
  2         1082  
35              
36             @ISA = qw(Pod::Text);
37              
38             $VERSION = '4.08';
39              
40             ##############################################################################
41             # Overrides
42             ##############################################################################
43              
44             # Make level one headings bold, overridding any existing formatting.
45             sub cmd_head1 {
46 11     11 0 14 my ($self, $attrs, $text) = @_;
47 11         28 $text =~ s/\s+$//;
48 11         22 $text = $self->strip_format ($text);
49 11         142 $text =~ s/(.)/$1\b$1/g;
50 11         34 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 4 my ($self, $attrs, $text) = @_;
56 2         6 $text =~ s/\s+$//;
57 2         4 $text = $self->strip_format ($text);
58 2         26 $text =~ s/(.)/$1\b$1/g;
59 2         8 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 3 my ($self, $attrs, $text) = @_;
65 2         8 $text =~ s/\s+$//;
66 2         3 $text = $self->strip_format ($text);
67 2         37 $text =~ s/(.)/_\b$1/g;
68 2         9 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 5 my ($self, $attrs, $text) = @_;
74 2         5 $text =~ s/\s+$//;
75 2         4 $text = $self->strip_format ($text);
76 2         30 $text =~ s/(.)/_\b$1/g;
77 2         9 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 23 my ($self, $text, $indent, $marker) = @_;
84 17 50       44 $self->item ("\n\n") if defined $$self{ITEM};
85 17 50       30 $text .= "\n" if $$self{opt_loose};
86 17         34 my $margin = ' ' x ($$self{opt_margin} + $indent);
87 17         61 $self->output ($margin . $text . "\n");
88 17         123 return '';
89             }
90              
91             # Fix the various formatting codes.
92 12     12 0 26 sub cmd_b { local $_ = $_[0]->strip_format ($_[2]); s/(.)/$1\b$1/g; $_ }
  12         153  
  12         30  
93 3     3 0 6 sub cmd_f { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
  3         24  
  3         5  
94 16     16 0 29 sub cmd_i { local $_ = $_[0]->strip_format ($_[2]); s/(.)/_\b$1/g; $_ }
  16         165  
  16         37  
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 82 my ($self, $text) = @_;
107 77         160 $text =~ s/(.)[\b]\1/$1/g;
108 77         87 $text =~ s/_[\b]//g;
109 77         120 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 79 my $self = shift;
116 101         77 local $_ = shift;
117 101         83 my $output = '';
118 101         116 my $spaces = ' ' x $$self{MARGIN};
119 101         107 my $width = $$self{opt_width} - $$self{MARGIN};
120 101         196 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         26 my $char = '(?:[^\n][\b])?[^\n]';
126 32 50       703 if (s/^((?>$char){0,$width})(?:\Z|\s+)//) {
127 32         130 $output .= $spaces . $1 . "\n";
128             } else {
129 0         0 last;
130             }
131             }
132 101         126 $output .= $spaces . $_;
133 101         375 $output =~ s/\s+$/\n\n/;
134 101         281 return $output;
135             }
136              
137             ##############################################################################
138             # Module return value and documentation
139             ##############################################################################
140              
141             1;
142             __END__