]> git.mxchange.org Git - friendica.git/blob - mod/help.php
Merge pull request #4252 from zeroadam/Nav-#3878
[friendica.git] / mod / help.php
1 <?php
2 /**
3  * @file mod/help.php
4  */
5 use Friendica\App;
6 use Friendica\Content\Nav;
7 use Friendica\Content\Text\Markdown;
8 use Friendica\Core\System;
9
10 if (!function_exists('load_doc_file')) {
11
12         function load_doc_file($s) {
13                 global $lang;
14                 if (!isset($lang))
15                         $lang = 'en';
16                 $b = basename($s);
17                 $d = dirname($s);
18                 if (file_exists("$d/$lang/$b"))
19                         return file_get_contents("$d/$lang/$b");
20                 if (file_exists($s))
21                         return file_get_contents($s);
22                 return '';
23         }
24
25 }
26
27 function help_content(App $a) {
28
29         Nav::setSelected('help');
30
31         global $lang;
32
33         $text = '';
34
35         if ($a->argc > 1) {
36                 $path = '';
37                 // looping through the argv keys bigger than 0 to build
38                 // a path relative to /help
39                 for($x = 1; $x < argc(); $x ++) {
40                         if(strlen($path))
41                                 $path .= '/';
42                         $path .= argv($x);
43                 }
44                 $title = basename($path);
45                 $filename = $path;
46                 $text = load_doc_file('doc/' . $path . '.md');
47                 $a->page['title'] = t('Help:') . ' ' . str_replace('-', ' ', notags($title));
48         }
49         $home = load_doc_file('doc/Home.md');
50         if (!$text) {
51                 $text = $home;
52                 $filename = "Home";
53                 $a->page['title'] = t('Help');
54         } else {
55                 $a->page['aside'] = Markdown::convert($home, false);
56         }
57
58         if (!strlen($text)) {
59                 header($_SERVER["SERVER_PROTOCOL"] . ' 404 ' . t('Not Found'));
60                 $tpl = get_markup_template("404.tpl");
61                 return replace_macros($tpl, [
62                                         '$message' => t('Page not found.')
63                                 ]);
64         }
65
66         $html = Markdown::convert($text, false);
67
68         if ($filename !== "Home") {
69                 // create TOC but not for home
70                 $lines = explode("\n", $html);
71                 $toc="<style>aside ul {padding-left: 1em;}aside h1{font-size:2em}</style><h2>TOC</h2><ul id='toc'>";
72                 $lastlevel=1;
73                 $idnum = [0,0,0,0,0,0,0];
74                 foreach($lines as &$line){
75                         if (substr($line,0,2)=="<h") {
76                                 $level = substr($line,2,1);
77                                 if ($level!="r") {
78                                         $level = intval($level);
79                                         if ($level<$lastlevel) {
80                                                 for($k=$level;$k<$lastlevel; $k++) $toc.="</ul>";
81                                                 for($k=$level+1;$k<count($idnum);$k++) $idnum[$k]=0;
82                                         }
83                                         if ($level>$lastlevel) $toc.="<ul>";
84                                         $idnum[$level]++;
85                                         $id = implode("_", array_slice($idnum,1,$level));
86                                         $href = System::baseUrl()."/help/{$filename}#{$id}";
87                                         $toc .= "<li><a href='{$href}'>".strip_tags($line)."</a></li>";
88                                         $line = "<a name='{$id}'></a>".$line;
89                                         $lastlevel = $level;
90                                 }
91                         }
92                 }
93                 for($k=0;$k<$lastlevel; $k++) $toc.="</ul>";
94                 $html = implode("\n",$lines);
95
96                 $a->page['aside'] = '<section class="help-aside-wrapper">' . $toc . $a->page['aside'] . '</section>';
97         }
98
99         $html = "
100                 <style>
101                 .md_warning {
102                         padding: 1em; border: #ff0000 solid 2px;
103                         background-color: #f9a3a3; color: #ffffff;
104                 }
105                 </style>".$html;
106         return $html;
107
108 }