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 ++++++++++++++++++++++++++-------- application/views/file/html_header.php | 2 -- data/paste.css | 6 ++++ 3 files changed, 52 insertions(+), 15 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 ''; 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"; diff --git a/application/views/file/html_header.php b/application/views/file/html_header.php index cd2116ada..60db38238 100644 --- a/application/views/file/html_header.php +++ b/application/views/file/html_header.php @@ -50,5 +50,3 @@ if ("onhashchange" in window) { } /* ]]> */ - - diff --git a/data/paste.css b/data/paste.css index c8f165a75..925f432d4 100644 --- a/data/paste.css +++ b/data/paste.css @@ -83,6 +83,12 @@ a.no { a.raw_link { color: lightblue; } + +.error { + padding: 10px 20px; + background: #fcc; +} + .hll { background-color: #ffffcc } .c { color: #408080; font-style: italic } /* Comment */ .err { border: 1px solid #FF0000 } /* Error */ -- cgit v1.2.3-24-g4f1b