Last active
August 29, 2015 14:26
-
-
Save masudiiuc/f77eb12766c823e48f4a to your computer and use it in GitHub Desktop.
html2js
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 | |
function bundleHtml($file) | |
{ | |
$html = file_get_contents($file); | |
$html = str_replace(array("\r\n", "\n", "\r"), " ", $html); | |
$html = str_replace("'", "\\'", $html); | |
$html = preg_replace("/\s+/", " ", $html); | |
return $html; | |
} | |
$dirIterator = new DirectoryIterator(__DIR__ . '/../templates'); | |
$bundle = "Templates = {};\n"; | |
foreach ($dirIterator as $file) { | |
if ($file->isFile()) { | |
$html = bundleHtml($file->getPathname()); | |
$bundle .= "Templates." . $file->getBasename('.html') . " = '" . $html . "';\n"; | |
} | |
if ($file->isDir()) { | |
if ($file != '.' && $file != '..') { | |
$fDirIterator = new DirectoryIterator(__DIR__ . '/../templates/'. $file); | |
foreach ($fDirIterator as $f) { | |
if ($f->isFile()) { | |
$html = bundleHtml($f->getPathname()); | |
$bundle .= "Templates." . $f->getBasename('.html') . " = '" . $html . "';\n"; | |
} | |
} | |
} | |
} | |
} | |
file_put_contents(__DIR__ . '/../build/templates.js', $bundle); | |
if (php_sapi_name() == 'cli') { | |
echo "Templates bundle created at build/templates.js for inclusion.", PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment