3 namespace Friendica\Module;
5 use Friendica\BaseModule;
6 use Friendica\Content\Nav;
7 use Friendica\Content\Text\Markdown;
8 use Friendica\Core\L10n;
10 use Friendica\Network\HTTPException;
11 use Friendica\Util\Strings;
14 * Shows the friendica help based on the /doc/ directory
16 class Help extends BaseModule
18 public static function content(array $parameters = [])
20 Nav::setSelected('help');
26 $config = DI::config();
27 $lang = $config->get('system', 'language');
29 // @TODO: Replace with parameter from router
32 // looping through the argv keys bigger than 0 to build
33 // a path relative to /help
34 for ($x = 1; $x < $a->argc; $x ++) {
39 $path .= DI::args()->get($x);
41 $title = basename($path);
43 $text = self::loadDocFile('doc/' . $path . '.md', $lang);
44 $a->page['title'] = L10n::t('Help:') . ' ' . str_replace('-', ' ', Strings::escapeTags($title));
47 $home = self::loadDocFile('doc/Home.md', $lang);
51 $a->page['title'] = L10n::t('Help');
53 $a->page['aside'] = Markdown::convert($home, false);
57 throw new HTTPException\NotFoundException();
60 $html = Markdown::convert($text, false);
62 if ($filename !== "Home") {
63 // create TOC but not for home
64 $lines = explode("\n", $html);
65 $toc = "<h2>TOC</h2><ul id='toc'>";
67 $idNum = [0, 0, 0, 0, 0, 0, 0];
68 foreach ($lines as &$line) {
70 foreach ($lines as &$line) {
71 if (preg_match('#<h([1-6])>([^<]+?)</h\1>#i', $line, $matches)) {
73 $anchor = urlencode($matches[2]);
74 if ($level < $lastLevel) {
75 for ($k = $level; $k < $lastLevel; $k++) {
79 for ($k = $level + 1; $k < count($idNum); $k++) {
84 if ($level > $lastLevel) {
90 $href = DI::baseUrl()->get() . "/help/{$filename}#{$anchor}";
91 $toc .= "<li><a href=\"{$href}\">" . strip_tags($line) . "</a></li>";
92 $id = implode("_", array_slice($idNum, 1, $level));
93 $line = "<a name=\"{$id}\"></a>" . $line;
94 $line = "<a name=\"{$anchor}\"></a>" . $line;
101 for ($k = 0; $k < $lastLevel; $k++) {
105 $html = implode("\n", $lines);
107 $a->page['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . $a->page['aside'] . '</div>';
113 private static function loadDocFile($fileName, $lang = 'en')
115 $baseName = basename($fileName);
116 $dirName = dirname($fileName);
117 if (file_exists("$dirName/$lang/$baseName")) {
118 return file_get_contents("$dirName/$lang/$baseName");
121 if (file_exists($fileName)) {
122 return file_get_contents($fileName);