Last active
December 28, 2015 20:19
-
-
Save BriSeven/7556390 to your computer and use it in GitHub Desktop.
mustachc for doing mustache templating from makefiles and the bash command line. Requires mustache.php and php itself.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
// Command line utility to compile MUSTACHE to STDOUT | |
// I bastardised this based on lessc by: | |
// Leaf Corcoran <leafot@gmail.com>, 2012 | |
// which is licensed by MIT/GPL according to his website. | |
// Additionally, this requires mustache.php to be in the same directory | |
// Mustache.php can be "compiled" to be in a single file | |
// following these instructions https://twitter.com/bobthecow/status/319159329578180609 | |
// (quoted below) | |
// ProTip™ for Mustache.php v2.3 — http://hile.mn/XUgSFc | |
// | |
// Running `bin/build_bootstrap.php` creates a single-file `mustache.php` include. Hot. | |
// -@bobthecow | |
// | |
// the mustachec command includes a help screen, in the heredoc just below. | |
$exe = array_shift($argv); // remove filename | |
$HELP = <<<EOT | |
Usage: $exe [options] [mustache-file] [json-file] | |
Options include: | |
-h, --help Show this message | |
-v Print the version | |
-r Read from STDIN instead of json-file | |
-e use htmlentities instead of htmlspecialchars | |
-d don't double encode entities. | |
EOT; | |
$opts = getopt('hvred:', array('help')); | |
while (count($argv) > 0 && preg_match('/^-([-hvred])/', $argv[0])) { | |
array_shift($argv); | |
} | |
function has() { | |
global $opts; | |
foreach (func_get_args() as $arg) { | |
if (isset($opts[$arg])) return true; | |
} | |
return false; | |
} | |
if (has("h", "help")) { | |
exit($HELP); | |
} | |
error_reporting(E_ALL); | |
$path = realpath(dirname(__FILE__)).'/'; | |
require $path."mustache.php"; | |
$VERSION = Mustache_Engine::VERSION; | |
if (has("v")) { | |
exit($VERSION."\n"); | |
} | |
if (!$fname = array_shift($argv)) { | |
echo $HELP; | |
exit(1); | |
} | |
if (has("r")) { | |
if (!empty($argv)) { | |
$data = $argv[0]; | |
} else { | |
$data = ""; | |
while (!feof(STDIN)) { | |
$data .= fread(STDIN, 8192); | |
} | |
} | |
exit(process($data)); | |
} | |
$fa = "Fatal Error: "; | |
function err($msg) { | |
fwrite(STDERR, $msg."\n"); | |
} | |
if (php_sapi_name() != "cli") { | |
err($fa.$argv[0]." must be run in the command line."); | |
exit(1); | |
} | |
function make_mustache() { | |
global $opts; | |
global $fname; | |
if(has("e")){ | |
$m = new Mustache_Engine(array( | |
'loader' => new Mustache_Loader_FilesystemLoader(getcwd()), | |
'escape' => function($value) { | |
return htmlentities($value, ENT_COMPAT, 'UTF-8',!has("d")); | |
} | |
)); | |
} else { | |
$m = new Mustache_Engine(array( | |
'loader' => new Mustache_Loader_FilesystemLoader(getcwd()), | |
'escape' => function($value) { | |
return htmlspecialchars($value, ENT_COMPAT, 'UTF-8',!has("d")); | |
} | |
)); | |
} | |
$t = $m->loadTemplate($fname); | |
return $t; | |
} | |
function process($data=''){ | |
$m = make_mustache(); | |
$j = json_decode($data,TRUE); | |
if($j == NULL) { | |
err("json error: ".json_last_error()); | |
} | |
return $m->render($j); | |
return "processing\n"; | |
} | |
try { | |
while($fjson = array_shift($argv)){ | |
echo process(file_get_contents($fjson)); | |
} | |
} catch (exception $ex) { | |
err($fa.$ex->getMessage()); | |
exit(1); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment