View/Text.pm
Go to the documentation of this file.
1 #============================================================= -*-Perl-*-
2 #
3 # Pod::POM::View::Text
4 #
5 # DESCRIPTION
6 # Text view of a Pod Object Model.
7 #
8 # AUTHOR
9 # Andy Wardley <abw@kfs.org>
10 #
11 # COPYRIGHT
12 # Copyright (C) 2000 Andy Wardley. All Rights Reserved.
13 #
14 # This module is free software; you can redistribute it and/or
15 # modify it under the same terms as Perl itself.
16 #
17 # REVISION
18 # $Id: Text.pm 77 2009-08-20 20:44:14Z ford $
19 #
20 #========================================================================
21 
22 package BASIS::Pod::POM::View::Text;
23 
24 require 5.004;
25 
26 use strict;
27 use BASIS::Pod::POM::View;
28 use parent qw( BASIS::Pod::POM::View );
29 use vars qw( $VERSION $DEBUG $ERROR $AUTOLOAD $INDENT );
30 use Text::Wrap;
31 
32 $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
33 $DEBUG = 0 unless defined $DEBUG;
34 $INDENT = 0;
35 
36 
37 sub new {
38  my $class = shift;
39  my $args = ref $_[0] eq 'HASH' ? shift : { @_ };
40  bless {
41  INDENT => 0,
42  %$args,
43  }, $class;
44 }
45 
46 
47 sub view {
48  my ($self, $type, $item) = @_;
49 
50  if ($type =~ s/^seq_//) {
51  return $item;
52  }
53  elsif (UNIVERSAL::isa($item, 'HASH')) {
54  if (defined $item->{ content }) {
55  return $item->{ content }->present($self);
56  }
57  elsif (defined $item->{ text }) {
58  my $text = $item->{ text };
59  return ref $text ? $text->present($self) : $text;
60  }
61  else {
62  return '';
63  }
64  }
65  elsif (! ref $item) {
66  return $item;
67  }
68  else {
69  return '';
70  }
71 }
72 
73 
74 sub view_head1 {
75  my ($self, $head1) = @_;
76  my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
77  my $pad = ' ' x $$indent;
78  local $Text::Wrap::unexpand = 0;
79  my $title = wrap($pad, $pad,
80  $head1->title->present($self));
81 
82  $$indent += 4;
83  my $output = "$title\n" . $head1->content->present($self);
84  $$indent -= 4;
85 
86  return $output;
87 }
88 
89 
90 sub view_head2 {
91  my ($self, $head2) = @_;
92  my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
93  my $pad = ' ' x $$indent;
94  local $Text::Wrap::unexpand = 0;
95  my $title = wrap($pad, $pad,
96  $head2->title->present($self));
97 
98  $$indent += 4;
99  my $output = "$title\n" . $head2->content->present($self);
100  $$indent -= 4;
101 
102  return $output;
103 }
104 
105 
106 sub view_head3 {
107  my ($self, $head3) = @_;
108  my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
109  my $pad = ' ' x $$indent;
110  local $Text::Wrap::unexpand = 0;
111  my $title = wrap($pad, $pad,
112  $head3->title->present($self));
113 
114  $$indent += 4;
115  my $output = "$title\n" . $head3->content->present($self);
116  $$indent -= 4;
117 
118  return $output;
119 }
120 
121 
122 sub view_head4 {
123  my ($self, $head4) = @_;
124  my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
125  my $pad = ' ' x $$indent;
126  local $Text::Wrap::unexpand = 0;
127  my $title = wrap($pad, $pad,
128  $head4->title->present($self));
129 
130  $$indent += 4;
131  my $output = "$title\n" . $head4->content->present($self);
132  $$indent -= 4;
133 
134  return $output;
135 }
136 
137 
138 #------------------------------------------------------------------------
139 # view_over($self, $over)
140 #
141 # Present an =over block - this is a blockquote if there are no =items
142 # within the block.
143 #------------------------------------------------------------------------
144 
145 sub view_over {
146  my ($self, $over) = @_;
147 
148  if (@{$over->item}) {
149  return $over->content->present($self);
150  }
151  else {
152  my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
153  my $pad = ' ' x $$indent;
154  $$indent += 4;
155  my $content = $over->content->present($self);
156  $$indent -= 4;
157 
158  return $content;
159  }
160 }
161 
162 sub view_item {
163  my ($self, $item) = @_;
164  my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
165  my $pad = ' ' x $$indent;
166  local $Text::Wrap::unexpand = 0;
167  my $title = wrap($pad . '* ', $pad . ' ',
168  $item->title->present($self));
169 
170  $$indent += 2;
171  my $content = $item->content->present($self);
172  $$indent -= 2;
173 
174  return "$title\n\n$content";
175 }
176 
177 
178 sub view_for {
179  my ($self, $for) = @_;
180  return '' unless $for->format() =~ /\btext\b/;
181  return $for->text()
182  . "\n\n";
183 }
184 
185 
186 sub view_begin {
187  my ($self, $begin) = @_;
188  return '' unless $begin->format() =~ /\btext\b/;
189  return $begin->content->present($self);
190 }
191 
192 
193 sub view_textblock {
194  my ($self, $text) = @_;
195  my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
196  $text =~ s/\s+/ /mg;
197 
198  $$indent ||= 0;
199  my $pad = ' ' x $$indent;
200  local $Text::Wrap::unexpand = 0;
201  return wrap($pad, $pad, $text) . "\n\n";
202 }
203 
204 
205 sub view_verbatim {
206  my ($self, $text) = @_;
207  my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
208  my $pad = ' ' x $$indent;
209  $text =~ s/^/$pad/mg;
210  return "$text\n\n";
211 }
212 
213 
214 sub view_seq_bold {
215  my ($self, $text) = @_;
216  return "*$text*";
217 }
218 
219 
220 sub view_seq_italic {
221  my ($self, $text) = @_;
222  return "_${text}_";
223 }
224 
225 
226 sub view_seq_code {
227  my ($self, $text) = @_;
228  return "'$text'";
229 }
230 
231 
232 sub view_seq_file {
233  my ($self, $text) = @_;
234  return "_${text}_";
235 }
236 
237 my $entities = {
238  gt => '>',
239  lt => '<',
240  amp => '&',
241  quot => '"',
242 };
243 
244 
245 sub view_seq_entity {
246  my ($self, $entity) = @_;
247  return $entities->{ $entity } || $entity;
248 }
249 
250 sub view_seq_index {
251  return '';
252 }
253 
254 sub view_seq_link {
255  my ($self, $link) = @_;
256  if ($link =~ s/^.*?\|//) {
257  return $link;
258  }
259  else {
260  return "the $link manpage";
261  }
262 }
263 
264 
265 
266 1;
267 
268 =head1 NAME
269 
270 Pod::POM::View::Text
271 
272 =head1 DESCRIPTION
273 
274 Text view of a Pod Object Model.
275 
276 =head1 METHODS
277 
278 =over 4
279 
280 =item C<view($self, $type, $item)>
281 
282 =item C<view_pod($self, $pod)>
283 
284 =item C<view_head1($self, $head1)>
285 
286 =item C<view_head2($self, $head2)>
287 
288 =item C<view_head3($self, $head3)>
289 
290 =item C<view_head4($self, $head4)>
291 
292 =item C<view_over($self, $over)>
293 
294 =item C<view_item($self, $item)>
295 
296 =item C<view_for($self, $for)>
297 
298 =item C<view_begin($self, $begin)>
299 
300 =item C<view_textblock($self, $textblock)>
301 
302 =item C<view_verbatim($self, $verbatim)>
303 
304 =item C<view_meta($self, $meta)>
305 
306 =item C<view_seq_bold($self, $text)>
307 
308 Returns the text of a C<BE<lt>E<gt>> sequence in 'bold' (i.e. surrounded by asterisks, like *this*).
309 
310 =item C<view_seq_italic($self, $text)>
311 
312 Returns the text of a C<IE<lt>E<gt>> sequence in 'italics' (i.e. surrounded by underscores, like _this_).
313 
314 =item C<view_seq_code($self, $text)>
315 
316 =item C<view_seq_file($self, $text)>
317 
318 =item C<view_seq_entity($self, $text)>
319 
320 =item C<view_seq_index($self, $text)>
321 
322 Returns an empty string. Index sequences are suppressed in text view.
323 
324 =item C<view_seq_link($self, $text)>
325 
326 =back
327 
328 =head1 AUTHOR
329 
330 Andy Wardley E<lt>abw@kfs.orgE<gt>
331 
332 =head1 COPYRIGHT AND LICENSE
333 
334 Copyright (C) 2000 Andy Wardley. All Rights Reserved.
335 
336 This module is free software; you can redistribute it and/or
337 modify it under the same terms as Perl itself.
338 
339 =cut