File: podlatex.pl

package info (click to toggle)
remstats 1.0.13a-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 7,176 kB
  • ctags: 1,283
  • sloc: perl: 16,135; ansic: 2,776; sh: 1,061; makefile: 688
file content (377 lines) | stat: -rwxr-xr-x 9,795 bytes parent folder | download | duplicates (2)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!@@PERL@@ @@PERLOPTS@@

# podhtml - convert pod file to latex
#		The one major difference is that lines with only whitespace
#		are treated as empty lines.  If it looks like a paragraph
#		break, it is a paragraph break.
# $Id: podlatex.pl,v 1.6 2002/08/14 11:29:11 remstats Exp $
# from remstats @@VERSION@@

# Copyright 2001, 2002 (c) Thomas Erskine <@@AUTHOR@@>
# See the COPYRIGHT file with the distribution.

# - - -   Configuration   - - -

use strict;

# What is this program called, for error-messages and file-names
$main::prog = 'podlatex';
# What's the suffix for html files?
$main::html_suffix = '.html';
$main::html_suffix_pattern = '\.html';
# Debugging anyone?
$main::debug = 0;

# What's the suffix for pod files? (it's a regex)
$main::pod_suffix_pattern = '\.[^\.]+';

# - - -   Version History   - - -

$main::version = (split(' ', '$Revision: 1.6 $'))[1];

# - - -   Setup   - - -

use Getopt::Std;

# Parse the command-line
my %opt = ();
getopts('d:hs:S:u:', \%opt);

if (defined $opt{'h'}) { &usage; } # no return
if (defined $opt{'d'}) { $main::debug = $opt{'d'}; }
if (defined $opt{'s'}) { $main::html_suffix = $opt{'s'}; }
if (defined $opt{'S'}) { $main::pod_suffix_pattern = $opt{'S'}; }
if (defined $opt{'u'}) { $main::baseurl = $opt{'u'}; } else { $main::baseurl = ''; }

