summaryrefslogtreecommitdiff
path: root/app/Controller/Doc.php
blob: 6f309d48cf7d640bdf5cc5245fef301086f22f61 (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
<?php

namespace Kanboard\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, ) = explode("\n", $data, 2);

        $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()
    {
        $page = $this->request->getStringParam('file', 'index');

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

        $filenames = array(__DIR__.'/../../doc/'.$page.'.markdown');
        $filename = __DIR__.'/../../doc/index.markdown';

        if ($this->config->getCurrentLanguage() === 'fr_FR') {
            array_unshift($filenames, __DIR__.'/../../doc/fr/'.$page.'.markdown');
        }

        foreach ($filenames as $file) {
            if (file_exists($file)) {
                $filename = $file;
                break;
            }
        }

        $this->response->html($this->helper->layout->app('doc/show', $this->readFile($filename)));
    }
}