aboutsummaryrefslogtreecommitdiff
path: root/app/class
diff options
context:
space:
mode:
authorvincent-peugnet <v.peugnet@free.fr>2020-04-09 00:38:20 +0200
committervincent-peugnet <v.peugnet@free.fr>2020-04-09 00:38:20 +0200
commit1270f3300998afbc5defab832e02cc831f486d9f (patch)
tree1710425ba321873785a7643f722332bb6bed5296 /app/class
parente91dfdbc7549b60a6bc2decff885a26f7fbb365f (diff)
parentf718dc515b3d617e24578dfa875ae786e93f8295 (diff)
downloadwcms-1270f3300998afbc5defab832e02cc831f486d9f.tar.gz
wcms-1270f3300998afbc5defab832e02cc831f486d9f.zip
Merge branch 'master' of https://github.com/vincent-peugnet/wcms
Diffstat (limited to 'app/class')
-rw-r--r--app/class/Header.php24
-rw-r--r--app/class/Modelrender.php20
-rw-r--r--app/class/Summary.php62
3 files changed, 60 insertions, 46 deletions
diff --git a/app/class/Header.php b/app/class/Header.php
new file mode 100644
index 0000000..0165e59
--- /dev/null
+++ b/app/class/Header.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Wcms;
+
+/**
+ * Simple "c struct like" class that represent a Header's datas.
+ * All members are public because there is no logic in this class. It is only
+ * used to pass data from one element to another with cleanly typed members.
+ */
+class Header
+{
+ /** @var string $id the id of this header. */
+ public $id;
+ /** @var int $level the level of deepness of this header. */
+ public $level;
+ /** @var string $title the title displayed by this header. */
+ public $title;
+
+ public function __construct(string $id, int $level, string $title) {
+ $this->id = $id;
+ $this->level = $level;
+ $this->title = $title;
+ }
+}
diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php
index 29a3fa7..091179d 100644
--- a/app/class/Modelrender.php
+++ b/app/class/Modelrender.php
@@ -437,17 +437,23 @@ class Modelrender extends Modelpage
$max = 6;
}
- $sum = [];
$text = preg_replace_callback(
- '/<h([' . $min . '-' . $max . '])(\s+(\s*\w+="\w+")*)?\s*>(.+)<\/h[' . $min . '-' . $max . ']>/mU',
- function ($matches) use (&$sum) {
- $cleanid = idclean($matches[4]);
- $sum[$cleanid][$matches[1]] = $matches[4];
- return '<h' . $matches[1] . $matches[2] . ' id="' . $cleanid . '">' . $matches[4] . '</h' . $matches[1] . '>';
+ "/<h([$min-$max])((.*)id=\"([^\"]*)\"(.*)|.*)>(.+)<\/h[$min-$max]>/mU",
+ function ($matches) {
+ $level = $matches[1];
+ $beforeid = $matches[3];
+ $id = $matches[4];
+ $afterid = $matches[5];
+ $content = $matches[6];
+ // if no custom id is defined, use idclean of the content as id
+ if (empty($id)) {
+ $id = idclean($content);
+ }
+ $this->sum[] = new Header($id, intval($level), $content);
+ return "<h$level $beforeid id=\"$id\" $afterid>$content</h$level>";
},
$text
);
- $this->sum[$element] = $sum;
return $text;
}
diff --git a/app/class/Summary.php b/app/class/Summary.php
index 373fb33..b8b17e9 100644
--- a/app/class/Summary.php
+++ b/app/class/Summary.php
@@ -16,7 +16,7 @@ class Summary extends Item
/** @var int Maximum summary level*/
protected $max = 6;
- /** @var array Headers datas */
+ /** @var Header[] Headers datas */
protected $sum = [];
/** @var string Name of element to display */
@@ -48,46 +48,30 @@ class Summary extends Item
public function sumparser()
{
$sumstring = '';
-
-
- foreach ($this->sum as $type => $element) {
- if(!empty($element) && (empty($this->element) || $type === $this->element)) {
-
- $filteredsum = [];
-
- foreach ($element as $key => $menu) {
- $deepness = array_keys($menu)[0];
- if($deepness >= $this->min && $deepness <= $this->max) {
- $filteredsum[$key] = $menu;
- }
- }
-
- $last = 0;
- foreach ($filteredsum as $title => $list) {
- foreach ($list as $h => $link) {
- if ($h > $last) {
- for ($i = 1; $i <= ($h - $last); $i++) {
- $sumstring .= '<ul>' . PHP_EOL;
- }
- $sumstring .= '<li><a href="#' . $title . '">' . $link . '</a></li>' . PHP_EOL;
- } elseif ($h < $last) {
- for ($i = 1; $i <= ($last - $h); $i++) {
- $sumstring .= '</ul>' . PHP_EOL;
- }
- $sumstring .= '<li><a href="#' . $title . '">' . $link . '</a></li>' . PHP_EOL;
- } elseif ($h = $last) {
- $sumstring .= '<li><a href="#' . $title . '">' . $link . '</a></li>' . PHP_EOL;
- }
- $last = $h;
- }
- }
- for ($i = 1; $i <= ($last); $i++) {
- $sumstring .= '</ul>' . PHP_EOL;
- }
-
+ $minlevel = $this->min - 1;
+ $prevlevel = $minlevel;
+
+ foreach ($this->sum as $header) {
+ if ($header->level < $this->min || $header->level > $this->max) {
+ // not in the accepted range, skiping this header.
+ continue;
+ };
+ for ($i = $header->level; $i > $prevlevel; $i--) {
+ $sumstring .= '<ul><li>';
+ }
+ for ($i = $header->level; $i < $prevlevel; $i++) {
+ $sumstring .= '</li></ul>';
}
+ if ($header->level <= $prevlevel) {
+ $sumstring .= '</li><li>';
+ }
+ $sumstring .= "<a href=\"#$header->id\">$header->title</a>";
+ $prevlevel = $header->level;
+ }
+ for ($i = $minlevel; $i < $prevlevel; $i++) {
+ $sumstring .= "</li></ul>";
}
- return $sumstring;
+ return $sumstring;
}