diff options
author | Florian Pritz <bluewind@xinu.at> | 2012-10-15 23:38:44 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2012-10-16 22:12:32 +0200 |
commit | 36f0185d8c950933099268c69081cabc084545c9 (patch) | |
tree | ddfdc602a15080d2de464aa670a9715f33294da4 /application/controllers | |
parent | 143d920cb703d47bd8606ba2497241fce7d06a27 (diff) |
Fall back to plain text if lexer is unknown/broken
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application/controllers')
-rw-r--r-- | application/controllers/file.php | 59 |
1 files changed, 46 insertions, 13 deletions
diff --git a/application/controllers/file.php b/application/controllers/file.php index 427af33dc..2cf84016b 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -179,35 +179,68 @@ class File extends CI_Controller { // highlight the file and chache the result $this->load->library("MemcacheLibrary"); if (! $cached = $this->memcachelibrary->get($filedata['hash'].'_'.$lexer)) { - ob_start(); if ($lexer == "rmd") { + ob_start(); + + echo '<table class="content"><tr>'; echo '<td class="markdownrender">'."\n"; passthru('perl '.FCPATH.'scripts/Markdown.pl '.escapeshellarg($file), $return_value); + + $cached = ob_get_contents(); + ob_end_clean(); } elseif ($lexer == "ascii") { + ob_start(); + + echo '<table class="content"><tr>'; echo '<td class="code"><pre class="text">'."\n"; passthru('perl '.FCPATH.'scripts/ansi2html '.escapeshellarg($file), $return_value); echo "</pre>\n"; + + $cached = ob_get_contents(); + ob_end_clean(); } else { - echo '<td class="numbers"><pre>'; - // generate line numbers (links) - passthru('perl -ne \'print "<a href=\"#n$.\" id=\"n$.\">$.</a>\n"\' '.escapeshellarg($file), $return_value); - echo '</pre></td><td class="code">'."\n"; - passthru('pygmentize -F codetagify -O encoding=guess,outencoding=utf8 -l '.escapeshellarg($lexer).' -f html '.escapeshellarg($file), $return_value); + $ret = $this->_pygmentize($file, $lexer); + $return_value = $ret["return_value"]; + $cached = $ret["output"]; + } + + if ($return_value != 0) { + $cached = "<div class=\"error\"><p>Error trying to process the file. + Either the lexer is unknown or something is broken. + Falling back to plain text.</p></div>\n"; + $ret = $this->_pygmentize($file, "text"); + $cached .= $ret["output"]; } - $cached = ob_get_contents(); - ob_end_clean(); $this->memcachelibrary->set($filedata['hash'].'_'.$lexer, $cached, 100); } - if ($return_value != 0) { - show_error("Error trying to process the file. Either the lexer is unknown or something is broken.\n"); - } else { - $this->output->append_output($cached); - } + $this->output->append_output($cached); $this->load->view($this->var->view_dir.'/html_footer', $this->data); } + private function _pygmentize($file, $lexer) + { + $return_value = 0; + + ob_start(); + + echo '<table class="content"><tr>'; + echo '<td class="numbers"><pre>'; + // generate line numbers (links) + passthru('perl -ne \'print "<a href=\"#n$.\" id=\"n$.\">$.</a>\n"\' '.escapeshellarg($file), $return_value); + echo '</pre></td><td class="code">'."\n"; + passthru('pygmentize -F codetagify -O encoding=guess,outencoding=utf8 -l '.escapeshellarg($lexer).' -f html '.escapeshellarg($file), $return_value); + + $output = ob_get_contents(); + ob_end_clean(); + + return array( + "return_value" => $return_value, + "output" => $output + ); + } + function _display_info($id) { $this->data["title"] .= " - Info $id"; |