summaryrefslogtreecommitdiff
path: root/txt2latex.pl
blob: 5e04ef158c7a0112262390b88d3a9265310058b4 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/perl

use strict;

my @depth2latex = (
	'\chapter',
	'\section',
	'\subsection',
	'\subsubsection',
	'\paragraph',
	'\subparagraph'
);

my $skip_depth = 1;

sub find_footnotes {
	my @text = @_;
	my @notes = ();
	my $found = 0;
	my $l;

	for ($l = 0; $l <= $#text; $l++) {
		if ($text[$l] =~ m/^FOOTNOTES:$/) {
			$found = 1;
		}
		next unless $found;
		if ($text[$l] =~ m/^\[[0-9]+\]\s/) {
			push @notes, $l;
		}
	}
	return @notes;
};

sub find_sections {
	my @text = @_;
	my @sections = ();
	my $l;

	for ($l = 0; $l <= $#text - 1; $l++) {
		next unless (($text[$l + 1] =~ m/^=======*$/) or
			     ($text[$l + 1] =~ m/^-------*$/));

		next unless ($text[$l] =~ m/^(([0-9]+\.)+) /);

		push @sections, $l;

	}
	return @sections;
};

my @text = ();
while (<>) {
	push @text, $_;
}

my @footnotes = find_footnotes(@text);
my @sections = find_sections(@text);

#Format footnotes
my %footnote_by_number = ();
my $f;
for ($f = 0; $f <= $#footnotes; $f++) {
	my $l = $footnotes[$f];
	die unless ($text[$l] =~ m/^\[([0-9]+)\]\s+(.*)/);
	my $footnote = $1;
	my $text = $2;
	die "duplicate footnote number $footnote" if defined($footnote_by_number{$footnote});
	$footnote_by_number{$footnote} = "$text\n";
	my $next;
	if ($f < $#footnotes) {
		$next = $footnotes[$f + 1];
	} else {
		$next = $#text + 1;
	}
	for ($l = $footnotes[$f] + 1; $l < $next; $l++) {
		next if ($text[$l] =~ m/^$/);
		$footnote_by_number{$footnote} .= $text[$l];
	}
}

#Format sections
my %label_by_section = ();
my $s;

my %latest_by_depth = ();

for ($s = 0; $s <= $#sections; $s++) {
	my $l = $sections[$s];
	die unless ($text[$l] =~ m/^(([0-9]+\.)+)\s+(.+)\s*/);
	my $section = $1;
	my $name = $3;
 	my @path = split(/\./, $section);
	my $depth = $#path - $skip_depth;
	if ($depth < 0) {
		$depth = 0;
	}
	if ($depth > $#depth2latex) {
		$depth = $#depth2latex;
	}
	$latest_by_depth{$#path} = $name;
 	my $type = $depth2latex[$depth];
	my $label = $name;
	#Prepend hierarchical path to make name unique
	for (my $i = 1; $i <= $#path - $skip_depth; $i++) {
		last if (not defined $latest_by_depth{$#path - $i});
		$label = "$latest_by_depth{$#path - $i} / $label";
	}
	$text[$l] = $type . "{$name}\\label{sec:$label}\n";
	$label_by_section{$section} = $label;
}

my $ifndef = 0;
my $listing = 0;
my $table = 0;
my $buffer = "";
for my $line (@text) {
	last if ($line =~ m/^FOOTNOTES:$/);
	next if (($line =~ m/^=======*$/) or
		 ($line =~ m/^-------*$/));

	if ($line =~ m/^#if/) {
		print "\\begin{lstlisting}\n";
		$ifndef++;
	}
	if ($ifndef) {
		if ($line =~ m/^#endif/) {
			$ifndef--;
		}
		$buffer .= $line;
		if (not $ifndef) {
			print $buffer;
			print "\\end{lstlisting}\n";
			$buffer = "";
		}
		next;
	}
	if (not $table and $line =~ m/^\+\-/) {
		print "\\begin{verbatim}\n";
		$table = 1;
	}
	if ($table and not $line =~ m/^(\+\-|\|)/) {
		print "\\end{verbatim}\n";
		$table = 0;
	}
	if (not $listing and $line =~ m/^\t/) {
		print "\\begin{lstlisting}\n";
		$listing = 1;
	}
	if ($listing and $line =~ m/^$/) {
		$buffer .= $line;
		next;
	}
	if ($listing and not $line =~ m/^\t/) {
		print "\\end{lstlisting}\n";
		$listing = 0;
	}

	if (not $table and not $listing) {
		if ($line =~ m/\S+\s*\^\s*\S+/) {
			$line =~ s/(\S+\s*\^\s*)(\S+)/\$$1\{$2\}\$/g;
		} else {
			$line =~ s/\^/\\^/go;
		}
		$line =~ s/#/\\#/go;
		$line =~ s/&/\\&/go;
		if ($line =~ m/\[[0-9]+\]/) { #premature optimization
			for my $n (keys(%footnote_by_number)) {
				my $txt = $footnote_by_number{$n};
				$line =~ s/\[$n\]/\n\\footnote{$txt}/g;
			}
		}
	}
	if ($line =~ m/"(([0-9]+\.)+)[^"]*"/) {
		my $section = $1;
		$line =~ s/"(([0-9]+\.)+)[^"]*"/\\ref{sec:$label_by_section{$section}}~\\nameref{sec:$label_by_section{$section}}/g;
	}
	print $buffer;
	$buffer = "";
	print $line;
}