From 36f0185d8c950933099268c69081cabc084545c9 Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Mon, 15 Oct 2012 23:38:44 +0200 Subject: Fall back to plain text if lexer is unknown/broken Signed-off-by: Florian Pritz --- application/controllers/file.php | 59 +++++++++++++++++++++++++++++++--------- 1 file changed, 46 insertions(+), 13 deletions(-) (limited to 'application/controllers') 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 ''; echo '
'."\n"; passthru('perl '.FCPATH.'scripts/Markdown.pl '.escapeshellarg($file), $return_value); + + $cached = ob_get_contents(); + ob_end_clean(); } elseif ($lexer == "ascii") { + ob_start(); + + echo ''; echo '
'."\n";
 				passthru('perl '.FCPATH.'scripts/ansi2html '.escapeshellarg($file), $return_value);
 				echo "
\n"; + + $cached = ob_get_contents(); + ob_end_clean(); } else { - echo '
';
-				// generate line numbers (links)
-				passthru('perl -ne \'print "$.\n"\' '.escapeshellarg($file), $return_value);
-				echo '
'."\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 = "

Error trying to process the file. + Either the lexer is unknown or something is broken. + Falling back to plain text.

\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 ''; + echo '
';
+		// generate line numbers (links)
+		passthru('perl -ne \'print "$.\n"\' '.escapeshellarg($file), $return_value);
+		echo '
'."\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"; -- cgit v1.2.3-24-g4f1b