Repository navigation
Expand file tree
/
Copy pathdocs.php
More file actions
115 lines (104 loc) · 4.9 KB
/
Copy pathdocs.php
File metadata and controls
115 lines (104 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* Render the bundled user documentation (docs/userdocs.md) in the browser.
*
* @package report_sql
* @copyright 2026 Marcus Green
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/adminlib.php');
$context = context_system::instance();
// Registered in the admin tree (settings.php) under the SQL Report category, gated by report/sql:author,
// so only authors get the page and the primary/secondary navigation and breadcrumb are set.
admin_externalpage_setup('report_sql_docs', '', null, new moodle_url('/report/sql/docs.php'));
$PAGE->add_body_class('limitedwidth');
$PAGE->set_title(get_string('userdocs', 'report_sql'));
$PAGE->set_heading(get_string('reportsources', 'report_sql'));
$markdown = file_get_contents(__DIR__ . '/docs/userdocs.md');
// The markdown references images by bare filename (e.g. logo.png); they live in docs/. Rewrite both
// markdown image syntax  and raw <img src="file.png"> to absolute plugin URLs so they
// resolve once the doc is served from docs.php rather than the docs/ directory.
$docsbase = (new moodle_url('/report/sql/docs/'))->out(false);
$markdown = preg_replace_callback(
'/!\[([^\]]*)\]\((?!https?:\/\/)([^)]+)\)/',
function ($m) use ($docsbase) {
return '![' . $m[1] . '](' . $docsbase . ltrim($m[2], '/') . ')';
},
$markdown
);
$markdown = preg_replace_callback(
'/<img\b([^>]*?)\bsrc="(?!https?:\/\/)([^"]+)"/i',
function ($m) use ($docsbase) {
return '<img' . $m[1] . 'src="' . $docsbase . ltrim($m[2], '/') . '"';
},
$markdown
);
echo $OUTPUT->header();
$html = format_text($markdown, FORMAT_MARKDOWN, ['context' => $context, 'noclean' => true]);
// Moodle's markdown renderer emits headings without id attributes, so the in-page table-of-contents
// anchors (GitHub-style #slug links) have nothing to jump to. Inject a GitHub-compatible slug id on
// every heading so the TOC and cross-references work. GitHub's slugger: lowercase, drop anything that
// is not a letter/number/space/hyphen, then spaces -> hyphens; duplicate slugs get a -1, -2 … suffix.
$seen = [];
$slugger = function (string $text) use (&$seen): string {
$text = html_entity_decode(strip_tags($text), ENT_QUOTES, 'UTF-8');
$slug = strtolower(trim($text));
$slug = preg_replace('/[^\p{L}\p{N} \-]+/u', '', $slug); // Drop punctuation (keep spaces/hyphens).
$slug = str_replace(' ', '-', $slug);
$base = $slug;
$i = 0;
while (isset($seen[$slug])) {
$slug = $base . '-' . (++$i);
}
$seen[$slug] = true;
return $slug;
};
$html = preg_replace_callback(
'/<h([1-6])>(.*?)<\/h\1>/s',
function ($m) use ($slugger) {
return '<h' . $m[1] . ' id="' . s($slugger($m[2])) . '">' . $m[2] . '</h' . $m[1] . '>';
},
$html
);
// Lift the "Contents" list out of the flow. It is output first so on small and medium screens it sits
// at the top of the content, and on large screens (see styles.css) it floats in the left margin outside
// the content box without narrowing it (the list is rendered twice, once per breakpoint).
$toc = '';
if (preg_match('/<h2 id="contents">.*?<\/ol>/s', $html, $m)) {
$toc = $m[0];
$html = str_replace($m[0], '', $html);
// The "---" rules on either side of the list would leave a double divider behind.
$html = preg_replace('/(<hr\s*\/?>\s*){2,}/', '<hr />', $html, 1);
}
// Below xxl the list is plain content at the top of the box. From xxl up it floats in the left page margin
// (as in mod_cms's type edit page): absolutely positioned outside a relative wrapper, sticky within it.
$floating = '';
if ($toc !== '') {
$floating = html_writer::tag(
'nav',
html_writer::div($toc, 'position-sticky overflow-auto', ['style' => 'top: 9rem; max-height: calc(100vh - 10rem);']),
[
'class' => 'rs-docs-toc d-none d-xxl-block position-absolute top-0 bottom-0 end-100 me-3',
'style' => 'width: 16rem;',
'aria-label' => get_string('userdocs', 'report_sql'),
]
);
$toc = html_writer::div($toc, 'd-xxl-none mb-4');
}
echo html_writer::div($floating . $OUTPUT->box($toc . $html, 'generalbox rs-docs'), 'position-relative');
echo $OUTPUT->footer();