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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     1
#! /usr/bin/perl
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     2
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     3
use strict;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     4
use warnings;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     5
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     6
my %docs;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     7
my @tags;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
     8
my $inside;
4
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
     9
my $harvest;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    10
my @values;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    11
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    12
foreach my $file (@ARGV) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    13
	if ( not open SOURCE, '<', $file ) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    14
		print STDERR "Cannot open $file\n";
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    15
		next;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    16
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    17
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    18
	my $chunk = 0;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    19
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    20
	while (<SOURCE>) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    21
		if ( $inside ) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    22
			if ( not /^\/\/\// ) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    23
				$inside = 0;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    24
				$chunk++;
4
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    25
			} elsif ( /^\/\/\/ G:/ ) {
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    26
				$inside = 0;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    27
				$harvest = 'V: ' . substr ( $_, 6 );
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    28
			} else {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    29
				push @{$docs{$file}[$chunk]}, substr ( $_, 4 );
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    30
			}
4
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    31
		} elsif ( $harvest ) {
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    32
			if ( /\{\s*NULL.*\}/ ) {
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    33
				push @{$docs{$file}[$chunk]}, $harvest . ' ' . join ( ', ', @values );
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    34
				$harvest = undef;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    35
				@values  = ();
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    36
				$chunk++;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    37
			} elsif ( /\{\s*"(.+)".*\}/ ) {
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    38
				push @values, $1;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    39
			}
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    40
		} else {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    41
			next if not /^\/\/\//;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    42
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    43
			$inside = 1;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    44
			my $tag = substr $_, 4;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    45
			chomp $tag;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    46
			push @{$docs{$file}[$chunk]}, $tag;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    47
			# hack to allow twoword objects be written in text with spaces
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    48
			# now it matches "lm message" instead of "lm message node" -.-
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    49
			# and even if tag list will be reverse sorted by length,
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    50
			# it will produce nested links...
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    51
			# well, that all is now solved, but in not too impressive way..
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    52
			$tag =~ s/_/./g;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    53
			push @tags, $tag;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    54
		}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    55
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    56
	
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    57
	close SOURCE;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    58
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    59
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    60
print <<HEADER
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    61
<html>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    62
<head><title>lua-loudmouth docs</title></head>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    63
<body>
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    64
HEADER
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    65
;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    66
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    67
@tags = reverse sort { length $a <=> length $b } @tags;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    68
# TODO preserve original order
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    69
foreach my $file ( sort keys %docs ) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    70
	print "<hr>";
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    71
	foreach my $chunk ( @{$docs{$file}} ) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    72
		my $head = shift @$chunk;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    73
		my $tag  = $head;
4
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    74
		my $list = undef;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    75
		$tag =~ s/_/./g;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    76
		print "<a name='$tag'></a><h2>$head</h2><p>";
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    77
		foreach ( @$chunk ) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    78
			s/^A: /<br\/>Arguments: /;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    79
			s/^R: /<br\/>Return values: /;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    80
			s/^V: /<br\/>Values: /;
4
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    81
			if ( $list ) {
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    82
				if ( /^\* / ) {
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    83
					s/^\* /<\/li><li>/;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    84
				} else {
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    85
					s/^/<\/li><\/ul> /;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    86
					$list = undef;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    87
				}
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    88
			} elsif ( /^\* / ) {
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    89
				s/^\* /<ul><li>/;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    90
				$list = 1;
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
    91
			}
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    92
			foreach my $tag ( @tags ) {
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    93
				# TODO quotemeta required, but for now
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    94
				# this bug is rather desired...
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    95
				#s/\b$tag\b/<a href="#$tag">$&<\/a>/g;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    96
				s/(.)\b($tag)\b/ if ( $1 eq '#' or $1 eq '>' ) { "$1$2" } else { "$1<a href='#$tag'>$2<\/a>" } /ge;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    97
			}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    98
			print $_;
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
    99
		}
4
5770be2d5f3f Documentation updates
Myhailo Danylenko <isbear@ukrpost.net>
parents: 0
diff changeset
   100
		print "</li></ul>" if $list;
0
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   101
		print "</p>"
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   102
	}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   103
	print "<hr>";
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   104
}
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   105
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   106
print "</body></html>"
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   107
84fdfb0344c9 Initial commit
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff changeset
   108
# The end