1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-11-04 08:17:17 -05:00
elinks/doc/tools/code2doc

115 lines
2.4 KiB
Plaintext
Raw Normal View History

#!/usr/bin/perl -w
use strict;
use warnings;
use diagnostics;
use Getopt::Std;
my $HELP = "Usage: $0 [FILE]...
Parses [FILE], outputing the result to stdout.";
sub usage {
print "@_\n";
exit;
}
our($opt_h, $opt_v, $title, $body, $indent, $idpath, $inblock);
getopts("hv") or usage($HELP);
$opt_v and usage("Copyleft (c) 2006, Russ Rowan (See `COPYING')");
usage($HELP) if $opt_h or @ARGV < 1;
sub put_section {
if ($title) {
print "\n$title\n";
$_ = $title;
s/[^-]/-/g;
print "$_\n" if not $indent;
}
if ($body) {
$_ = $body;
s/#newline#/$indent/g;
print "$_\n";
}
$title = $body = undef;
}
$idpath = $title = $body = "";
while (<>)
{
2006-01-09 00:49:15 -05:00
my $end = s/\s*\*+\//\n/ ? 'yes' : undef;
if ($end and /[^=]*[\s*](\w+)[\s:,;].*\/\*:\s*(.*)([.]\s*)?$/) {
2006-01-09 02:25:03 -05:00
# Implicit id for enum values and struct members.
print "\nid:[$idpath$1]::\n\t$2.\n";
} elsif ($inblock) {
2006-01-09 02:25:03 -05:00
# Redo the indentation, preserve empty lines.
s/^(\s|\*)*//;
s/^$/\n/;
$body .= "#newline#" . $_;
2006-01-09 02:25:03 -05:00
2006-01-09 06:46:13 -05:00
} elsif (s/\s*\/\*\*+\s*(.*)/$1/) {
# Found magic header; flush, record title and set indentation.
put_section;
$title = "$1";
$indent = /::/ ? "\t" : "";
2006-01-09 02:25:03 -05:00
} else {
2006-01-09 06:46:13 -05:00
next if not ($title or $body) or /^\s$/;
2006-01-09 06:46:13 -05:00
my $orig_title = $title;
# Combine multi-line declarations to one. Break at empty
# for #define ...
while (not /(struct|enum|typedef|[^=])*[\s*](\w+).*[\[:,;{]/) {
my $line = $_;
$_ .= <>;
last if $_ eq $line;
2006-01-09 06:46:13 -05:00
}
if (/struct\s+(\w+)\s*{/) {
$title = "struct:$1" . "[$title]";
$idpath = "$1.";
2006-01-09 06:46:13 -05:00
} elsif (/enum\s+(\w+)\s*{/) {
$title = "enum:$1" . "[$title]";
$idpath = "";
} elsif (/#define/) {
if (/#define\s+(\w+)[(]/) {
$title = "func:$1" . "[$title]";
} elsif (/#define\s+(\w+)/) {
$title = "macro:$1" . "[$title]";
}
$idpath = "";
} elsif (/typedef/) {
if (/.*\(\*(\w+)\)\(/) {
$title = "typedef:$1" . "[$title]";
} elsif (/typedef.*\s(\w+);/) {
$title = "typedef:$1" . "[$title]";
}
$idpath = "";
} elsif (/.*[\s*](\w+)\(/) {
$title = "func:$1" . "[$title]";
2006-01-09 06:46:13 -05:00
$idpath = "";
} elsif (/.*\(\*(\w+)\)\(/) {
$body = "#newline#" . $title if not $body;
$title = "id:[$idpath$1]::";
$indent = "\t";
} elsif (/[^=]*[\s*](\w+)[\[\s,:;]/) {
$body = "#newline#" . $title if not $body;
$title = "id:[$idpath$1]::";
$indent = "\t";
}
2006-01-09 06:46:13 -05:00
put_section if $orig_title ne $title;
2006-01-09 02:25:03 -05:00
next;
}
2006-01-09 02:25:03 -05:00
$inblock = $end ? undef : 'yes';
}