3 namespace Friendica\Module;
5 use Friendica\BaseModule;
6 use Friendica\Content\Nav;
7 use Friendica\Content\Text\Markdown;
9 use Friendica\Network\HTTPException;
10 use Friendica\Util\Strings;
13 * Shows the friendica help based on the /doc/ directory
15 class Help extends BaseModule
17 public static function content(array $parameters = [])
19 Nav::setSelected('help');
25 $config = DI::config();
26 $lang = $config->get('system', 'language');
28 // @TODO: Replace with parameter from router
31 // looping through the argv keys bigger than 0 to build
32 // a path relative to /help
33 for ($x = 1; $x < $a->argc; $x ++) {
38 $path .= DI::args()->get($x);
40 $title = basename($path);
42 $text = self::loadDocFile('doc/' . $path . '.md', $lang);
43 DI::page()['title'] = DI::l10n()->t('Help:') . ' ' . str_replace('-', ' ', Strings::escapeTags($title));
46 $home = self::loadDocFile('doc/Home.md', $lang);
50 DI::page()['title'] = DI::l10n()->t('Help');
52 DI::page()['aside'] = Markdown::convert($home, false);
56 throw new HTTPException\NotFoundException();
59 $html = Markdown::convert($text, false);
61 if ($filename !== "Home") {
62 // create TOC but not for home
63 $lines = explode("\n", $html);
64 $toc = "<h2>TOC</h2><ul id='toc'>";
66 $idNum = [0, 0, 0, 0, 0, 0, 0];
67 foreach ($lines as &$line) {
69 if (preg_match('#<h([1-6])>([^<]+?)</h\1>#i', $line, $matches)) {
71 $anchor = urlencode($matches[2]);
72 if ($level < $lastLevel) {
73 for ($k = $level; $k < $lastLevel; $k++) {
77 for ($k = $level + 1; $k < count($idNum); $k++) {
82 if ($level > $lastLevel) {
88 $href = DI::baseUrl()->get() . "/help/{$filename}#{$anchor}";
89 $toc .= "<li><a href=\"{$href}\">" . strip_tags($line) . "</a></li>";
90 $id = implode("_", array_slice($idNum, 1, $level));
91 $line = "<a name=\"{$id}\"></a>" . $line;
92 $line = "<a name=\"{$anchor}\"></a>" . $line;
98 for ($k = 0; $k < $lastLevel; $k++) {
102 $html = implode("\n", $lines);
104 DI::page()['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . DI::page()['aside'] . '</div>';
110 private static function loadDocFile($fileName, $lang = 'en')
112 $baseName = basename($fileName);
113 $dirName = dirname($fileName);
114 if (file_exists("$dirName/$lang/$baseName")) {
115 return file_get_contents("$dirName/$lang/$baseName");
118 if (file_exists($fileName)) {
119 return file_get_contents($fileName);