summaryrefslogtreecommitdiff
path: root/manual/tools/htmltbl
blob: 4b7b41a7db0431d133aa4daaf87a7bf134bbb777 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/local/bin/perl

while (<>) {
  if (m|^<tbl[> ]|) {
    while (! m|</tbl>$|) { $_ .= <>; }
    s/\n//g;
    print "<pre>\n";
    do format_table($_);
    print "</pre>\n";
  } else {
    print $_;
  }
}

sub format_table {
# On input, $_ contains:
# <tbl [border]><th>Header 1<th>Header2<th>...<th>Header M<tr>
# <td>Data11<td>Data12<td>...<td>Data1M<tr>
# ...
# <td>DataN1<td>DataN2<td>...<td>DataNM<tr>
# </tbl>

# Extract the entries and compute the number of lines and columns

  $numlines = 0;
  $numcols = 0;
  $border = 0;
  $header = 0;
  $x = 0;
  $y = 0;
  foreach $_ (split(/(<tbl[ a-zA-Z]*>|<th>|<td>|<tr>|<\/tbl>)/, $_)) {
    if (/^$/) { next; }
    elsif (/<tbl border>/) { $border = 1; }
    elsif (/<tr>/i) {
      if ($x > $numcols) { $numcols = $x; }
      $x = 0;
      $y++;
    }
    elsif (/<th>/) { $header = 1; }
    elsif (!/(<tbl[ a-zA-Z]*>|<th>|<td>|<tr>|<\/tbl>)/) {
      s|</?[a-zA-Z]*>||g;       # Remove embedded tags
      s/^\s*//;                 # and initial blanks
      s/\s*$//;                 # and final blanks
      s/\s\s\s*/ /g;            # and extra blanks
      s/&lt;/</g;               # Unescape HTML specials
      s/&gt;/>/g;
      s/&amp;/&/g;
      $entry{$x, $y} = $_;
      $x++;
    }
  }
  $numlines = $y;

# Compute the max width of each column

  $totalwidth = 0;

  for ($x = 0; $x < $numcols; $x++) {
    $max = 0;
    for ($y = 0; $y < $numlines; $y++) {
      $len = length($entry{$x, $y});
      if ($len > $max) { $max = $len; }
    }
    $width[$x] = $max;
    $totalwidth += $max;
  }

# If it does not fit in one line, turn wide fields into multi-line fields

  if ($totalwidth >= 65) {
    $totalwidth = 0;
    $maxwidth = 65 / $numcols;
    for ($x = 0; $x < $numcols; $x++) {
      if ($width[$x] > $maxwidth) {
        if ($x < $numcols - 1) {
          $width[$x] = $maxwidth;
        } else {
          $width[$x] = 70 - $totalwidth;
        }
      }
      $totalwidth += $width[$x];
    }
  }

# Compute the separators

  if ($border) {
    $horsep = '+-';
    for ($x = 0; $x < $numcols; $x++) {
      if ($x > 0) { $horsep .= '-+-'; }
      $horsep .= '-' x $width[$x];
    }
    $horsep .= '-+';
    $verleft = '| ';
    $versep = ' | ';
    $verright = ' |';
  } else {
    $horsep = '';
    $verleft = '  ';
    $versep = '   ';
    $verright = '  ';
  }

# Print the table
  print $horsep, "\n";
  for ($y = 0; $y < $numlines; $y++) {
    do {
      $overflow = 0;
      print $verleft;
      for ($x = 0; $x < $numcols; $x++) {
        if ($x > 0) { print $versep; }
        $_ = $entry{$x, $y};
        if (length($_) > $width[$x]) {
          $pos = rindex($_, ' ', $width[$x]);
          if ($pos < 0) { $pos = $width[$x]; } else { $pos++; }
          $entry{$x, $y} = substr($_, $pos);
          $_ = substr($_, 0, $pos - 1);
          $overflow = 1;
        } else {
          $entry{$x, $y} = '';
        }
        $len = length($_);
        s/&/&amp;/g;
        s/</&lt;/g;
        s/>/&gt;/g;
        print $_, ' ' x ($width[$x] - $len);
      }
      print $verright, "\n";
    } while($overflow);
    if ($header && $y == 0) { print $horsep, "\n"; }
  }
  print $horsep, "\n";
}