From 6c711f696f73bc59813f6834ec241aef3b626cbd Mon Sep 17 00:00:00 2001
From: Frederic Guillot <fred@kanboard.net>
Date: Sat, 29 Aug 2015 20:00:53 -0400
Subject: Include documentation in the application

---
 app/Controller/Doc.php | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 app/Controller/Doc.php

(limited to 'app/Controller')

diff --git a/app/Controller/Doc.php b/app/Controller/Doc.php
new file mode 100644
index 00000000..19644b84
--- /dev/null
+++ b/app/Controller/Doc.php
@@ -0,0 +1,51 @@
+<?php
+
+namespace Controller;
+
+use Parsedown;
+
+/**
+ * Documentation controller
+ *
+ * @package  controller
+ * @author   Frederic Guillot
+ */
+class Doc extends Base
+{
+    private function readFile($filename)
+    {
+        $url = $this->helper->url;
+        $data = file_get_contents($filename);
+        list($title,, $content) = explode("\n", $data, 3);
+
+        $replaceUrl = function (array $matches) use ($url) {
+            return '('.$url->to('doc', 'show', array('file' => str_replace('.markdown', '', $matches[1]))).')';
+        };
+
+        $content = preg_replace_callback('/\((.*.markdown)\)/', $replaceUrl, $data);
+
+        return array(
+            'content' => Parsedown::instance()->text($content),
+            'title' => $title !== 'Documentation' ? t('Documentation: %s', $title) : $title,
+        );
+    }
+
+    public function show()
+    {
+        $filename = $this->request->getStringParam('file', 'index');
+
+        if (! preg_match('/^[a-z0-9\-]+/', $filename)) {
+            $filename = 'index';
+        }
+
+        $filename = __DIR__.'/../../doc/'.$filename.'.markdown';
+
+        if (! file_exists($filename)) {
+            $filename = __DIR__.'/../../doc/index.markdown';
+        }
+
+        $this->response->html($this->template->layout('doc/show', $this->readFile($filename) + array(
+            'board_selector' => $this->projectPermission->getAllowedProjects($this->userSession->getId()),
+        )));
+    }
+}
-- 
cgit v1.2.3