3 * @copyright Copyright (C) 2010-2023, 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;
31 * Shows the friendica help based on the /doc/ directory
33 class Help extends BaseModule
35 protected function content(array $request = []): string
37 Nav::setSelected('help');
42 $config = DI::config();
43 $lang = DI::session()->get('language', $config->get('system', 'language'));
45 // @TODO: Replace with parameter from router
46 if (DI::args()->getArgc() > 1) {
48 // looping through the argv keys bigger than 0 to build
49 // a path relative to /help
50 for ($x = 1; $x < DI::args()->getArgc(); $x ++) {
55 $path .= DI::args()->get($x);
57 $title = basename($path);
59 $text = self::loadDocFile('doc/' . $path . '.md', $lang);
60 DI::page()['title'] = DI::l10n()->t('Help:') . ' ' . str_replace('-', ' ', $title);
63 $home = self::loadDocFile('doc/Home.md', $lang);
67 DI::page()['title'] = DI::l10n()->t('Help');
69 DI::page()['aside'] = Markdown::convert($home, false);
73 throw new HTTPException\NotFoundException();
76 $html = Markdown::convert($text, false);
78 if ($filename !== "Home") {
79 // create TOC but not for home
80 $lines = explode("\n", $html);
81 $toc = "<h2>TOC</h2><ul id='toc'>";
83 $idNum = [0, 0, 0, 0, 0, 0, 0];
84 foreach ($lines as &$line) {
86 if (preg_match('#<h([1-6])>([^<]+?)</h\1>#i', $line, $matches)) {
88 $anchor = urlencode($matches[2]);
89 if ($level < $lastLevel) {
90 for ($k = $level; $k < $lastLevel; $k++) {
94 for ($k = $level + 1; $k < count($idNum); $k++) {
99 if ($level > $lastLevel) {
105 $href = DI::baseUrl()->get() . "/help/{$filename}#{$anchor}";
106 $toc .= "<li><a href=\"{$href}\">" . strip_tags($line) . "</a></li>";
107 $id = implode("_", array_slice($idNum, 1, $level));
108 $line = "<a name=\"{$id}\"></a>" . $line;
109 $line = "<a name=\"{$anchor}\"></a>" . $line;
115 for ($k = 0; $k < $lastLevel; $k++) {
119 $html = implode("\n", $lines);
121 DI::page()['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . DI::page()['aside'] . '</div>';
127 private static function loadDocFile($fileName, $lang = 'en')
129 $baseName = basename($fileName);
130 $dirName = dirname($fileName);
131 if (file_exists("$dirName/$lang/$baseName")) {
132 return file_get_contents("$dirName/$lang/$baseName");
135 if (file_exists($fileName)) {
136 return file_get_contents($fileName);