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