docgen.pl
author Myhailo Danylenko <isbear@ukrpost.net>
Sat, 21 Feb 2009 12:43:51 +0200
changeset 7 5db1448eb857
parent 4 5770be2d5f3f
child 17 ab4470465a0c
permissions -rwxr-xr-x
Message have node methods

#! /usr/bin/perl

use strict;
use warnings;

my %docs;
my @tags;
my $inside;
my $harvest;
my @values;

foreach my $file (@ARGV) {
	if ( not open SOURCE, '<', $file ) {
		print STDERR "Cannot open $file\n";
		next;
	}

	my $chunk = 0;

	while (<SOURCE>) {
		if ( $inside ) {
			if ( not /^\/\/\// ) {
				$inside = 0;
				$chunk++;
			} elsif ( /^\/\/\/ G:/ ) {
				$inside = 0;
				$harvest = 'V: ' . substr ( $_, 6 );
			} else {
				push @{$docs{$file}[$chunk]}, substr ( $_, 4 );
			}
		} elsif ( $harvest ) {
			if ( /\{\s*NULL.*\}/ ) {
				push @{$docs{$file}[$chunk]}, $harvest . ' ' . join ( ', ', @values );
				$harvest = undef;
				@values  = ();
				$chunk++;
			} elsif ( /\{\s*"(.+)".*\}/ ) {
				push @values, $1;
			}
		} else {
			next if not /^\/\/\//;

			$inside = 1;
			my $tag = substr $_, 4;
			chomp $tag;
			push @{$docs{$file}[$chunk]}, $tag;
			# hack to allow twoword objects be written in text with spaces
			# now it matches "lm message" instead of "lm message node" -.-
			# and even if tag list will be reverse sorted by length,
			# it will produce nested links...
			# well, that all is now solved, but in not too impressive way..
			$tag =~ s/_/./g;
			push @tags, $tag;
		}
	}
	
	close SOURCE;
}

print <<HEADER
<html>
<head><title>lua-loudmouth docs</title></head>
<body>
HEADER
;

@tags = reverse sort { length $a <=> length $b } @tags;
# TODO preserve original order
foreach my $file ( sort keys %docs ) {
	print "<hr>";
	foreach my $chunk ( @{$docs{$file}} ) {
		my $head = shift @$chunk;
		my $tag  = $head;
		my $list = undef;
		$tag =~ s/_/./g;
		print "<a name='$tag'></a><h2>$head</h2><p>";
		foreach ( @$chunk ) {
			s/^A: /<br\/>Arguments: /;
			s/^R: /<br\/>Return values: /;
			s/^V: /<br\/>Values: /;
			if ( $list ) {
				if ( /^\* / ) {
					s/^\* /<\/li><li>/;
				} else {
					s/^/<\/li><\/ul> /;
					$list = undef;
				}
			} elsif ( /^\* / ) {
				s/^\* /<ul><li>/;
				$list = 1;
			}
			foreach my $tag ( @tags ) {
				# TODO quotemeta required, but for now
				# this bug is rather desired...
				#s/\b$tag\b/<a href="#$tag">$&<\/a>/g;
				s/(.)\b($tag)\b/ if ( $1 eq '#' or $1 eq '>' ) { "$1$2" } else { "$1<a href='#$tag'>$2<\/a>" } /ge;
			}
			print $_;
		}
		print "</li></ul>" if $list;
		print "</p>"
	}
	print "<hr>";
}

print "</body></html>"

# The end