1package main;
2
3# Copyright (c) 2009  Openismus GmbH  <http://www.openismus.com/>
4#
5# This file is part of mm-common.
6#
7# mm-common is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published
9# by the Free Software Foundation, either version 2 of the License,
10# or (at your option) any later version.
11#
12# mm-common is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with mm-common.  If not, see <http://www.gnu.org/licenses/>.
19
20use strict;
21use warnings;
22use bytes;
23use File::Glob qw(:glob);
24use File::Spec;
25use Getopt::Long qw(:config no_getopt_compat no_ignore_case require_order bundling);
26
27sub path_basename ($)
28{
29  my ($path) = @_;
30  my $basename = File::Spec->splitpath($path);
31
32  return $basename;
33}
34
35sub exit_help ()
36{
37  my $script_name = path_basename($0) || 'doc-postprocess.pl';
38
39  print <<"EOF";
40Usage: perl $script_name [OPTION]... [PATTERN]...
41
42Post-process the Doxygen-generated HTML files matching PATTERN.
43
44Options:
45  -?, --help                        display this help and exit
46EOF
47  exit;
48}
49
50sub error (@)
51{
52  my $script_name = path_basename($0);
53  $script_name =~ s/\.[^.]*$//s if (defined $script_name);
54
55  print STDERR ($script_name || 'doc-postprocess', ': ', @_, "\n");
56  exit 1;
57}
58
59GetOptions('help|?' => \&exit_help)
60  or exit 2;
61
62foreach my $filename (map(bsd_glob($_, GLOB_NOSORT), @ARGV))
63{
64  my @buf;
65  my $file;
66
67  open($file, '<', $filename) and (@buf = <$file>) and close($file)
68    or error('Failed to read ', path_basename($filename), ': ', $!);
69
70  foreach (@buf)
71  {
72    if (/<a class="el"/)
73    {
74      # return value
75      s/ &amp;&nbsp; */&amp;&#160;/;
76      s/ \*&nbsp; */*&#160;/;
77
78      # parameters
79      s/ &amp;/&amp;/g;
80      s/&amp;\b/&amp; /g;
81      s/ \*/*/g;
82      s/\*\b/* /g;
83
84      # templates
85      s/\btemplate&lt;/template &lt;/;
86    }
87    elsif (/<td class="md(?:name)?"/)
88    {
89      # left parenthesis
90      s/\(&nbsp; */(/;
91
92      # return value
93      s/ &amp; /&amp; /g;
94      s/ \* /* /g;
95
96      # parameters
97      s/ &amp;&nbsp; */&amp;&#160;/g;
98      s/ \*&nbsp; */*&#160;/g;
99
100      # templates
101      s/\btemplate&lt;/template &lt;/;
102    }
103    else
104    {
105      # template decls
106      s/^(<h\d>|)template&lt;/$1template &lt;/;
107    }
108
109    # For some reason, recent versions of Doxygen output the full path to
110    # referenced tag files.  This is bad since it breaks doc-install.pl,
111    # and also because it leaks local path names into source tarballs.
112    # Thus, strip the directory prefix here.
113    s| doxygen="[^":]*/([^":]+\.tag):| doxygen="$1:|g;
114
115    s/&copy;/&#169;/g;
116    s/&mdash;/&#8212;/g;
117    s/&ndash;/&#8211;/g;
118    s/ *&nbsp; */&#160;/g;
119  }
120
121  # write the whole buffer back
122  open($file, '>', $filename) and print $file (@buf) and close($file)
123    or error('Failed to write ', path_basename($filename), ': ', $!);
124}
125
126exit;
127