if ($#ARGV != 0) { &usage; } # no return
$main::podfile = shift @ARGV;
open (POD, "<$main::podfile") or &abort("can't open $main::podfile: $!");

if ($main::podfile =~ /^(.*)\.pod$/) { $main::latexfile = $1 .'.tex'; }
else { $main::latexfile = $main::podfile . '.tex'; }
open (LATEX, ">$main::latexfile") or &abort("can't open $main::latexfile: $!");

# Special characters and their substitution
%main::special = (
	'\\' => '$\\backslash$',
	'~' => '\~{}',
	'#' => '\\#',
	'$' => '\\$',
	'%' => '\\%',
	'^' => '\\^',
	'&' => '\\&',
	'_' => '\\_',
	'{' => '\\{',
	'}' => '\\}',
);
$main::special = quotemeta join('', keys %main::special);

# - - -   Mainline   - - -

my ($state, $line, $paragraph);

$state = 'between_paragraphs';
$main::cutting = 0;
$main::inlist = 0;
$main::startlist = 0;
$main::chapter = '';

while ($state ne 'done') {

	if ($state eq 'between_paragraphs') {
		$line = &readline;
		if (! defined $line) {
			$state = 'done';
		}
		elsif ($line !~ /^\s*$/) {
			$paragraph = $line;
			$state = 'reading_paragraph';
		}
	}

	elsif ($state eq 'reading_paragraph') {
		$line = &readline;
		if (! defined $line) {
			$state = 'done';
		}
		elsif ($line =~ /^\s*$/) {
			$state = 'done_paragraph';
		}
		else {
			$paragraph .= "\n" . $line;
		}
	}

	elsif ($state eq 'done_paragraph') {
		&process($paragraph);
		$state = 'between_paragraphs';
		undef $paragraph;
	}

	else {
		&abort("unknown internal state: $state");
	}

}
&process($paragraph);
close(LATEX);
close (POD);

exit 0;

#----------------------------------------------------------------- usage ---
sub usage {
	print STDERR <<"EOD_USAGE";
$main::prog version $main::version from remstats @@VERSION@@
usage: $main::prog [options] podfile
where options are:
	-d ddd  enable debugging output
	-h      show this help
	-s sss  use 'sss' as the suffix for html files [$main::html_suffix]
	-u uuu  use 'uuu' as a URL prefix
EOD_USAGE
	exit 0;
}

#----------------------------------------------------------------- debug ---
sub debug {
	print STDERR 'DEBUG: ', @_, "\n";
}

#------------------------------------------------------------------ error ---
sub error {
	print STDERR 'ERROR: ', @_, "\n";
}

#------------------------------------------------------------------ abort ---
sub abort {
	print STDERR 'ABORT: ', @_, "\n";
	exit 6;
}

#--------------------------------------------------------------- readline ---
sub readline {
	my $line = <POD>;
	chomp $line if (defined $line);
$line;
}

#----------------------------------------------------------------- process ---
sub process {
	my ($paragraph) = @_;
	my ($startitem, $level, $word1, $word2, $rest, $what, $name);

	return unless (defined $paragraph);
	($word1, $word2, $rest) = split(' ', $paragraph,3);
	unless (defined $word2) { $word2 = ''; }
	unless (defined $rest) { $rest = ''; }
	$name = &fixup($word2 .' '. $rest);

	if ($word1 eq '=pod') { $main::cutting = 0; }

	elsif ($main::cutting) {
		if ($rest =~ /^SECTION=(.*)/m) {
			$main::chapter = $1;
		}
		return;
	}

	elsif ($word1 eq '=cut') { $main::cutting = 1; }

	elsif ($word1 =~ /^=head(\d)$/i) {
		$level = $1;
		if ($level == 1 and $main::chapter) {
			print LATEX "\n\\chapter\{". &fixup($main::chapter) ."}\n".
				"\\index{". $main::chapter ."}\n";
			if ($main::chapter eq $name) { $what = 'done'; }
			else { $what = 'section'; }
		}
		elsif ($level == 1) { $what = 'section'; }
		elsif ($level == 2) { $what = 'subsection'; }
		else {
			&error("I can't handle =head$level; treated as =head2");
			$what = 'subsection';
		}
		if (not defined $what) {
			&error("word1='$word1', name='$name', rest=$rest");
		}
		elsif ($what ne 'done') {
			print LATEX "\n\\${what}\{". $name ."}%\n".
				"\\index{". $name ."}\n";
		}
	}

	elsif ($word1 eq '=over') { $main::inlist++; $main::startlist = 1; }
	elsif ($word1 eq '=back') {
		print LATEX "\n". $main::endlist[$main::inlist];
		$main::inlist--;
		$main::startlist = 0;
	}

	elsif ($word1 eq '=item') {
		if ($main::startlist) {
			if ($word2 =~ /^\d/) {
				print LATEX "\n\\begin{enumerate}\n";
				$main::lose_word2[$main::inlist] = 1;
				$main::endlist[$main::inlist] = "\n\\end{enumerate}";
			}
			elsif ($word2 =~ /^[-+o\*]$/) {
				print LATEX "\n\\begin{itemize}\n";
				$main::lose_word2[$main::inlist] = 1;
				$main::endlist[$main::inlist] = "\n\\end{itemize}";
			}
			else {
				$main::lose_word2[$main::inlist] = 0;
				print LATEX "\n\\begin{itemize}\n";
				$main::endlist[$main::inlist] = "\n\\end{itemize}";
			}
		}
		$startitem = "\n\n\\item ";

		$main::startlist = 0;
		if ($main::lose_word2[$main::inlist]) { $word2 = ''; }
		print LATEX ($startitem . &fixup($word2 .' '. $rest));
	}

	elsif ($word1 eq '=for' and $word2 =~ /^latex$/i) {
		print LATEX $rest . "\n";
	}

	elsif ($word1 eq '=for') {
		# junk it; it's not for html
	}

	elsif ($word1 eq '=exec') {
		$rest =~ s/\$\\backslash\$/\\/g;
		print LATEX "\n\\begin{verbatim}%\n". `$word2 $rest` ."\\end{verbatim}\n";
	}

	elsif ($word1 =~ /^=/) {
		&error("unknown pod keyword ($word1); ignored");
		print LATEX &fixup($word2 .' '. $rest);
	}

	elsif ($paragraph =~ /^\s/) {
		print LATEX "\n\\begin{verbatim}%\n". &fixup($paragraph) . "\\end{verbatim}\n";
	}

	else {
		print LATEX "\n". &fixup($paragraph) ."\n";
	}
}

#---------------------------------------------------------------- fixup ---
sub fixup {
	my ($text) = @_;

#	$text =~ s/\\/\044\\backslash\044/g;
#	$text =~ s/([\&\$\^\_\#\%\{\}])/\\$1/g;
##	$text =~ s/\_/\\\_/g;
##	$text =~ s/\&/\\\&/g;
##	$text =~ s/\$/\\\$/g;
##	$text =~ s/\^/\\\^/g;
##	$text =~ s/\#/\\\#/g;
##	$text =~ s/\%/\\\%/g;
##	$text =~ s/\{/\\\{/g;
##	$text =~ s/\}/\\\}/g;
#	$text =~ s/\~/\\verb|~|/g;
	$text =~ s/([$main::special])/$main::special{"$1"}/eg;
	$text =~ s/([A-Z])<(.*?)>/&do_tag("$1", "$2")/egs;
	$text =~ s/([<>])/\$$1\$/g;

$text;
}

#---------------------------------------------------------------- do_tag ---
sub do_tag {
	my ($tag, $text) = @_;

	&debug("do_tag: $tag, text='$text'") if ($main::debug>1);
	if ($tag eq 'I') { $text = '\\textit{'. $text .'}'; }
	elsif ($tag eq 'B') { $text = '\\textbf{'. $text .'}'; }
	elsif ($tag eq 'C') { $text = '\\texttt{'. $text .'}'; }
	elsif ($tag eq 'E') { $text = &do_escape($text); }
	elsif ($tag eq 'F') { $text = '\\textif{'. $text .'}'; }
	elsif ($tag eq 'L') { $text = &do_link($text); }
	elsif ($tag eq 'S') { $text =~ s/ /\\ /g; }
	elsif ($tag eq 'U') { $text = '\\textsl{'. $text .'}'; }
	elsif ($tag eq 'X') { $text = &do_index($text); }
	elsif ($tag eq 'Z') { $text = ''; }
	else { &debug("unknown pod tag ($tag); ignored") if($main::debug); }
	&debug("do_tag: done text='$text'") if ($main::debug>1);

$text;
}

#--------------------------------------------------------------- do_link ---
sub do_link {
	my ($text) = @_;
	my ($linktext, $link, $sublink);

	if ($text =~ /^([^\|]+)\|(.*)$/) { $linktext = $1; $link = $2; }
	else { $linktext = $text; $link = $text; }

	if ($link =~ m#^(http:|ftp:)#) { $sublink = ''; }
	elsif ($link =~ m#^([^/]*)/(.*)$#) { $link = $1; $sublink = '#'. $2; }
	else { $sublink = ''; }

	if ($link !~ /$main::html_suffix_pattern$/i and 
			$link !~ /\./) {
		$link .= $main::html_suffix;
	}

	if ($link =~ /^(http:|ftp:)/) {
		$text = $linktext .' (see \\textbf{'. $link . $sublink .'})';
	}
	else {
		$text = $linktext .' (see the '. $linktext .' section) ';
	}

$text;
}

#--------------------------------------------------------------- do_index ---
sub do_index {
	my ($text) = @_;

$text;
}

#--------------------------------------------------------------- do_escape ---
sub do_escape {
	my ($text) = @_;

	if    ($text eq 'lt') { $text = '<'; }
	elsif ($text eq 'gt') { $text = '>'; }
	elsif ($text eq 'sol') { $text = '/'; }
	elsif ($text eq 'verbar') { $text = '|'; }
	elsif ($text =~ /^\d+$/) { $text = '\\unknowncharacter{'. $text .'}'; }
	else { $text = '\\unknowncharacter{'. $text . '}'; }

$text;
}

#--------------------------------------------------------- makename ---
sub makename {
	my ($text) = @_;
	my $name = $text;
	my $thisurl = $main::podfile;

	$thisurl =~ s/$main::pod_suffix_pattern$/$main::html_suffix/i;

	$name =~ s/^\s+//gsm;
	$name =~ s/\s+$//gsm;
	$name =~ s/\s+/_/gsm;
	$name =~ s/[A-Z]<(.*?)>/$1/gsm;
	$name =~ tr/A-Z/a-z/;
	$name =~ tr/-a-z0-9\._//cd;

	$text =~ s/[A-Z]<([^\>\|]+)(|[^\>]+)?>/$1/gsm;

	open (INDEX, ">>podhtml--rawindex") or 
		&abort("$main::prog: can't open podhtml--rawindex: $!");
	print INDEX "${main::baseurl}${thisurl}#${name} $text\n";
	close (INDEX);

$name;
}