Humdrum2wikitable

From CCARH Wiki
Jump to navigation Jump to search
#!/usr/bin/perl
# Description: Convert a humdrum file into a wiki table
#vim: ts=3

use strict;

my @contents = <>;
chomp @contents;

my $counter = 0;
for (my $i=0; $i<@contents; $i++) {
	my $line = $contents[$i];
	next if $line =~ /^\*/;
	if ($counter == 0) {
		printHeader($line);
		$counter++;
	} elsif ($line =~ /^!/) {
		next;
	} else {
		printEntry($line);
	}
}
printFooter();

###########################################################################


##############################
##
## printEntry --
##

sub printEntry {
	my ($line) = @_;
	my @fields = split(/\t+/, $line);
	print "\n|- valign=\"top\"\n";
	for (my $i=0; $i<@fields; $i++) {
		my $item = $fields[$i];
		print "|";
		print "|" if $i > 0;
		$item = "" if $item =~ /^\s*\.\s*$/;
		print " $item\n";
	}
}



##############################
##
## printHeader --
##

sub printHeader {
	my ($line) = @_;
	my @fields = split(/\t+/, $line);
	my $count = @fields;
	print "{| cellpadding=\"1\" class=\"wikitable\"\n\n";
	print "|- {{Style|table header}}\n";
	foreach my $item (@fields) {
		$item =~ s/^!//;
		print "! scope=\"col\" align=\"left\"\t|\t$item\n";
	}
	print "\n";
}



##############################
##
## printFooter --
##

sub printFooter {
	print "\n|}\n\n\n\n";
}