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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
<?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';
}
$filename = $this->getPageFilename($page);
$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);
$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)
{
return '('.$this->getFileBaseUrl($matches[1]).')';
}
/**
* Get Markdown file according to the current language
*
* @access private
* @param string $page
* @return string
*/
private function getPageFilename($page)
{
return $this->getFileLocation($page . '.markdown') ?:
implode(DIRECTORY_SEPARATOR, array(ROOT_DIR, 'doc', 'en_US', 'index.markdown'));
}
/**
* Get base URL for Markdown links
*
* @access private
* @param string $filename
* @return string
*/
private function getFileBaseUrl($filename)
{
$language = $this->languageModel->getCurrentLanguage();
$path = $this->getFileLocation($filename);
if (strpos($path, $language) !== false) {
$url = implode('/', array('doc', $language, $filename));
} else {
$url = implode('/', array('doc', $filename));
}
return $this->helper->url->base().$url;
}
/**
* Get file location according to the current language
*
* @access private
* @param string $filename
* @return string
*/
private function getFileLocation($filename)
{
$files = array(
implode(DIRECTORY_SEPARATOR, array(ROOT_DIR, 'doc', $this->languageModel->getCurrentLanguage(), $filename)),
implode(DIRECTORY_SEPARATOR, array(ROOT_DIR, 'doc', 'en_US', $filename)),
);
foreach ($files as $filename) {
if (file_exists($filename)) {
return $filename;
}
}
return '';
}
}
|