3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Content\Nav;
26 use Friendica\Content\Text\Markdown;
28 use Friendica\Network\HTTPException;
29 use Friendica\Util\Strings;
32 * Shows the friendica help based on the /doc/ directory
34 class Help extends BaseModule
36 public static function content(array $parameters = [])
38 Nav::setSelected('help');
44 $config = DI::config();
45 $lang = $config->get('system', 'language');
47 // @TODO: Replace with parameter from router
50 // looping through the argv keys bigger than 0 to build
51 // a path relative to /help
52 for ($x = 1; $x < $a->argc; $x ++) {
57 $path .= DI::args()->get($x);
59 $title = basename($path);
61 $text = self::loadDocFile('doc/' . $path . '.md', $lang);
62 DI::page()['title'] = DI::l10n()->t('Help:') . ' ' . str_replace('-', ' ', Strings::escapeTags($title));
65 $home = self::loadDocFile('doc/Home.md', $lang);
69 DI::page()['title'] = DI::l10n()->t('Help');
71 DI::page()['aside'] = Markdown::convert($home, false);
75 throw new HTTPException\NotFoundException();
78 $html = Markdown::convert($text, false);
80 if ($filename !== "Home") {
81 // create TOC but not for home
82 $lines = explode("\n", $html);
83 $toc = "<h2>TOC</h2><ul id='toc'>";
85 $idNum = [0, 0, 0, 0, 0, 0, 0];
86 foreach ($lines as &$line) {
88 if (preg_match('#<h([1-6])>([^<]+?)</h\1>#i', $line, $matches)) {
90 $anchor = urlencode($matches[2]);
91 if ($level < $lastLevel) {
92 for ($k = $level; $k < $lastLevel; $k++) {
96 for ($k = $level + 1; $k < count($idNum); $k++) {
101 if ($level > $lastLevel) {
107 $href = DI::baseUrl()->get() . "/help/{$filename}#{$anchor}";
108 $toc .= "<li><a href=\"{$href}\">" . strip_tags($line) . "</a></li>";
109 $id = implode("_", array_slice($idNum, 1, $level));
110 $line = "<a name=\"{$id}\"></a>" . $line;
111 $line = "<a name=\"{$anchor}\"></a>" . $line;
117 for ($k = 0; $k < $lastLevel; $k++) {
121 $html = implode("\n", $lines);
123 DI::page()['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . DI::page()['aside'] . '</div>';
129 private static function loadDocFile($fileName, $lang = 'en')
131 $baseName = basename($fileName);
132 $dirName = dirname($fileName);
133 if (file_exists("$dirName/$lang/$baseName")) {
134 return file_get_contents("$dirName/$lang/$baseName");
137 if (file_exists($fileName)) {
138 return file_get_contents($fileName);