#!/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 () { 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 () { 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 = ''; }