summaryrefslogtreecommitdiff
path: root/app/Controller/DocumentationController.php
diff options
context:
space:
mode:
Diffstat (limited to 'app/Controller/DocumentationController.php')
-rw-r--r--app/Controller/DocumentationController.php92
1 files changed, 92 insertions, 0 deletions
diff --git a/app/Controller/DocumentationController.php b/app/Controller/DocumentationController.php
new file mode 100644
index 00000000..d86fb3c8
--- /dev/null
+++ b/app/Controller/DocumentationController.php
@@ -0,0 +1,92 @@
+<?php
+
+namespace Kanboard\Controller;
+
+use Parsedown;
+
+/**
+ * Documentation Viewer
+ *
+ * @package Kanboard\Controller
+ * @author Frederic Guillot
+ */
+class DocumentationController extends BaseController
+{
+ public function show()
+ {
+ $page = $this->request->getStringParam('file', 'index');
+
+ if (!preg_match('/^[a-z0-9\-]+/', $page)) {
+ $page = 'index';
+ }
+
+ if ($this->languageModel->getCurrentLanguage() === 'fr_FR') {
+ $filename = __DIR__.'/../../doc/fr/' . $page . '.markdown';
+ } else {
+ $filename = __DIR__ . '/../../doc/' . $page . '.markdown';
+ }
+
+ if (!file_exists($filename)) {
+ $filename = __DIR__.'/../../doc/index.markdown';
+ }
+
+ $this->response->html($this->helper->layout->app('doc/show', $this->render($filename)));
+ }
+
+ /**
+ * Display keyboard shortcut
+ */
+ public function shortcuts()
+ {
+ $this->response->html($this->template->render('config/keyboard_shortcuts'));
+ }
+
+ /**
+ * Prepare Markdown file
+ *
+ * @access private
+ * @param string $filename
+ * @return array
+ */
+ private function render($filename)
+ {
+ $data = file_get_contents($filename);
+ $content = preg_replace_callback('/\((.*.markdown)\)/', array($this, 'replaceMarkdownUrl'), $data);
+ $content = preg_replace_callback('/\((screenshots.*\.png)\)/', array($this, 'replaceImageUrl'), $content);
+
+ list($title, ) = explode("\n", $data, 2);
+
+ return array(
+ 'content' => Parsedown::instance()->text($content),
+ 'title' => $title !== 'Documentation' ? t('Documentation: %s', $title) : $title,
+ );
+ }
+
+ /**
+ * Regex callback to replace Markdown links
+ *
+ * @access public
+ * @param array $matches
+ * @return string
+ */
+ public function replaceMarkdownUrl(array $matches)
+ {
+ return '('.$this->helper->url->to('DocumentationController', 'show', array('file' => str_replace('.markdown', '', $matches[1]))).')';
+ }
+
+ /**
+ * Regex callback to replace image links
+ *
+ * @access public
+ * @param array $matches
+ * @return string
+ */
+ public function replaceImageUrl(array $matches)
+ {
+ if ($this->languageModel->getCurrentLanguage() === 'fr_FR') {
+ return '('.$this->helper->url->base().'doc/fr/'.$matches[1].')';
+ }
+
+ return '('.$this->helper->url->base().'doc/'.$matches[1].')';
+ }
+}