Mark grauphel as compatible with Nextcloud 16
[grauphel.git] / controller / notescontroller.php
index 5618bcbb04b33473e9be78a88cb36e4abe4dc4bf..a54cfe31f1c868eefe2c2184fb1856236d0d3942 100644 (file)
@@ -47,6 +47,143 @@ class NotesController extends Controller
         header('HTTP/1.0 500 Internal Server Error');
     }
 
+    /**
+     * Output a note as a standalone HTML file
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     */
+    public function html($guid)
+    {
+        $note = $this->getNotes()->load($guid, false);
+        if ($note === null) {
+            $res = new ErrorResponse('Note does not exist');
+            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
+            return $res;
+        }
+
+        $xw = new \XMLWriter();
+        $xw->openMemory();
+        $xw->setIndent(true);
+        $xw->setIndentString(' ');
+        $xw->startDocument('1.0', 'utf-8');
+        $xw->writeRaw(
+            '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
+            . ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
+            . "\n"
+        );
+
+        $xw->startElementNS(null, 'html', 'http://www.w3.org/1999/xhtml');
+
+        //head
+        $xw->startElement('head');
+        $xw->writeElement(
+            'title',
+            htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
+        );
+
+        $xw->startElement('meta');
+        $xw->writeAttribute('name', 'author');
+        $xw->writeAttribute('content', $this->user->getDisplayName());
+        $xw->endElement();
+
+        $xw->startElement('meta');
+        $xw->writeAttribute('http-equiv', 'Content-Type');
+        $xw->writeAttribute('content', 'text/html; charset=utf-8');
+        $xw->endElement();
+
+        $xw->startElement('link');
+        $xw->writeAttribute('rel', 'schema.DC');
+        $xw->writeAttribute('href', 'http://purl.org/dc/elements/1.1/');
+        $xw->endElement();
+
+        $xw->startElement('meta');
+        $xw->writeAttribute('name', 'DC.date.created');
+        $xw->writeAttribute(
+            'content', date('c', strtotime($note->{'create-date'}))
+        );
+        $xw->endElement();
+
+        $xw->startElement('meta');
+        $xw->writeAttribute('name', 'DC.date.modified');
+        $xw->writeAttribute(
+            'content', date('c', strtotime($note->{'last-change-date'}))
+        );
+        $xw->endElement();
+
+        $xw->endElement();//head
+
+        //body
+        $xw->startElement('body');
+        $xw->writeElement(
+            'h1', htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5)
+        );
+
+        $converter = new \OCA\Grauphel\Converter\CleanHtml();
+        $converter->internalLinkHandler = array($this, 'htmlNoteLinkHandler');
+        try {
+            $xw->writeRaw(
+                $converter->convert($note->{'note-content'})
+            );
+        } catch (\OCA\Grauphel\Converter\Exception $e) {
+            $res = new ErrorResponse(
+                'Error converting note to HTML.'
+                . ' Please report a bug to the grauphel developers.'
+            );
+            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
+            return $res;
+        }
+
+        $xw->endElement();//body
+
+        $xw->endElement();//html
+        return new \OCA\Grauphel\Response\XmlResponse($xw->outputMemory());
+    }
+
+    public function htmlNoteLinkHandler($noteTitle)
+    {
+        return urlencode($noteTitle) . '.html';
+    }
+
+    /**
+     * Output a note as a standalone text file
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     */
+    public function text($guid)
+    {
+        $note = $this->getNotes()->load($guid, false);
+        if ($note === null) {
+            $res = new ErrorResponse('Note does not exist');
+            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
+            return $res;
+        }
+
+        $converter = new \OCA\Grauphel\Converter\ReStructuredText();
+        $converter->internalLinkHandler = array($this, 'textNoteLinkHandler');
+        try {
+            $title = htmlspecialchars_decode($note->title, ENT_QUOTES | ENT_HTML5);
+            $text = $title . "\n"
+                . str_repeat('*', strlen($title)) . "\n"
+                . "\n";
+            $text .= $converter->convert($note->{'note-content'});
+            return new \OCA\Grauphel\Response\TextResponse($text);
+        } catch (\OCA\Grauphel\Converter\Exception $e) {
+            $res = new ErrorResponse(
+                'Error converting note to reStructuredText.'
+                . ' Please report a bug to the grauphel developers.'
+            );
+            $res->setStatus(\OCP\AppFramework\Http::STATUS_NOT_FOUND);
+            return $res;
+        }
+    }
+
+    public function textNoteLinkHandler($noteTitle)
+    {
+        return $noteTitle;
+    }
+
     /**
      * Output a note in tomboy XML format
      *