parseFile($file); } else { return "\n"; } } // switch } /** * @brief opens file and parses it for commands * @param $filename [String] path and file name * @return [String] content of the file (after commands execution) */ public function parseFile($filename){ //first create head of the HTML file $lines = Events::rise(Events::ON_HEADER); $head = implode("\n", $lines); //now create HTML body $lines = Events::rise(Events::ON_BODY_START); $body = implode("\n", $lines); //parse file $file = file($filename); $this->process(&$file); $body .= implode("\n", $file); //get end of body $lines = Events::rise(Events::ON_BODY_END); $body .= implode("\n", $lines); //create final HTML file return << $head $body HTML; } //parseFile() /** * @brief processes lines and executes commands * @param $lines [&Array of Strings] lines to process * @param $commandMap [Array of String] if command found as index, value will be uses instead * @return [void] */ public function process(&$lines, $commandMap = array()) { $emptyLine = false; foreach($lines as $row => $line){ $trim = trim($line); if (preg_match('/^\#([^\ ]+)(\ (.*))?$/', $trim, $params)) { $command = $params[1]; $params = array_key_exists(3, $params) ? $params[3] : ''; if (array_key_exists($command, $commandMap)) { $params = $command . ' ' . $params; $command = $commandMap[$command]; } $result = Plugins::executeCommand($command, $params); if (false === $result) { throw new Exception('Unknown command "'.$command.'"'); } else { $lines[$row] = $result; } } else { //line w/o command if ($emptyLine AND preg_match('/^[^\<](.*)?$/', $trim, $params)) { //paragraph following after empty line is converted into HTML paragraph $lines[$row] = '

'.$trim.'

'; } $emptyLine = ("" === $trim); } } //foreach(line in file) } } //Register this class as Interpreter and Observer Plugins::register(Plugins::OBSERVER, 'HtmlParser'); ?>