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