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