Last active
July 22, 2017 11:26
-
-
Save logarytm/a5c7b4ebd8f166fb8884 to your computer and use it in GitHub Desktop.
preprocessor for arbitrary text
This file contains hidden or 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
| <?php | |
| $config = ...; | |
| $input = file_get_contents('php://stdin'); | |
| $output = $input; | |
| $opening = '<%'; | |
| $closing = '%>'; | |
| for ($offset = strpos($output, $opening); $offset !== false; $offset = strpos($output, $opening)) { | |
| $endOffset = strpos($output, $closing, $offset); | |
| $expression = substr($output, $offset + strlen($opening), $endOffset - $offset - strlen($opening)); | |
| $expression = trim($expression); | |
| $output = substr_replace($output, evaluate($expression, $config), $offset, $endOffset - $offset + 2); | |
| } | |
| function evaluate($expression, $config) | |
| { | |
| $value = ''; | |
| $command = $expression[0]; | |
| $inner = trim(substr($expression, 1)); | |
| if ($command === '=') { | |
| $chunks = explode('.', $inner); | |
| $value = $config; | |
| foreach ($chunks as $chunk) { | |
| $value = $value[$chunk]; | |
| } | |
| } else if ($command === '.') { | |
| return file_get_contents($inner); | |
| } | |
| return $value; | |
| } | |
| echo $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment