From ef4d9d383a92a6759ee6e6b2961d5b8e1b9f5db6 Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Wed, 8 Apr 2020 16:06:41 +0200 Subject: fix: SUMMARY produces invalid HTML code - add new Header class to pass clean data structures. - store all headers in the same array. (not anymore grouped by element) - modify sumparser to produce valid HTML. Fixes #81 --- app/class/Header.php | 24 ++++++++++++++++++ app/class/Modelrender.php | 6 ++--- app/class/Summary.php | 62 ++++++++++++++++++----------------------------- 3 files changed, 49 insertions(+), 43 deletions(-) create mode 100644 app/class/Header.php (limited to 'app') 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 @@ +id = $id; + $this->level = $level; + $this->title = $title; + } +} diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php index 29a3fa7..1721e4e 100644 --- a/app/class/Modelrender.php +++ b/app/class/Modelrender.php @@ -437,17 +437,15 @@ class Modelrender extends Modelpage $max = 6; } - $sum = []; $text = preg_replace_callback( '/(.+)<\/h[' . $min . '-' . $max . ']>/mU', - function ($matches) use (&$sum) { + function ($matches) { $cleanid = idclean($matches[4]); - $sum[$cleanid][$matches[1]] = $matches[4]; + $this->sum[] = new Header($cleanid, intval($matches[1]), $matches[4]); return '' . $matches[4] . ''; }, $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 .= '' . PHP_EOL; - } - $sumstring .= '
  • ' . $link . '
  • ' . PHP_EOL; - } elseif ($h = $last) { - $sumstring .= '
  • ' . $link . '
  • ' . PHP_EOL; - } - $last = $h; - } - } - for ($i = 1; $i <= ($last); $i++) { - $sumstring .= '' . 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 .= ''; } + if ($header->level <= $prevlevel) { + $sumstring .= '
  • '; + } + $sumstring .= "id\">$header->title"; + $prevlevel = $header->level; + } + for ($i = $minlevel; $i < $prevlevel; $i++) { + $sumstring .= "
  • "; } - return $sumstring; + return $sumstring; } -- cgit v1.2.3 From bfa7d5b2b22b2286faba00884925ff7ecb9392cb Mon Sep 17 00:00:00 2001 From: n-peugnet Date: Wed, 8 Apr 2020 17:22:41 +0200 Subject: fix: SUMMARY does not work with custom header Ids Fixes #80 --- app/class/Modelrender.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'app') diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php index 1721e4e..091179d 100644 --- a/app/class/Modelrender.php +++ b/app/class/Modelrender.php @@ -438,11 +438,19 @@ class Modelrender extends Modelpage } $text = preg_replace_callback( - '/(.+)<\/h[' . $min . '-' . $max . ']>/mU', + "/(.+)<\/h[$min-$max]>/mU", function ($matches) { - $cleanid = idclean($matches[4]); - $this->sum[] = new Header($cleanid, intval($matches[1]), $matches[4]); - return '' . $matches[4] . ''; + $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 "$content"; }, $text ); -- cgit v1.2.3