aboutsummaryrefslogtreecommitdiff
path: root/maketpl
blob: 7908f3b1dbf5f58c77260e43301c8aa35f7ba7a5 (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
#!/usr/bin/env perl

# `maketpl' translates a template file (.t) to a C program (.tc)
# that prints that template with the desired interpolations.

use strict;
use warnings;

my $buf;

while (<STDIN>) {
	if (/(.*)\<%=(.+)%\>(.*)/) {
		$buf .= $1;
		flsh();
		print qq(printf("%s", $2);\n);
		if ($3) {
			$buf .= "$3\n";
		}
	} elsif (/(.*)\<%/) {
		my $code;
		$buf .= $1;
		flsh();
		if (/\<%(.*)%\>(.*)/) {
			$code = $1;
			$buf .= $2;
			$buf .= "\n";
		} else {
			/\<%(.*)/;
			$code = $1;
			while (<STDIN>) {
				if (/(.*)%\>(.*)/) {
					$code .= $1;
					$buf .= $2;
					$buf .= "\n";
					last;
				}
				$code .= $_;
			}
		}
		print $code;
	} else {
		$buf .= $_;
	}
}

flsh();

sub flsh {
	return if not $buf;
	$buf =~ s/\\/\\\\/g;
	$buf =~ s/"/\\"/g;
	$buf =~ s/\n/\\n/g;
	print qq(printf("$buf");\n);
	$buf = '';
}