aboutsummaryrefslogtreecommitdiff
path: root/maketpl
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-09-17 01:14:23 +0200
committerJohn Ankarström <john@ankarstrom.se>2021-09-17 01:14:23 +0200
commit470fc96361fe47b5b4a72deaa8fb3b736f30a471 (patch)
tree1cf91d0697388006ae9748e41fced9609e316ac2 /maketpl
downloadcforum-470fc96361fe47b5b4a72deaa8fb3b736f30a471.tar.gz
First commit
Diffstat (limited to 'maketpl')
-rwxr-xr-xmaketpl55
1 files changed, 55 insertions, 0 deletions
diff --git a/maketpl b/maketpl
new file mode 100755
index 0000000..7908f3b
--- /dev/null
+++ b/maketpl
@@ -0,0 +1,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 = '';
+} \ No newline at end of file