58 lines
No EOL
2.3 KiB
PHP
58 lines
No EOL
2.3 KiB
PHP
<?php
|
|
|
|
class Klein {
|
|
private $code;
|
|
|
|
function compile($html) {
|
|
$pattern = array_map(function ($grammar) {
|
|
list(, $result) = $grammar;
|
|
$body = strtr($grammar[0], [' ' => "\s*", '+' => "\s+", ':var' => "(\w+(\s*\.\s*\w+)*)", ':id' => "(\w+)", '{{' => "{{\!?"]);
|
|
return ["/$body/",
|
|
function ($x) use ($result) {
|
|
$code = "<?php " . preg_replace_callback("/#(\d+)(s?)/", function ($s) use($x) {
|
|
$pref = $s[2] == 's' ? '$' : '';
|
|
return implode(", ", array_map(function ($s) use($pref) {
|
|
return $pref.preg_replace("/\.\s*(\w+)/", "['$1']", $s); }, preg_split("/\s*,\s*/", $x[$s[1]])));
|
|
}, $result) . " ?>";
|
|
return ($x[0][2] == '!') ? "<!-- $code -->" : $code;
|
|
}];
|
|
}, [
|
|
["{% for+:id+in+:var %}", "foreach(#2s as \$index => #1s): \$loop = Klein::loop(\$index, #2s);"],
|
|
["{% for+:id , :id+in+:var %}", "foreach(#3s as #1s => #2s):"],
|
|
["{% endfor %}", "endforeach;"],
|
|
["{% ifset+:var %}", "if(isset(#1s)):"],
|
|
["{% if+:var %}", "if(isset(#1s) && #1s):"],
|
|
["{% if+:id \(( :var (, :var )*)?\) %}", "if(macro_#1(#2s)):"],
|
|
["{% unless+:var %}", "if(!(isset(#1s) && #1s)):"],
|
|
["{% else %}", "else:"],
|
|
["{% endif %}", "endif;"],
|
|
["{{ :var }}", "echo #1s;"],
|
|
["{{ :id \(( :var (, :var )*)?\) }}", "echo macro_#1(#2s);"],
|
|
["{{ :var\|:id }}", "echo #3(#1s);"],
|
|
["{% macro+:id \(( :id (, :id )*)?\) %}", "function macro_#1(#2s) {"],
|
|
["{% endmacro %}", "}"]
|
|
]);
|
|
|
|
$result = file_get_contents($html);
|
|
foreach($pattern as $arg) {
|
|
$result = preg_replace_callback($arg[0], $arg[1], $result);
|
|
}
|
|
|
|
$this->code = $result;
|
|
}
|
|
|
|
static function loop($idx, &$array) {
|
|
$is_even = $idx % 2;
|
|
return ['first' => $idx == 0
|
|
, 'last' => $idx == count($array) - 1
|
|
, 'odd' => !$is_even
|
|
, 'even' => $is_even];
|
|
}
|
|
|
|
function render($vars) {
|
|
extract($vars);
|
|
ob_start();
|
|
eval(" ?>".$this->code."<?php ");
|
|
return ob_get_clean();
|
|
}
|
|
} |