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;
31 * Shows the friendica help based on the /doc/ directory
33 class Help extends BaseModule
35 public static function content(array $parameters = [])
37 Nav::setSelected('help');
43 $config = DI::config();
44 $lang = $config->get('system', 'language');
46 // @TODO: Replace with parameter from router
47 if (DI::args()->getArgc() > 1) {
49 // looping through the argv keys bigger than 0 to build
50 // a path relative to /help
51 for ($x = 1; $x < DI::args()->getArgc(); $x ++) {
56 $path .= DI::args()->get($x);
58 $title = basename($path);
60 $text = self::loadDocFile('doc/' . $path . '.md', $lang);
61 DI::page()['title'] = DI::l10n()->t('Help:') . ' ' . str_replace('-', ' ', $title);
64 $home = self::loadDocFile('doc/Home.md', $lang);
68 DI::page()['title'] = DI::l10n()->t('Help');
70 DI::page()['aside'] = Markdown::convert($home, false);
74 throw new HTTPException\NotFoundException();
77 $html = Markdown::convert($text, false);
79 if ($filename !== "Home") {
80 // create TOC but not for home
81 $lines = explode("\n", $html);
82 $toc = "<h2>TOC</h2><ul id='toc'>";
84 $idNum = [0, 0, 0, 0, 0, 0, 0];
85 foreach ($lines as &$line) {
87 if (preg_match('#<h([1-6])>([^<]+?)</h\1>#i', $line, $matches)) {
89 $anchor = urlencode($matches[2]);
90 if ($level < $lastLevel) {
91 for ($k = $level; $k < $lastLevel; $k++) {
95 for ($k = $level + 1; $k < count($idNum); $k++) {
100 if ($level > $lastLevel) {
106 $href = DI::baseUrl()->get() . "/help/{$filename}#{$anchor}";
107 $toc .= "<li><a href=\"{$href}\">" . strip_tags($line) . "</a></li>";
108 $id = implode("_", array_slice($idNum, 1, $level));
109 $line = "<a name=\"{$id}\"></a>" . $line;
110 $line = "<a name=\"{$anchor}\"></a>" . $line;
116 for ($k = 0; $k < $lastLevel; $k++) {
120 $html = implode("\n", $lines);
122 DI::page()['aside'] = '<div class="help-aside-wrapper widget"><div id="toc-wrapper">' . $toc . '</div>' . DI::page()['aside'] . '</div>';
128 private static function loadDocFile($fileName, $lang = 'en')
130 $baseName = basename($fileName);
131 $dirName = dirname($fileName);
132 if (file_exists("$dirName/$lang/$baseName")) {
133 return file_get_contents("$dirName/$lang/$baseName");
136 if (file_exists($fileName)) {
137 return file_get_contents($fileName);