summaryrefslogtreecommitdiff
path: root/app/Controller/DocumentationController.php
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2016-05-28 11:31:54 -0400
committerFrederic Guillot <fred@kanboard.net>2016-05-28 11:31:54 -0400
commitab48a09f0d674b703467975b376c5ac7352670ae (patch)
tree99fbb0cf719acee8c500622d498bd7f6375b62a1 /app/Controller/DocumentationController.php
parent82b5b491bec94cb3d40a5820fbef9959435309be (diff)
Rename controllers
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..379e3dab
--- /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->language->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->language->getCurrentLanguage() === 'fr_FR') {
+ return '('.$this->helper->url->base().'doc/fr/'.$matches[1].')';
+ }
+
+ return '('.$this->helper->url->base().'doc/'.$matches[1].')';
+ }
+}