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