File Coverage

blib/lib/Pod/Tree/Pod.pm
Criterion Covered Total %
statement 109 132 82.5
branch 30 40 75.0
condition n/a
subroutine 23 24 95.8
pod 2 2 100.0
total 164 198 82.8


line stmt bran cond sub pod time code
1             # Copyright (c) 2000-2003 by Steven McDougall. This module is free
2             # software; you can redistribute it and/or modify it under the same
3             # terms as Perl itself.
4              
5             package Pod::Tree::Pod;
6 2     2   4854804 use 5.006;
  2         15  
7 2     2   19 use strict;
  2         4  
  2         109  
8 2     2   16 use warnings;
  2         4  
  2         149  
9              
10             our $VERSION = '1.29';
11              
12 2     2   559 use IO::File;
  2         10090  
  2         271  
13 2     2   529 use Pod::Tree;
  2         4  
  2         1266  
14              
15             sub new {
16 6     6 1 97 my ( $class, $tree, $dest ) = @_;
17 6 50       12 defined $dest or die "Pod::Tree::Pod::new: not enough arguments\n";
18              
19 6         13 my $file = _resolve_dest($dest);
20              
21 6         16 my $pod = {
22             tree => $tree,
23             root => $tree->get_root,
24             file => $file,
25             interior => 0,
26             link => 0
27             };
28              
29 6         13 bless $pod, $class;
30             }
31              
32             sub _resolve_dest {
33 6     6   8 my $dest = shift;
34              
35 6 50       14 ref $dest and return $dest;
36              
37 0         0 my $fh = IO::File->new;
38 0 0       0 $fh->open(">$dest") or die "Pod::Tree::Pod::new: Can't open $dest: $!\n";
39 0         0 $fh;
40             }
41              
42             sub translate {
43 6     6 1 18 my $pod = shift;
44 6         9 my $root = $pod->{root};
45 6         15 $pod->_emit_children($root);
46             }
47              
48             sub _emit_children {
49 204     204   228 my ( $pod, $node ) = @_;
50              
51 204         284 my $children = $node->get_children;
52              
53 204         289 for my $child (@$children) {
54 410         489 $pod->_emit_node($child);
55             }
56             }
57              
58             sub _emit_siblings {
59 42     42   59 my ( $pod, $node ) = @_;
60              
61 42         347 my $siblings = $node->get_siblings;
62              
63 42         59 for my $sibling (@$siblings) {
64 47         62 $pod->_emit_node($sibling);
65             }
66             }
67              
68             sub _emit_node {
69 497     497   592 my ( $pod, $node ) = @_;
70 497         563 my $type = $node->{type};
71              
72 497         572 for ($type) {
73 497 100       721 /code/ and $pod->_emit_code($node);
74 497 100       674 /command/ and $pod->_emit_command($node);
75 497 100       709 /for/ and $pod->_emit_for($node);
76 497 100       666 /item/ and $pod->_emit_item($node);
77 497 100       651 /list/ and $pod->_emit_list($node);
78 497 100       711 /ordinary/ and $pod->_emit_ordinary($node);
79 497 100       693 /sequence/ and $pod->_emit_sequence($node);
80 497 100       839 /text/ and $pod->_emit_text($node);
81 497 100       1398 /verbatim/ and $pod->_emit_verbatim($node);
82             }
83             }
84              
85             sub _emit_code {
86 2     2   3 my ( $pod, $node ) = @_;
87 2         2 my $file = $pod->{file};
88 2         5 my $text = $node->get_text;
89              
90 2         4 $file->print($text);
91             }
92              
93             sub _emit_command {
94 23     23   29 my ( $pod, $node ) = @_;
95 23         25 my $file = $pod->{file};
96 23         38 my $raw = $node->get_raw;
97              
98 23         38 $file->print($raw);
99             }
100              
101             sub _emit_for {
102 3     3   5 my ( $pod, $node ) = @_;
103 3         6 my $file = $pod->{file};
104 3         5 my $brackets = $node->get_brackets;
105              
106 3         7 $file->print( $brackets->[0] );
107 3         11 $file->print( $node->get_text );
108 3 100       14 $file->print( $brackets->[1] ) if $brackets->[1];
109             }
110              
111             sub _emit_item {
112 42     42   50 my ( $pod, $node ) = @_;
113 42         48 my $file = $pod->{file};
114              
115 42         69 $file->print("=item ");
116 42         138 $pod->_emit_children($node);
117              
118 42         58 $pod->_emit_siblings($node);
119             }
120              
121             sub _emit_list {
122 19     19   28 my ( $pod, $node ) = @_;
123 19         24 my $file = $pod->{file};
124              
125 19         25 my $over = $node->get_raw;
126 19         35 $file->print($over);
127              
128 19         65 $pod->_emit_children($node);
129              
130 19         31 my $back = $node->get_back;
131 19 100       38 $back
132             and $file->print( $back->get_raw );
133             }
134              
135             sub _emit_ordinary {
136 88     88   103 my ( $pod, $node ) = @_;
137              
138 88         119 $pod->_emit_children($node);
139             }
140              
141             sub _emit_sequence {
142 76     76   86 my ( $pod, $node ) = @_;
143              
144 76         81 $pod->{interior}++;
145              
146 76         123 for ( $node->get_letter ) {
147 76 100       224 /I|B|C|E|F|S|X/ and $pod->_emit_element($node), last;
148 27 50       65 /L/ and $pod->_emit_link($node), last;
149             }
150              
151 76         232 $pod->{interior}--;
152             }
153              
154             sub _emit_element {
155 49     49   65 my ( $pod, $node ) = @_;
156              
157 49         72 my $letter = $node->get_letter;
158 49         58 my $file = $pod->{file};
159              
160 49         104 $file->print("$letter<");
161 49         169 $pod->_emit_children($node);
162 49         62 $file->print(">");
163             }
164              
165             sub _emit_link {
166 27     27   39 my ( $pod, $node ) = @_;
167              
168 27         35 my $file = $pod->{file};
169              
170 27         45 $file->print("L<");
171              
172 27         85 my $children = $node->get_raw_kids;
173 27         35 for my $child (@$children) {
174 40         48 $pod->_emit_node($child);
175             }
176              
177 27         41 $file->print(">");
178             }
179              
180             sub _emit_link_hide {
181 0     0   0 my ( $pod, $node ) = @_;
182              
183 0         0 my $file = $pod->{file};
184 0         0 my $target = $node->get_target;
185 0         0 my $page = $target->get_page;
186 0         0 my $section = $target->get_section;
187 0 0       0 my $slash = $section ? '/' : '';
188 0         0 my $link = "$page$slash$section";
189              
190 0 0       0 if ( $link eq $node->get_deep_text ) {
191 0         0 $file->print("L<");
192 0         0 $pod->_emit_children($node);
193 0         0 $file->print(">");
194             }
195             else {
196 0         0 $pod->{link}++;
197              
198 0         0 $file->print("L<");
199 0         0 $pod->_emit_children($node);
200              
201 0         0 $page = $pod->_escape($page);
202 0         0 $section = $pod->_escape($section);
203 0         0 $file->print("|$page$slash$section>");
204              
205 0         0 $pod->{link}--;
206             }
207             }
208              
209             sub _emit_text {
210 241     241   269 my ( $pod, $node ) = @_;
211 241         250 my $file = $pod->{file};
212 241         336 my $text = $node->get_text;
213              
214 241         311 $text = $pod->_escape($text);
215 241         378 $file->print($text);
216             }
217              
218             sub _escape {
219 241     241   286 my ( $pod, $text ) = @_;
220              
221 241         258 $text =~ s/^=(\w)/=Z<>$1/;
222              
223 241 100       338 if ( $pod->{interior} ) {
224 76         82 $text =~ s/([A-Z])/g;
225 76         85 $text =~ s/>/E/g;
226             }
227              
228 241 50       313 if ( $pod->{link} ) {
229 0         0 $text =~ s(\|)(E)g;
230 0         0 $text =~ s(/)(E)g;
231             }
232              
233 241         302 $text =~ s/([\x80-\xff])/sprintf("E<%d>", ord($1))/eg;
  12         34  
234              
235 241         334 $text;
236             }
237              
238             sub _emit_verbatim {
239 3     3   4 my ( $pod, $node ) = @_;
240 3         5 my $file = $pod->{file};
241 3         7 my $text = $node->get_text;
242              
243 3         7 $file->print($text);
244             }
245              
246             1
247              
248             __END__