docgen.pl
changeset 0 84fdfb0344c9
child 4 5770be2d5f3f
equal deleted inserted replaced
-1:000000000000 0:84fdfb0344c9
       
     1 #! /usr/bin/perl
       
     2 
       
     3 use strict;
       
     4 use warnings;
       
     5 
       
     6 my %docs;
       
     7 my @tags;
       
     8 my $inside;
       
     9 
       
    10 foreach my $file (@ARGV) {
       
    11 	if ( not open SOURCE, '<', $file ) {
       
    12 		print STDERR "Cannot open $file\n";
       
    13 		next;
       
    14 	}
       
    15 
       
    16 	my $chunk = 0;
       
    17 
       
    18 	while (<SOURCE>) {
       
    19 		if ( $inside ) {
       
    20 			if ( not /^\/\/\// ) {
       
    21 				$inside = 0;
       
    22 				$chunk++;
       
    23 			} else {
       
    24 				push @{$docs{$file}[$chunk]}, substr ( $_, 4 );
       
    25 			}
       
    26 		} else {
       
    27 			next if not /^\/\/\//;
       
    28 
       
    29 			$inside = 1;
       
    30 			my $tag = substr $_, 4;
       
    31 			chomp $tag;
       
    32 			push @{$docs{$file}[$chunk]}, $tag;
       
    33 			# hack to allow twoword objects be written in text with spaces
       
    34 			# now it matches "lm message" instead of "lm message node" -.-
       
    35 			# and even if tag list will be reverse sorted by length,
       
    36 			# it will produce nested links...
       
    37 			# well, that all is now solved, but in not too impressive way..
       
    38 			$tag =~ s/_/./g;
       
    39 			push @tags, $tag;
       
    40 		}
       
    41 	}
       
    42 	
       
    43 	close SOURCE;
       
    44 }
       
    45 
       
    46 print <<HEADER
       
    47 <html>
       
    48 <head><title>lua-loudmouth docs</title></head>
       
    49 <body>
       
    50 HEADER
       
    51 ;
       
    52 
       
    53 @tags = reverse sort { length $a <=> length $b } @tags;
       
    54 # TODO preserve original order
       
    55 foreach my $file ( sort keys %docs ) {
       
    56 	print "<hr>";
       
    57 	foreach my $chunk ( @{$docs{$file}} ) {
       
    58 		my $head = shift @$chunk;
       
    59 		my $tag  = $head;
       
    60 		$tag =~ s/_/./g;
       
    61 		print "<a name='$tag'></a><h2>$head</h2><p>";
       
    62 		foreach ( @$chunk ) {
       
    63 			s/^A: /<br\/>Arguments: /;
       
    64 			s/^R: /<br\/>Return values: /;
       
    65 			s/^V: /<br\/>Values: /;
       
    66 			foreach my $tag ( @tags ) {
       
    67 				# TODO quotemeta required, but for now
       
    68 				# this bug is rather desired...
       
    69 				#s/\b$tag\b/<a href="#$tag">$&<\/a>/g;
       
    70 				s/(.)\b($tag)\b/ if ( $1 eq '#' or $1 eq '>' ) { "$1$2" } else { "$1<a href='#$tag'>$2<\/a>" } /ge;
       
    71 			}
       
    72 			print $_;
       
    73 		}
       
    74 		print "</p>"
       
    75 	}
       
    76 	print "<hr>";
       
    77 }
       
    78 
       
    79 print "</body></html>"
       
    80 
       
    81 # The end