* * Licensed under AGPLv3 * (see COPYING for full license text) * */ namespace service; class renderer { /** * @param $output_cache output cache object * @param $mfile mfile object * @param $data data for the rendering of views */ public function __construct($output_cache, $mfile, $data) { $this->output_cache = $output_cache; $this->mfile = $mfile; $this->data = $data; } private function colorify($file, $lexer, $anchor_id = false) { $output = ""; $lines_to_remove = 0; $output .= '
'."\n";
$content = file_get_contents($file);
$linecount = count(explode("\n", $content));
$content = $this->reformat_json($lexer, $linecount, $content);
if ($lexer == "ascii") {
// TODO: use exec safe and catch exception
$ret = (new \libraries\ProcRunner(array('ansi2html', '-p', '-m')))
->input($content)
->forbid_stderr()
->exec();
// Last line is empty
$lines_to_remove = 1;
} else {
// TODO: use exec safe and catch exception
$ret = (new \libraries\ProcRunner(array('pygmentize', '-F', 'codetagify', '-O', 'encoding=guess,outencoding=utf8,stripnl=False', '-l', $lexer, '-f', 'html')))
->input($content)
->exec();
// Last 2 items are "
", "", $line);
}
$anchor = "n$line_number";
if ($anchor_id !== false) {
$anchor = "n-$anchor_id-$line_number";
}
if ($line === "") {
$line = "
";
}
// Be careful not to add superflous whitespace here (we are in a )
$output .= ""
.""
." "
.""
."".$line."";
}
$output .= "
$message
"), "file/fragments/alert-wide" ); } } $data = array_merge($this->data, array( 'title' => htmlspecialchars($filedata['filename']), 'id' => $filedata["id"], 'current_highlight' => htmlspecialchars($lexer), 'timeout' => $this->mfile->get_timeout_string($filedata["id"]), 'filedata' => $filedata, )); $this->output_cache->render_now($data, 'file/html_paste_header'); $this->output_cache->render_now($highlit["output"]); $this->output_cache->render_now($data, 'file/html_paste_footer'); } /** * @param $lexer * @param $linecount * @param $content * @return string */ private function reformat_json($lexer, $linecount, $content) { if ($lexer !== "json" || $linecount !== 1) { return $content; } $decoded_json = json_decode($content); if ($decoded_json === null || $decoded_json === false) { return $content; } $pretty_json = json_encode($decoded_json, JSON_PRETTY_PRINT); if ($pretty_json === false) { return $content; } $this->output_cache->render_now( array( "error_type" => "alert-info", "error_message" => "The file below has been reformated for readability. It may differ from the original.
" ), "file/fragments/alert-wide" ); return $pretty_json; } }