perl 将XML格式转换为html、txt、pod格式
xmlfile:# cat example
Test README File
This is a summary of the file.
It should appear in PRE tags
This is the full description of the file
Subsection> Subsection text
Another Subsection> More Subsection text
List item 1
List item 2
Dave Cross
dave@mag-sol.com
Something
Something else
#
perl转换脚本:
#!/usr/bin/perl -w
use strict;
use XML::Parser;
use Getopt::Std;
use Text::Wrap;
my %formats = (h => {name => 'html'},
p => {name => 'pod'},
t => {name => 'text'});
my %opts;
(getopts('f:', \%opts) && @ARGV) || die "usage: format_xml.pl -f h|p|t xml_file\n";
die "Invalid format: $opts{f}\n" unless exists $formats{$opts{f}};
warn "Formatting file as $formats{$opts{f}}->{name}\n";
my $p = XML::Parser->new(Style => 'Tree');
my $tree = $p->parsefile(shift);
my $level = 0;
my $ind = '';
my $head = 1;
top($tree);
process_node(@$tree);
bot();
sub process_node {
my ($type, $content) = @_;
$ind = ' ' x $level;
if ($type) {
local $_ = $type;
my $attrs = shift @$content;
/^NAME$/ && name($content);
/^SYNOPSIS$/ && synopsis($content);
/^DESCRIPTION$/ && description();
/^TEXT$/ && text($content);
/^CODE$/ && code($content);
/^HEAD$/ && head($content);
/^LIST$/ && do {list($attrs, $content); @$content = ()};
/^AUTHOR$/ && author();
/^ANAME$/ && aname($content);
/^EMAIL$/ && email($content);
/^SEE_ALSO$/ && see_also($content);
while (my @node = splice @$content, 0, 2) {
++$level;
++$head if $type eq 'SUBSECTION';
process_node(@node);
--$head if $type eq 'SUBSECTION';
--$level;
}
}
}
sub top {
$tree = shift;
if ($opts{f} eq 'h') {
print "\n";
print "\n";
print "$tree->->->\n";
print "\n\n";
} elsif ($opts{f} eq 'p') {
print "=pod\n\n";
} elsif ($opts{f} eq 't') {
print "\n", $tree->->->, "\n";
print '-' x length($tree->->->), "\n\n";
}
}
sub bot {
if ($opts{f} eq 'h') {
print "\n\n";
} elsif ($opts{f} eq 'p') {
print "=cut\n\n";
} elsif ($opts{f} eq 't') {
# do nothing
}
}
sub name {
my $content = shift;
if ($opts{f} eq 'h') {
print "NAME\n";
print "$content->\n"
} elsif ($opts{f} eq 'p') {
print "=head1 NAME\n\n";
print "$content->\n\n";
} elsif ($opts{f} eq 't') {
print "NAME\n\n";
print $ind, "$content->\n\n";
}
}
sub synopsis {
my $content = shift;
if ($opts{f} eq 'h') {
print "SYNOPSIS\n";
print "$content->\n"
} elsif ($opts{f} eq 'p') {
print "=head1 SYNOPSIS\n\n";
print "$content->\n";
} elsif ($opts{f} eq 't') {
print "SYNOPSIS\n";
print "$content->\n";
}
}
sub description {
if ($opts{f} eq 'h') {
print "DESCRIPTION\n";
} elsif ($opts{f} eq 'p') {
print "=head1 DESCRIPTION\n\n";
} elsif ($opts{f} eq 't') {
print "DESCRIPTION\n\n";
}
}
sub text {
my $content = shift;
if ($opts{f} eq 'h') {
print "$content->\n"
} elsif ($opts{f} eq 'p') {
print wrap('', '', trim($content->)), "\n\n";
} elsif ($opts{f} eq 't') {
print wrap($ind, $ind, trim($content->)), "\n\n";
}
}
sub code {
my $content = shift;
if ($opts{f} eq 'h') {
print "$content->\n"
} elsif ($opts{f} eq 'p') {
print "$content->\n";
} elsif ($opts{f} eq 't') {
print "$content->\n";
}
}
sub head {
my $content = shift;
if ($opts{f} eq 'h') {
print "", trim($content->), "\n"
} elsif ($opts{f} eq 'p') {
print "=head$head ", trim($content->), "\n\n";
} elsif ($opts{f} eq 't') {
print trim($content->), "\n\n";
}
}
sub list {
my ($attrs, $content) = @_;
my %list = (bullet => 'ul', numbered => 'ol');
my $type = $attrs->{TYPE};
if ($opts{f} eq 'h') {
print "\n";
while (my @node = splice @$content, 0, 2) {
if ($node eq 'ITEM') {
print "$node->\n";
}
}
print "\n";
} elsif ($opts{f} eq 'p') {
print "=over 4\n";
while (my @node = splice @$content, 0, 2) {
my $cnt = 1;
if ($node eq 'ITEM') {
print "=item *\n$node->\n\n";
}
}
print "=back\n\n";
} elsif ($opts{f} eq 't') {
while (my @node = splice @$content, 0, 2) {
my $cnt = 1;
if ($node eq 'ITEM') {
print $ind, "* $node->\n";
}
}
print "\n";
}
}
sub author {
if ($opts{f} eq 'h') {
print "AUTHOR\n";
} elsif ($opts{f} eq 'p') {
print "=head1 AUTHOR\n\n";
} elsif ($opts{f} eq 't') {
print "AUTHOR\n\n";
}
}
sub aname {
my $content = shift;
if ($opts{f} eq 'h') {
print "$content->\n"
} elsif ($opts{f} eq 'p') {
print trim($content->), ' ';
} elsif ($opts{f} eq 't') {
print $ind, trim($content->), ' ';
}
}
sub email {
my $content = shift;
if ($opts{f} eq 'h') {
print '<', trim($content->), ">\n"
} elsif ($opts{f} eq 'p') {
print '
页:
[1]