Skip to content

Instantly share code, notes, and snippets.

@andrewgilmartin
Last active October 8, 2019 16:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save andrewgilmartin/e27f6a189dee6ceacbb7 to your computer and use it in GitHub Desktop.
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