BASIS  r3148
Text.pm
Go to the documentation of this file.
00001 #============================================================= -*-Perl-*-
00002 #
00003 # Pod::POM::View::Text
00004 #
00005 # DESCRIPTION
00006 #   Text view of a Pod Object Model.
00007 #
00008 # AUTHOR
00009 #   Andy Wardley   <abw@kfs.org>
00010 #
00011 # COPYRIGHT
00012 #   Copyright (C) 2000 Andy Wardley.  All Rights Reserved.
00013 #
00014 #   This module is free software; you can redistribute it and/or
00015 #   modify it under the same terms as Perl itself.
00016 #
00017 # REVISION
00018 #   $Id: Text.pm 77 2009-08-20 20:44:14Z ford $
00019 #
00020 #========================================================================
00021 
00022 package BASIS::Pod::POM::View::Text;
00023 
00024 require 5.004;
00025 
00026 use strict;
00027 use BASIS::Pod::POM::View;
00028 use parent qw( BASIS::Pod::POM::View );
00029 use vars qw( $VERSION $DEBUG $ERROR $AUTOLOAD $INDENT );
00030 use Text::Wrap;
00031 
00032 $VERSION = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
00033 $DEBUG   = 0 unless defined $DEBUG;
00034 $INDENT  = 0;
00035 
00036 
00037 sub new {
00038     my $class = shift;
00039     my $args  = ref $_[0] eq 'HASH' ? shift : { @_ };
00040     bless { 
00041     INDENT => 0,
00042     %$args,
00043     }, $class;
00044 }
00045 
00046 
00047 sub view {
00048     my ($self, $type, $item) = @_;
00049 
00050     if ($type =~ s/^seq_//) {
00051     return $item;
00052     }
00053     elsif (UNIVERSAL::isa($item, 'HASH')) {
00054     if (defined $item->{ content }) {
00055         return $item->{ content }->present($self);
00056     }
00057     elsif (defined $item->{ text }) {
00058         my $text = $item->{ text };
00059         return ref $text ? $text->present($self) : $text;
00060     }
00061     else {
00062         return '';
00063     }
00064     }
00065     elsif (! ref $item) {
00066     return $item;
00067     }
00068     else {
00069     return '';
00070     }
00071 }
00072 
00073 
00074 sub view_head1 {
00075     my ($self, $head1) = @_;
00076     my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
00077     my $pad = ' ' x $$indent;
00078     local $Text::Wrap::unexpand = 0;
00079     my $title = wrap($pad, $pad, 
00080              $head1->title->present($self));
00081     
00082     $$indent += 4;
00083     my $output = "$title\n" . $head1->content->present($self);
00084     $$indent -= 4;
00085 
00086     return $output;
00087 }
00088 
00089 
00090 sub view_head2 {
00091     my ($self, $head2) = @_;
00092     my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
00093     my $pad = ' ' x $$indent;
00094     local $Text::Wrap::unexpand = 0;
00095     my $title = wrap($pad, $pad, 
00096              $head2->title->present($self));
00097 
00098     $$indent += 4;
00099     my $output = "$title\n" . $head2->content->present($self);
00100     $$indent -= 4;
00101 
00102     return $output;
00103 }
00104 
00105 
00106 sub view_head3 {
00107     my ($self, $head3) = @_;
00108     my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
00109     my $pad = ' ' x $$indent;
00110     local $Text::Wrap::unexpand = 0;
00111     my $title = wrap($pad, $pad, 
00112              $head3->title->present($self));
00113 
00114     $$indent += 4;
00115     my $output = "$title\n" . $head3->content->present($self);
00116     $$indent -= 4;
00117 
00118     return $output;
00119 }
00120 
00121 
00122 sub view_head4 {
00123     my ($self, $head4) = @_;
00124     my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
00125     my $pad = ' ' x $$indent;
00126     local $Text::Wrap::unexpand = 0;
00127     my $title = wrap($pad, $pad, 
00128              $head4->title->present($self));
00129 
00130     $$indent += 4;
00131     my $output = "$title\n" . $head4->content->present($self);
00132     $$indent -= 4;
00133 
00134     return $output;
00135 }
00136 
00137 
00138 #------------------------------------------------------------------------
00139 # view_over($self, $over)
00140 #
00141 # Present an =over block - this is a blockquote if there are no =items
00142 # within the block.
00143 #------------------------------------------------------------------------
00144 
00145 sub view_over {
00146     my ($self, $over) = @_;
00147 
00148     if (@{$over->item}) {
00149     return $over->content->present($self);
00150     }
00151     else {
00152     my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
00153     my $pad = ' ' x $$indent;
00154     $$indent += 4;
00155     my $content = $over->content->present($self);
00156     $$indent -= 4;
00157     
00158     return $content;
00159     }
00160 }
00161 
00162 sub view_item {
00163     my ($self, $item) = @_;
00164     my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
00165     my $pad = ' ' x $$indent;
00166     local $Text::Wrap::unexpand = 0;
00167     my $title = wrap($pad . '* ', $pad . '  ', 
00168              $item->title->present($self));
00169 
00170     $$indent += 2;
00171     my $content = $item->content->present($self);
00172     $$indent -= 2;
00173     
00174     return "$title\n\n$content";
00175 }
00176 
00177 
00178 sub view_for {
00179     my ($self, $for) = @_;
00180     return '' unless $for->format() =~ /\btext\b/;
00181     return $for->text()
00182     . "\n\n";
00183 }
00184 
00185     
00186 sub view_begin {
00187     my ($self, $begin) = @_;
00188     return '' unless $begin->format() =~ /\btext\b/;
00189     return $begin->content->present($self);
00190 }
00191 
00192     
00193 sub view_textblock {
00194     my ($self, $text) = @_;
00195     my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
00196     $text =~ s/\s+/ /mg;
00197 
00198     $$indent ||= 0;
00199     my $pad = ' ' x $$indent;
00200     local $Text::Wrap::unexpand = 0;
00201     return wrap($pad, $pad, $text) . "\n\n";
00202 }
00203 
00204 
00205 sub view_verbatim {
00206     my ($self, $text) = @_;
00207     my $indent = ref $self ? \$self->{ INDENT } : \$INDENT;
00208     my $pad = ' ' x $$indent;
00209     $text =~ s/^/$pad/mg;
00210     return "$text\n\n";
00211 }
00212 
00213 
00214 sub view_seq_bold {
00215     my ($self, $text) = @_;
00216     return "*$text*";
00217 }
00218 
00219 
00220 sub view_seq_italic {
00221     my ($self, $text) = @_;
00222     return "_${text}_";
00223 }
00224 
00225 
00226 sub view_seq_code {
00227     my ($self, $text) = @_;
00228     return "'$text'";
00229 }
00230 
00231 
00232 sub view_seq_file {
00233     my ($self, $text) = @_;
00234     return "_${text}_";
00235 }
00236 
00237 my $entities = {
00238     gt   => '>',
00239     lt   => '<',
00240     amp  => '&',
00241     quot => '"',
00242 };
00243 
00244 
00245 sub view_seq_entity {
00246     my ($self, $entity) = @_;
00247     return $entities->{ $entity } || $entity;
00248 }
00249 
00250 sub view_seq_index {
00251     return '';
00252 }
00253 
00254 sub view_seq_link {
00255     my ($self, $link) = @_;
00256     if ($link =~ s/^.*?\|//) {
00257     return $link;
00258     }
00259     else {
00260     return "the $link manpage";
00261     }
00262 }
00263     
00264     
00265 
00266 1;
00267 
00268 =head1 NAME
00269 
00270 Pod::POM::View::Text
00271 
00272 =head1 DESCRIPTION
00273 
00274 Text view of a Pod Object Model.
00275 
00276 =head1 METHODS
00277 
00278 =over 4
00279 
00280 =item C<view($self, $type, $item)>
00281 
00282 =item C<view_pod($self, $pod)>
00283 
00284 =item C<view_head1($self, $head1)>
00285 
00286 =item C<view_head2($self, $head2)>
00287 
00288 =item C<view_head3($self, $head3)>
00289 
00290 =item C<view_head4($self, $head4)>
00291 
00292 =item C<view_over($self, $over)>
00293 
00294 =item C<view_item($self, $item)>
00295 
00296 =item C<view_for($self, $for)>
00297 
00298 =item C<view_begin($self, $begin)>
00299 
00300 =item C<view_textblock($self, $textblock)>
00301 
00302 =item C<view_verbatim($self, $verbatim)>
00303 
00304 =item C<view_meta($self, $meta)>
00305 
00306 =item C<view_seq_bold($self, $text)>
00307 
00308 Returns the text of a C<BE<lt>E<gt>> sequence in 'bold' (i.e. surrounded by asterisks, like *this*).
00309 
00310 =item C<view_seq_italic($self, $text)>
00311 
00312 Returns the text of a C<IE<lt>E<gt>> sequence in 'italics' (i.e. surrounded by underscores, like _this_).
00313 
00314 =item C<view_seq_code($self, $text)>
00315 
00316 =item C<view_seq_file($self, $text)>
00317 
00318 =item C<view_seq_entity($self, $text)>
00319 
00320 =item C<view_seq_index($self, $text)>
00321 
00322 Returns an empty string.  Index sequences are suppressed in text view.
00323 
00324 =item C<view_seq_link($self, $text)>
00325 
00326 =back
00327 
00328 =head1 AUTHOR
00329 
00330 Andy Wardley E<lt>abw@kfs.orgE<gt>
00331 
00332 =head1 COPYRIGHT AND LICENSE
00333 
00334 Copyright (C) 2000 Andy Wardley.  All Rights Reserved.
00335 
00336 This module is free software; you can redistribute it and/or
00337 modify it under the same terms as Perl itself.
00338 
00339 =cut