File: xmlmantohtml

package info (click to toggle)
xmltoman 0.6-1
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 164 kB
  • sloc: xml: 331; perl: 278; makefile: 34
file content (151 lines) | stat: -rw-r--r-- 3,262 bytes parent folder | download
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/perl

#    xmlmanhtml - simple xml to man converter
#    Copyright (C) 2000-2002  Oliver Kurth <oku@masqmail.cx>
#    		   2017-2019  Adam Bilbrough
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use XML::Parser;
use strict;
use warnings;

my $section = "";

my %terminators = (
    manpage => "</td></tr></table></center>\n",
		synopsis => "</b>\n",
		description => "\n",
		options => "",
		seealso => "\n",
		section => "\n",
		arg => "</em>",
		file => "</em>",
		opt => "</b>",
		cmd => "<br>\n",
		p => "</p>"
	);

sub flush_buf{
  local $_;

  print my $buffer = "";
}

sub handle_start{
  local $_;

  my $expat = shift;
  my $element = shift;
  my %attr = @_;

  flush_buf();

  $_ = $element;

  /^manpage$/ and do{
    print "<center><table width=\"80%\">\n<tr><td>";
    print "<h1>$attr{name}</h1>\n";
    print "<h2>" . $attr{desc} . "</h2>\n";
  };
  /^synopsis$/ and do{
    print "<h2>Synopsis</h2>\n<b>";
    $section = $element;
  };
  /^description$/ and do{
    print "<h2>Description</h2>\n";
    $section = $element;
  };
  /^options$/ and do{
    print "<h2>Options</h2>\n";
    $section = $element;
  };
  /^seealso$/ and do{
    print "<h2>see also</h2>\n";
    $section = $element;
  };
  /^section$/ and do{
    print "<h2>".$attr{name}."</h2>\n";
    $section = $attr{name};
  };
  /^arg$/ and do{
    print "<em>";
  };
  /^file$/ and do{
    print "<em>";
  };
  /^opt$/ and do{
    print "<b>";
    my $opt = 1;
  };
  /^manref$/ and do{
    if(!$attr{href}){
      print "<b>" . $attr{name} ." (" . $attr{section} . ")</b>";
    }else{
      print "<a href=\"$attr{href}\">" . $attr{name}. "</a>";
    }
  };
  /^p$/ and do{
    print "<p>";
  };
  /^url$/ and do{
    print "<a href = \"$attr{href}\">".$attr{href}."</a>";
  };
}

sub handle_end{
  local $_;

  my $expat = shift;
  my $element = shift;

  flush_buf();

  my %attr = @_;

  $section = "" if($section eq $element);

  print $terminators{$element} if($terminators{$element});
}

sub handle_char{
  local $_;
  my $expat = shift;
  my $string = shift;

  $string =~ s/&/&amp;/gs;
  $string =~ s/</&lt;/gs;
  $string =~ s/>/&gt;/gs;
  $string =~ s/"/&quot;/gs;

  my $buffer .= $string;
}

MAIN:{
  my $file = shift;

  if (!$file) {
    print STDERR "You need to specify a file to parse\n";
    exit(1);
  }

  my $parser = new XML::Parser(Handlers => {Start => \&handle_start,
					    End   => \&handle_end,
					    Char  => \&handle_char});

  print '<body text="#000000" link="#0000ff" bgcolor="#ffffff">';
  $parser->parsefile($file);
  print "</body>\n";
}