summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2017-03-26 17:26:59 +0200
committerFlorian Pritz <bluewind@xinu.at>2017-03-26 17:26:59 +0200
commit5e65f7a40ded1827be1317ee09fc65174cb673c2 (patch)
treea1ca158c0631e336e88b45b3a9699195b9d7c490
parent5d87e913a17ebfae71705004c695c2b572e12f40 (diff)
Extract render code into dedicated class
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r--application/controllers/file/file_default.php146
-rw-r--r--application/service/renderer.php166
2 files changed, 168 insertions, 144 deletions
diff --git a/application/controllers/file/file_default.php b/application/controllers/file/file_default.php
index 265bff0d8..826e3f4af 100644
--- a/application/controllers/file/file_default.php
+++ b/application/controllers/file/file_default.php
@@ -225,7 +225,8 @@ class File_default extends MY_Controller {
$output_cache->add(array("filedata" => $filedata), "file/fragments/asciinema-player");
} else {
$output_cache->add_function(function() use ($output_cache, $filedata, $lexer, $is_multipaste) {
- $this->_highlight_file($output_cache, $filedata, $lexer, $is_multipaste);
+ $renderer = new \service\renderer($this->mfile, $this->data);
+ $renderer->highlight_file($output_cache, $filedata, $lexer, $is_multipaste);
});
}
}
@@ -242,149 +243,6 @@ class File_default extends MY_Controller {
echo $this->load->view('file/html_footer', $this->data, true);
}
- private function _colorify($output_cache, $file, $lexer, $anchor_id = false)
- {
- $output = "";
- $lines_to_remove = 0;
-
- $output .= '<div class="code content table">'."\n";
- $output .= '<div class="highlight"><code class="code-container">'."\n";
-
- $content = file_get_contents($file);
-
- $linecount = count(explode("\n", $content));
- if ($lexer === "json" && $linecount === 1) {
- $decoded_json = json_decode($content);
- if ($decoded_json !== null && $decoded_json !== false) {
- $pretty_json = json_encode($decoded_json, JSON_PRETTY_PRINT);
- if ($pretty_json !== false) {
- $content = $pretty_json;
- $output_cache->render_now(
- array(
- "error_type" => "alert-info",
- "error_message" => "<p>The file below has been reformated for readability. It may differ from the original.</p>"
- ),
- "file/fragments/alert-wide"
- );
- }
- }
- }
-
- if ($lexer == "ascii") {
- // TODO: use exec safe and catch exception
- $ret = (new \libraries\ProcRunner(array('ansi2html', '-p')))
- ->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 "</pre></div>" and ""
- $lines_to_remove = 2;
- }
-
-
- $buf = explode("\n", $ret["stdout"]);
- $line_count = count($buf);
-
- for ($i = 1; $i <= $lines_to_remove; $i++) {
- unset($buf[$line_count - $i]);
- }
-
- foreach ($buf as $key => $line) {
- $line_number = $key + 1;
- if ($key == 0) {
- $line = str_replace("<div class=\"highlight\"><pre>", "", $line);
- }
-
- $anchor = "n$line_number";
- if ($anchor_id !== false) {
- $anchor = "n-$anchor_id-$line_number";
- }
-
- if ($line === "") {
- $line = "<br>";
- }
-
- // Be careful not to add superflous whitespace here (we are in a <code>)
- $output .= "<div class=\"table-row\">"
- ."<a href=\"#$anchor\" class=\"linenumber table-cell\">"
- ."<span class=\"anchor\" id=\"$anchor\"> </span>"
- ."</a>"
- ."<span class=\"line table-cell\">".$line."</span><!--\n";
- $output .= "--></div>";
- }
-
- $output .= "</code></div>";
- $output .= "</div>";
-
- return array(
- "return_value" => $ret["return_code"],
- "output" => $output
- );
- }
-
- private function _highlight_file($output_cache, $filedata, $lexer, $is_multipaste)
- {
- // highlight the file and cache the result, fall back to plain text if $lexer fails
- foreach (array($lexer, "text") as $lexer) {
- $highlit = cache_function($filedata['data_id'].'_'.$lexer, 100,
- function() use ($output_cache, $filedata, $lexer, $is_multipaste) {
- $file = $this->mfile->file($filedata['data_id']);
- if ($lexer == "rmd") {
- ob_start();
-
- echo '<div class="code content table markdownrender">'."\n";
- echo '<div class="table-row">'."\n";
- echo '<div class="table-cell">'."\n";
-
- require_once(APPPATH."/third_party/parsedown/Parsedown.php");
- $parsedown = new Parsedown();
- echo $parsedown->text(file_get_contents($file));
-
- echo '</div></div></div>';
-
- return array(
- "output" => ob_get_clean(),
- "return_value" => 0,
- );
- } else {
- return get_instance()->_colorify($output_cache, $file, $lexer, $is_multipaste ? $filedata["id"] : false);
- }
- });
-
- if ($highlit["return_value"] == 0) {
- break;
- } else {
- $message = "Error trying to process the file. Either the lexer is unknown or something is broken.";
- if ($lexer != "text") {
- $message .= " Falling back to plain text.";
- }
- $output_cache->render_now(
- array("error_message" => "<p>$message</p>"),
- "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,
- ));
-
- $output_cache->render_now($data, 'file/html_paste_header');
- $output_cache->render_now($highlit["output"]);
- $output_cache->render_now($data, 'file/html_paste_footer');
- }
-
-
private function _display_info($id)
{
if ($this->mmultipaste->id_exists($id)) {
diff --git a/application/service/renderer.php b/application/service/renderer.php
new file mode 100644
index 000000000..c6e58165b
--- /dev/null
+++ b/application/service/renderer.php
@@ -0,0 +1,166 @@
+<?php
+/*
+ * Copyright 2017 Florian "Bluewind" Pritz <bluewind@server-speed.net>
+ *
+ * Licensed under AGPLv3
+ * (see COPYING for full license text)
+ *
+ */
+
+namespace service;
+class renderer {
+
+
+ /**
+ * @param mixed $mfile
+ */
+ public function __construct($mfile, $data)
+ {
+ $this->mfile = $mfile;
+ $this->data = $data;
+ }
+
+ private function colorify($output_cache, $file, $lexer, $anchor_id = false)
+ {
+ $output = "";
+ $lines_to_remove = 0;
+
+ $output .= '<div class="code content table">'."\n";
+ $output .= '<div class="highlight"><code class="code-container">'."\n";
+
+ $content = file_get_contents($file);
+
+ $linecount = count(explode("\n", $content));
+ if ($lexer === "json" && $linecount === 1) {
+ $decoded_json = json_decode($content);
+ if ($decoded_json !== null && $decoded_json !== false) {
+ $pretty_json = json_encode($decoded_json, JSON_PRETTY_PRINT);
+ if ($pretty_json !== false) {
+ $content = $pretty_json;
+ $output_cache->render_now(
+ array(
+ "error_type" => "alert-info",
+ "error_message" => "<p>The file below has been reformated for readability. It may differ from the original.</p>"
+ ),
+ "file/fragments/alert-wide"
+ );
+ }
+ }
+ }
+
+ if ($lexer == "ascii") {
+ // TODO: use exec safe and catch exception
+ $ret = (new \libraries\ProcRunner(array('ansi2html', '-p')))
+ ->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 "</pre></div>" and ""
+ $lines_to_remove = 2;
+ }
+
+
+ $buf = explode("\n", $ret["stdout"]);
+ $line_count = count($buf);
+
+ for ($i = 1; $i <= $lines_to_remove; $i++) {
+ unset($buf[$line_count - $i]);
+ }
+
+ foreach ($buf as $key => $line) {
+ $line_number = $key + 1;
+ if ($key == 0) {
+ $line = str_replace("<div class=\"highlight\"><pre>", "", $line);
+ }
+
+ $anchor = "n$line_number";
+ if ($anchor_id !== false) {
+ $anchor = "n-$anchor_id-$line_number";
+ }
+
+ if ($line === "") {
+ $line = "<br>";
+ }
+
+ // Be careful not to add superflous whitespace here (we are in a <code>)
+ $output .= "<div class=\"table-row\">"
+ ."<a href=\"#$anchor\" class=\"linenumber table-cell\">"
+ ."<span class=\"anchor\" id=\"$anchor\"> </span>"
+ ."</a>"
+ ."<span class=\"line table-cell\">".$line."</span><!--\n";
+ $output .= "--></div>";
+ }
+
+ $output .= "</code></div>";
+ $output .= "</div>";
+
+ return array(
+ "return_value" => $ret["return_code"],
+ "output" => $output
+ );
+ }
+
+ public function highlight_file($output_cache, $filedata, $lexer, $is_multipaste)
+ {
+ // highlight the file and cache the result, fall back to plain text if $lexer fails
+ foreach (array($lexer, "text") as $lexer) {
+ $highlit = cache_function($filedata['data_id'].'_'.$lexer, 100,
+ function() use ($output_cache, $filedata, $lexer, $is_multipaste) {
+ $file = $this->mfile->file($filedata['data_id']);
+ if ($lexer == "rmd") {
+ ob_start();
+
+ echo '<div class="code content table markdownrender">'."\n";
+ echo '<div class="table-row">'."\n";
+ echo '<div class="table-cell">'."\n";
+
+ require_once(APPPATH."/third_party/parsedown/Parsedown.php");
+ $parsedown = new Parsedown();
+ echo $parsedown->text(file_get_contents($file));
+
+ echo '</div></div></div>';
+
+ return array(
+ "output" => ob_get_clean(),
+ "return_value" => 0,
+ );
+ } else {
+ return $this->colorify($output_cache, $file, $lexer, $is_multipaste ? $filedata["id"] : false);
+ }
+ });
+
+ if ($highlit["return_value"] == 0) {
+ break;
+ } else {
+ $message = "Error trying to process the file. Either the lexer is unknown or something is broken.";
+ if ($lexer != "text") {
+ $message .= " Falling back to plain text.";
+ }
+ $output_cache->render_now(
+ array("error_message" => "<p>$message</p>"),
+ "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,
+ ));
+
+ $output_cache->render_now($data, 'file/html_paste_header');
+ $output_cache->render_now($highlit["output"]);
+ $output_cache->render_now($data, 'file/html_paste_footer');
+ }
+
+
+}