summaryrefslogtreecommitdiffstats
path: root/doc/podtbl
blob: 8d910e4603bd30fc65c6b5c53116df3a47f1ec68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/perl

use Pod::Parser;
use List::Util qw(max);

@ISA = Pod::Parser::;

sub stripfcodes {
   # strip formatting codes, dumb version
   map {
         s/[IBCLFSXZ]<< (.*?) >>/$1/gs;
         s/[IBCLFSXZ]<(.*?)>/$1/gs;
         $_
       } @$_;
}

sub htmlfcodes {
   my %tag = (
      I => "i",
      B => "b",
      C => "tt",
      L => "i",    # broken
      F => "tt",
      S => "nobr", # non-std
      X => "span", # broken
      Z => "span", # broken
   );
   # strip formatting codes, dumb version
   map {
         s/([IBCLFSXZ])<< (.*?) >>/<$tag{$1}>$2<\/$tag{$1}>/gs;
         s/([IBCLFSXZ])<(.*?)>/<$tag{$1}>$2<\/$tag{$1}>/gs;
         $_
       } @$_;
}

sub command {
   my ($self, $command, $paragraph) = @_;

   if ($command eq "begin" && $paragraph =~ /^\s*table\s*$/s) {
      $table++;
   } elsif ($command eq "end" && $paragraph =~ /^\s*table\s*$/s) {
      $table--;
   } else {
      shift;
      return $self->SUPER::command (@_);
   }
}

sub verbatim {
   my ($self, $para) = @_;
   shift;

   return $self->SUPER::verbatim (@_) unless $table;

   my $table = [ map [$_ =~ /\t([^\t]*)/g], split /\n/, $para ];
   my $cols = max map scalar @$_, @$table;

   my $fh = $self->output_handle;

   # format the table
   # text
   print $fh "=begin text\n\n";

   for (@$table) {
      print $fh "   ", (map +(sprintf "%-15s ", $_), stripfcodes @$_), "\n";
   }

   print $fh "\n=end text\n\n";


   # tbl
   print $fh "=begin roff\n\n";

   print $fh ".TS\n" . ("l " x $cols) . ".\n";
   print $fh map +(join "\t", stripfcodes @$_) . "\n", @$table;
   print $fh ".TE\n";

   print $fh "\n=end roff\n\n";

   # html
   # pod::xhtml fails on begin/end blocks
#   print $fh "=begin xhtml\n\n";

   print $fh "=for xhtml <table>";
   print $fh map "<tr><td>" . +(join "</td><td>", htmlfcodes @$_) . "</td></tr>", @$table;
   print $fh "</table>\n\n";

   print $fh "=for html <table>";
   print $fh map "<tr><td>" . +(join "</td><td>", htmlfcodes @$_) . "</td></tr>", @$table;
   print $fh "</table>\n\n";

#   print $fh "\n=end xhtml\n\n";

}

__PACKAGE__->new->parse_from_filehandle;