private function processDirectives($content)
{
/**
* Recognizes :
* ALl:
* NEWLINE" *=" (processed only in comment)
* JS:
* "//="
* "#="
*/
if (false === strpos($content, "\n *=") && false === strpos($content, '//=') && false === strpos($content, '#=')) {
return $content;
}
$new_content = '';
$lines = explode("\n", $content);
$in_comment = false;
for ($i = 0, $len = count($lines); $i < $len; ++$i) {
$line = $lines[$i];
if (substr($line, 0, 2) == '/*') {
$in_comment = true;
continue;
}
if ($in_comment && substr(trim($line), 0, 2) == '*/') {
$in_comment = false;
continue;
}
//a bit verbose, but definitely more readable ...
$is_directive = false;
if ($in_comment && substr(trim($line), 0, 2) == '*=') {
$is_directive = true;
}
if ($this->type == 'js') {
if (substr($line, 0, 3) == '//=') {
$is_directive = true;
}
if (substr($line, 0, 2) == '#=') {
$is_directive = true;
}
}
if ($is_directive) {
$directive = explode(' ', trim(substr($line, 3)));
$function = $directive[0];
$argument = $directive[1];
$last = substr($argument, -1);
while ($last == '\\' || $last == ',') {
//parse as many lines as needed
if ($last == '\\') {
// remove trailing \ remove leading //
$argument = substr($argument, 0, -1) . trim(substr($next = trim($lines[++$i]), 2));
} else {
if ($last == ',') {
$next = trim($lines[++$i]);
$argument .= ltrim($next, '/#*=');
}
}
$last = substr($next, -1);
}
$method = pascalize($function) . 'Directive';
if (!method_exists($this, $method)) {
throw new \RuntimeException('Cannot parse file ' . $this->path . ', unknow directive ' . $function);
}
$new_content .= call_user_func(array($this, $method), $argument) . "\n";
} else {
if (!$in_comment) {
//we can't balance comments.
$new_content .= $line . "\n";
}
}
}
return $new_content;
}