Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Last active September 27, 2017 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pesterhazy/e59d8c5e863fc45d7317c6dc99c994e4 to your computer and use it in GitHub Desktop.
Save pesterhazy/e59d8c5e863fc45d7317c6dc99c994e4 to your computer and use it in GitHub Desktop.
rplcr - Simple HTML templating
#!/usr/bin/env perl
#
# rplcr: Simple HTML templating using perl
#
# HTML templating in the spirit of "the simplest thing that could possibly work".
#
# Matches lines ending in
#
# <!-- $RPLCR$ some_perl_code_goes_here -->
#
# where some_perl_code_goes_here is arbitrary perl code that gets
# applied to the line.
#
# Example:
#
# <script src="js/lib.js"></script> <!-- $RPLCR$ s:(js/):$ENV{ASSET_PREFIX}.$1:e -->
#
# Used like this:
#
# ASSET_PREFIX=my-asset-id rplcr index.html
#
# Or if you want in-place editing:
#
# ASSET_PREFIX=my-asset-id perl -i rplcr index.html
while (<>) {
$line = $_;
if ($line =~ /^(.*)<!--\s+\$RPLCR\$\s+(.*?)-->\s*$/) {
$content = $1;
$code = $2;
$_ = $1;
eval($code);
print "$_\n";
}
else {
print $line;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment