]> git.mxchange.org Git - friendica.git/blob - mod/help.php
Continued a bit:
[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                         $path .= argv($x);
37                 }
38                 $title = basename($path);
39                 $filename = $path;
40                 $text = load_doc_file('doc/' . $path . '.md');
41                 $a->page['title'] = t('Help:') . ' ' . str_replace('-', ' ', notags($title));
42         }
43         $home = load_doc_file('doc/Home.md');
44         if (!$text) {
45                 $text = $home;
46                 $filename = "Home";
47                 $a->page['title'] = t('Help');
48         } else {
49                 $a->page['aside'] = Markdown($home);
50         }
51
52         if (!strlen($text)) {
53                 header($_SERVER["SERVER_PROTOCOL"] . ' 404 ' . t('Not Found'));
54                 $tpl = get_markup_template("404.tpl");
55                 return replace_macros($tpl, array(
56                                         '$message' => t('Page not found.')
57                                 ));
58         }
59
60         $html = Markdown($text);
61
62         if ($filename !== "Home") {
63                 // create TOC but not for home
64                 $lines = explode("\n", $html);
65                 $toc="<style>aside ul {padding-left: 1em;}aside h1{font-size:2em}</style><h2>TOC</h2><ul id='toc'>";
66                 $lastlevel=1;
67                 $idnum = array(0,0,0,0,0,0,0);
68                 foreach($lines as &$line){
69                         if (substr($line,0,2)=="<h") {
70                                 $level = substr($line,2,1);
71                                 if ($level!="r") {
72                                         $level = intval($level);
73                                         if ($level<$lastlevel) {
74                                                 for($k=$level;$k<$lastlevel; $k++) $toc.="</ul>";
75                                                 for($k=$level+1;$k<count($idnum);$k++) $idnum[$k]=0;
76                                         }
77                                         if ($level>$lastlevel) $toc.="<ul>";
78                                         $idnum[$level]++;
79                                         $id = implode("_", array_slice($idnum,1,$level));
80                                         $href = App::get_baseurl()."/help/{$filename}#{$id}";
81                                         $toc .= "<li><a href='{$href}'>".strip_tags($line)."</a></li>";
82                                         $line = "<a name='{$id}'></a>".$line;
83                                         $lastlevel = $level;
84                                 }
85                         }
86                 }
87                 for($k=0;$k<$lastlevel; $k++) $toc.="</ul>";
88                 $html = implode("\n",$lines);
89
90                 $a->page['aside'] = $toc.$a->page['aside'];
91         }
92
93         $html = "
94                 <style>
95                 .md_warning {
96                         padding: 1em; border: #ff0000 solid 2px;
97                         background-color: #f9a3a3; color: #ffffff;
98                 }
99                 </style>".$html;
100         return $html;
101
102 }