]> git.mxchange.org Git - friendica.git/blob - mod/help.php
Move mod/home to src/Module/Home
[friendica.git] / mod / help.php
1 <?php
2 /**
3  * @file mod/help.php
4  */
5
6 use Friendica\App;
7 use Friendica\Content\Nav;
8 use Friendica\Content\Text\Markdown;
9 use Friendica\Core\Config;
10 use Friendica\Core\L10n;
11 use Friendica\Core\Renderer;
12 use Friendica\Core\System;
13 use Friendica\Util\Strings;
14
15 function load_doc_file($s)
16 {
17         $lang = Config::get('system', 'language');
18         $b = basename($s);
19         $d = dirname($s);
20         if (file_exists("$d/$lang/$b")) {
21                 return file_get_contents("$d/$lang/$b");
22         }
23
24         if (file_exists($s)) {
25                 return file_get_contents($s);
26         }
27
28         return '';
29 }
30
31 function help_content(App $a)
32 {
33         Nav::setSelected('help');
34
35         $text = '';
36         $filename = '';
37
38         if ($a->argc > 1) {
39                 $path = '';
40                 // looping through the argv keys bigger than 0 to build
41                 // a path relative to /help
42                 for ($x = 1; $x < $a->argc; $x ++) {
43                         if (strlen($path)) {
44                                 $path .= '/';
45                         }
46
47                         $path .= $a->getArgumentValue($x);
48                 }
49                 $title = basename($path);
50                 $filename = $path;
51                 $text = load_doc_file('doc/' . $path . '.md');
52                 $a->page['title'] = L10n::t('Help:') . ' ' . str_replace('-', ' ', Strings::escapeTags($title));
53         }
54
55         $home = load_doc_file('doc/Home.md');
56         if (!$text) {
57                 $text = $home;
58                 $filename = "Home";
59                 $a->page['title'] = L10n::t('Help');
60         } else {
61                 $a->page['aside'] = Markdown::convert($home, false);
62         }
63
64         if (!strlen($text)) {
65                 throw new \Friendica\Network\HTTPException\NotFoundException();
66         }
67
68         $html = Markdown::convert($text, false);
69
70         if ($filename !== "Home") {
71                 // create TOC but not for home
72                 $lines = explode("\n", $html);
73                 $toc = "<h2>TOC</h2><ul id='toc'>";
74                 $lastlevel = 1;
75                 $idnum = [0, 0, 0, 0, 0, 0, 0];
76                 foreach ($lines as &$line) {
77                         if (substr($line, 0, 2) == "<h") {
78                                 $level = substr($line, 2, 1);
79                                 if ($level != "r") {
80                                         $level = intval($level);
81                                         if ($level < $lastlevel) {
82                                                 for ($k = $level; $k < $lastlevel; $k++) {
83                                                         $toc .= "</ul></li>";
84                                                 }
85
86                                                 for ($k = $level + 1; $k < count($idnum); $k++) {
87                                                         $idnum[$k] = 0;
88                                                 }
89                                         }
90
91                                         if ($level > $lastlevel) {
92                                                 $toc .= "<li><ul>";
93                                         }
94
95                                         $idnum[$level] ++;
96                                         $id = implode("_", array_slice($idnum, 1, $level));
97                                         $href = System::baseUrl() . "/help/{$filename}#{$id}";
98                                         $toc .= "<li><a href='{$href}'>" . strip_tags($line) . "</a></li>";
99                                         $line = "<a name='{$id}'></a>" . $line;
100                                         $lastlevel = $level;
101                                 }
102                         }
103                 }
104
105                 for ($k = 0; $k < $lastlevel; $k++) {
106                         $toc .= "</ul>";
107                 }
108
109                 $html = implode("\n", $lines);
110
111                 $a->page['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . $a->page['aside'] . '</div>';
112         }
113
114         return $html;
115 }