summaryrefslogtreecommitdiff
path: root/app/Controller/Doc.php
blob: 9164c6b9d066ce726fb35d40d10c8ca8fca4600d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

namespace Kanboard\Controller;

use Parsedown;

/**
 * Documentation Viewer
 *
 * @package  controller
 * @author   Frederic Guillot
 */
class Doc extends Base
{
    public function show()
    {
        $page = $this->request->getStringParam('file', 'index');

        if (!preg_match('/^[a-z0-9\-]+/', $page)) {
            $page = 'index';
        }

        if ($this->config->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('doc', '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)
    {
        return '('.$this->helper->url->base().'doc/'.$matches[1].')';
    }
}