Debian Codegraph @

xmlmantohtml

raw
1 #!/usr/bin/perl
2
3 # xmlmanhtml - simple xml to man converter
4 # Copyright (C) 2000-2002 Oliver Kurth <[email protected]>
5 # 2017-2019 Adam Bilbrough
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12 # This program 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 this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
21 use XML::Parser;
22 use strict;
23 use warnings;
24
25 my $section = "";
26
27 my %terminators = (
28 manpage => "</td></tr></table></center>\n",
29 synopsis => "</b>\n",
30 description => "\n",
31 options => "",
32 seealso => "\n",
33 section => "\n",
34 arg => "</em>",
35 file => "</em>",
36 opt => "</b>",
37 cmd => "<br>\n",
38 p => "</p>"
39 );
40
41 sub flush_buf{
42 local $_;
43
44 print my $buffer = "";
45 }
46
47 sub handle_start{
48 local $_;
49
50 my $expat = shift;
51 my $element = shift;
52 my %attr = @_;
53
54 flush_buf();
55
56 $_ = $element;
57
58 /^manpage$/ and do{
59 print "<center><table width=\"80%\">\n<tr><td>";
60 print "<h1>$attr{name}</h1>\n";
61 print "<h2>" . $attr{desc} . "</h2>\n";
62 };
63 /^synopsis$/ and do{
64 print "<h2>Synopsis</h2>\n<b>";
65 $section = $element;
66 };
67 /^description$/ and do{
68 print "<h2>Description</h2>\n";
69 $section = $element;
70 };
71 /^options$/ and do{
72 print "<h2>Options</h2>\n";
73 $section = $element;
74 };
75 /^seealso$/ and do{
76 print "<h2>see also</h2>\n";
77 $section = $element;
78 };
79 /^section$/ and do{
80 print "<h2>".$attr{name}."</h2>\n";
81 $section = $attr{name};
82 };
83 /^arg$/ and do{
84 print "<em>";
85 };
86 /^file$/ and do{
87 print "<em>";
88 };
89 /^opt$/ and do{
90 print "<b>";
91 my $opt = 1;
92 };
93 /^manref$/ and do{
94 if(!$attr{href}){
95 print "<b>" . $attr{name} ." (" . $attr{section} . ")</b>";
96 }else{
97 print "<a href=\"$attr{href}\">" . $attr{name}. "</a>";
98 }
99 };
100 /^p$/ and do{
101 print "<p>";
102 };
103 /^url$/ and do{
104 print "<a href = \"$attr{href}\">".$attr{href}."</a>";
105 };
106 }
107
108 sub handle_end{
109 local $_;
110
111 my $expat = shift;
112 my $element = shift;
113
114 flush_buf();
115
116 my %attr = @_;
117
118 $section = "" if($section eq $element);
119
120 print $terminators{$element} if($terminators{$element});
121 }
122
123 sub handle_char{
124 local $_;
125 my $expat = shift;
126 my $string = shift;
127
128 $string =~ s/&/&amp;/gs;
129 $string =~ s/</&lt;/gs;
130 $string =~ s/>/&gt;/gs;
131 $string =~ s/"/&quot;/gs;
132
133 my $buffer .= $string;
134 }
135
136 MAIN:{
137 my $file = shift;
138
139 if (!$file) {
140 print STDERR "You need to specify a file to parse\n";
141 exit(1);
142 }
143
144 my $parser = new XML::Parser(Handlers => {Start => \&handle_start,
145 End => \&handle_end,
146 Char => \&handle_char});
147
148 print '<body text="#000000" link="#0000ff" bgcolor="#ffffff">';
149 $parser->parsefile($file);
150 print "</body>\n";
151 }