summaryrefslogtreecommitdiff
path: root/cesar/common/doc/dox2rst
blob: d85218f2c9ebbe852a5d065a29b640f867378f22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/perl
use strict;
use warnings;

my $blank = 0;

use Getopt::Long qw(:config no_ignore_case bundling);
use Pod::Usage;

my $help;
my $scale;
my $width;
my @include;
push @include, '.';
GetOptions (
    'help|h' => \$help,
    'scale|s=s' => \$scale,
    'width|w=s' => \$width,
    'I=s' => \@include,
) or pod2usage (2);
pod2usage (1) if $help;

my @dontinclude;

sub find_include
{
    my $file = shift;
    for (@include)
    {
	-f "$_/$file" and return "$_/$file";
    }
    die "$file not found.\n";
}

while (<>)
{
    chomp;
    s/\\c ([^ ]*)/``$1``/g;
    if (/^\\image html (.*?) "(.*)"$/)
    {
	my ($img, $legend) = ($1, $2);
	my $s = '';
	$s .= defined $scale ? "   :scale: $scale\n" : '';
	$s .= defined $width ? "   :width: $width\n" : '';
	$_ = <<EOF;
.. figure:: $1
$s
   $2
EOF
    }
    elsif (/^\\code$/)
    {
	$_ = "::\n";
    }
    elsif (/^\\endcode$/)
    {
	$_ = '';
    }
    elsif (/^\\include (.*)$/)
    {
	my $file = find_include ($1);
	open INC, "<$file" or die;
	my @file = <INC>;
	close INC;
	$_ = "::\n\n  " . join ("  ", @file);
    }
    elsif (/^\\dontinclude (.*)$/)
    {
	my $file = find_include ($1);
	open INC, "<$file" or die;
	@dontinclude = <INC>;
	close INC;
	$_ = '';
    }
    elsif (/^\\skip (.*)$/)
    {
	my $pat = $1;
	@dontinclude or die;
	shift @dontinclude until $dontinclude[0] =~ /$pat/;
	$_ = '';
    }
    elsif (/^\\until (.*)$/)
    {
	my $pat = $1;
	@dontinclude or die;
	$_ = "::\n\n";
	until ($dontinclude[0] =~ /$pat/)
	{
	    $_ = $_ . '  ' . $dontinclude[0];
	    shift @dontinclude;
	}
	$_ = $_ . '  ' . $dontinclude[0];
	shift @dontinclude;
    }
    elsif (s/^\s(\s*- )\(/$1\\(/
	    or s/^\s(\s*)- \\b\s*/$1/
	    or s/^\s(\s*- )/$1/
	    or s/\\param  (.*?)  (.*)/:$1: $2/
	    or s/\\return  (.*)/:Returns: $1/)
    {
	print "\n" unless $blank && !/^\s*-/;
	$blank = 1;
    }
    elsif (s/^\s(\s*)/$1/)
    {
    }
    elsif (s/^> {3}/    /)
    {
	print "::\n\n" unless $blank;
	$blank = 1;
    }
    else
    {
	$blank = 0;
    }
} continue {
    print $_, "\n";
}

__END__

=head1 NAME

dox2rst - Convert a subset of the Doxygen syntax to reStructuredText

=head1 SYNOPSIS

dox2rst [options] hop

 Options:
   -h, --help		brief help message
   -s, --scale=SCALE	include a scale attribute in images
   -w, --width=WIDTH	include a width attribute in images

=cut