]> git.mxchange.org Git - friendica.git/blob - mod/help.php
320e622fa51c06674787d54e7c4d3513f8b9467e
[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 if(! function_exists('help_content')) {
22 function help_content(&$a) {
23
24         nav_set_selected('help');
25
26         global $lang;
27
28         $text = '';
29
30         if ($a->argc > 1) {
31                 $path = '';
32                 // looping through the argv keys bigger than 0 to build
33                 // a path relative to /help
34                 for($x = 1; $x < argc(); $x ++) {
35                         if(strlen($path))
36                                 $path .= '/';
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;}</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++) $toc.="</ul>";
76                                                 for($k=$level+1;$k<count($idnum);$k++) $idnum[$k]=0;
77                                         }
78                                         if ($level>$lastlevel) $toc.="<ul>";
79                                         $idnum[$level]++;
80                                         $id = implode("_", array_slice($idnum,1,$level));
81                                         $href = $a->get_baseurl()."/help/{$filename}#{$id}";
82                                         $toc .= "<li><a href='{$href}'>".strip_tags($line)."</a></li>";
83                                         $line = "<a name='{$id}'></a>".$line;
84                                         $lastlevel = $level;
85                                 }
86                         }
87                 }
88                 for($k=1;$k<$lastlevel; $k++) $toc.="</ul>";
89                 $html = implode("\n",$lines);
90
91                 $a->page['aside'] = $toc.$a->page['aside'];
92         }
93
94         $html = "
95                 <style>
96                 .md_warning {
97                         padding: 1em; border: #ff0000 solid 2px;
98                         background-color: #f9a3a3; color: #ffffff;
99                 }
100                 </style>".$html;
101         return $html;
102 }
103 }