Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simpleminded Java + Javadoc to HTML Converter
#!/usr/bin/perl -w
use strict;
use XML::Writer;
my $state = 0;
my $h = new XML::Writer();
$h->startTag("html");
$h->startTag("head");
$h->endTag("head");
$h->startTag("body");
$h->startTag("pre");
my $n = 0;
while ( <> ) {
$n += 1;
if ( $state == 0 ) {
if ( m~^\s*/\*\*\s*~ ) {
# new paragraph comment line
$h->endTag("pre");
$h->startTag("p");
$state = 1;
}
else {
# more code line
$h->characters(sprintf("%3d %s", $n, $_));
}
}
elsif ( $state == 1 ) {
if ( m~^\s*\*\s*$~ ) {
# new paragraph comment line (blank line)
$h->endTag("p");
$h->startTag("p");
}
elsif ( m~^\s\*\s*\{\@code\s+(.*)$~ ) {
# start code directive comment line
$h->endTag("p");
$h->startTag("pre");
$h->characters(sprintf("%3d %s", $n, $1));
$state = 2;
}
elsif ( m~^\s*\*\s+(.*)$~ ) {
# more text comment line
$h->characters("$1 ");
}
elsif ( m~^\s*\*/\s*~ ) {
# end of paragraph comment line
$h->endTag("p");
$h->startTag("pre");
$state = 0;
}
else {
die "unknown input $state $_";
}
}
elsif ( $state == 2 ) {
if ( m~^\s*\*\s*}\s*$~ ) {
# end code directive comment line
$h->endTag("pre");
$h->startTag("p");
$state = 1;
}
elsif ( m~^\s*\*\s+(.*)$~ ) {
# more code directive comment line
$h->characters(sprintf("%3d %s", $n, $1));
}
else {
die "unknown input $state $_";
}
}
else {
die "unknown state $state";
}
}
$h->endTag("pre") if $state == 0;
$h->endTag("p") if $state == 1;
$h->endTag("body");
$h->endTag("html");
$h->end();
# END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.