forked from yakamara/redaxo_yrewrite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocs.php
77 lines (61 loc) · 3.28 KB
/
docs.php
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
<?php
$readme_i18n = rex_path::addon($this->getName()).'/docs/README.'.rex::getUser()->getLanguage().'.md';
if(rex::getUser()->getLanguage() && file_exists($readme_i18n)) {
$readme = file_get_contents($readme_i18n);
} else {
$readme = file_get_contents(rex_path::addon($this->getName())."README.md");
}
$readme = preg_replace('/<a name=".*"\>\<\/a\>/', '', $readme); // manuelle Navigations-Anker entfernen
$readme = preg_replace('/http.*\/assets\//', '/assets/addons/'.$this->getName().'/', $readme); // Bilder lokal laden
$h2_chapter = explode("\n# ", "\n".$readme);
$readme_chapters = [];
$docs_chapter_active = rex_request('docs_chapter_active', 'string', false);
foreach ($h2_chapter as $h2_index => $h2_content) {
preg_match('/(.*)\n^(?:.|\n(?!#))*/m', $h2_content, $headline);
$h2_index = rex_string::normalize($headline[0]);
preg_match_all('/(?!## )*^## (.*)\n((?:.|\n(?!## ))*)/m', $h2_content, $matches);
if (isset($headline[1]) && count(array_filter($matches))) {
if($docs_chapter_active && $docs_chapter_active == $h2_index) {
$class = "panel-primary";
$navi_list[] = '<div class="panel '.$class.'"><div class="panel-heading"><strong>'.$headline[0].'</strong></div><div class="list-group">';
} else {
$class = "panel-default";
$navi_list[] = '<div class="panel '.$class.'"><div class="panel-heading"><a class="" href="index.php?page='.$this->getName().'/docs&docs_chapter_active='.$h2_index.'"><strong>'.$headline[0].'</strong></a></div><div class="list-group">';
}
$readme_h2_content = '# '.$headline[0];
$navi_elements = $matches[1];
$blocks = $matches[2];
$readme_chapters[$h2_index] = $readme_h2_content;
foreach ($navi_elements as $h3_index => $navi_element) {
$navi_list[] = '<a class="list-group-item" href="index.php?page='.$this->getName().'/docs&docs_chapter_active='.$h2_index.'#'.rex_string::normalize($navi_element).'">'.$navi_element.'</a>';
$readme_chapters[$h2_index] .= "".'<a id="'.rex_string::normalize($navi_element).'"></a>'."\n## ".$navi_element.$blocks[$h3_index]."\n";
}
$navi_list[] = '</div></div>';
}
}
reset($readme_chapters);
$docs_chapter_active = rex_request('docs_chapter_active', 'string', key($readme_chapters));
if (!isset($readme_chapters[$docs_chapter_active])) {
$docs_chapter_active = key($readme_chapters);
}
$navi_view = implode("\n", $navi_list);
$blocks_view = $readme_chapters[$docs_chapter_active];
$miu = rex_markdown::factory();
// Navigation
$blocks_view = $miu->parse($blocks_view);
$fragment = new rex_fragment();
$fragment->setVar('title', $this->i18n('docs').' [ <a target="_blank" href="'.$this->getProperty('docspage').'">bearbeiten</a> ]', false);
$fragment->setVar('body', $blocks_view, false);
$content = $fragment->parse('core/page/section.php');
$name = $this->getPackageId();
$version = $this->getVersion();
$author = $this->getAuthor();
$navi_view .= 'Credits: ' . $author;
echo '<section class="rex-docs">
<div class="row">
<div class="col-md-4 docs-nav">'.$navi_view.'</div>
<div class="col-md-8 docs-content">'.$content.'</div>
</div>
</section>';
// Dirty image overflow fix
echo '<style> .rex-docs img { max-width: 100%; } </style>';