From c238728613accecddcd9612c442f6a2b0280bb8d Mon Sep 17 00:00:00 2001 From: vincent-peugnet Date: Fri, 3 Apr 2020 05:01:37 +0200 Subject: render : elements header-id param close #63 --- MANUAL.md | 15 ++++- app/class/Controllerinfo.php | 3 +- app/class/Element.php | 47 ++++++++++---- app/class/Modelrender.php | 144 ++++++++++++++++++++++--------------------- app/class/Summary.php | 93 +++++++++++++++++----------- 5 files changed, 178 insertions(+), 124 deletions(-) diff --git a/MANUAL.md b/MANUAL.md index 556264c..072182b 100644 --- a/MANUAL.md +++ b/MANUAL.md @@ -262,13 +262,13 @@ This will include a connection form, redirecting to a specified page. Where `&max=% + %SUMMARY?min=&max=&element=% Where `` and `` are integers beetwen `1` to `6`. -You can set `` and `` values to filters beetwen `` and `` header to take care of. - +You can set `` and `` values to filters beetwen `` and `` headlines to take care of. +You can specify an [element](#markdown-elements) to target with the `element` parameter. The summary will only refer to this `element` headlines. #### Page list @@ -356,6 +356,15 @@ Activate or desactivate [markdonw](#markdown) parser in called ``. By d +##### Header ID + + %?headerid=-% + +By default, HTML `#id` are generated for every `

` to `

` headings. You can specify a range of headers outside which no ID will be added. + +You can also set `headerid=0` to totaly disable ID generation for this element. + + ### Templating diff --git a/app/class/Controllerinfo.php b/app/class/Controllerinfo.php index 7b27cea..ab2a526 100644 --- a/app/class/Controllerinfo.php +++ b/app/class/Controllerinfo.php @@ -19,7 +19,8 @@ class Controllerinfo extends Controller $htmlman = file_get_contents(Model::MAN_FILE); $htmlman = $render->rendermanual($htmlman); - $summary = $render->sumparser(2, 4); + $sum = new Summary(['max' => 4, 'sum' => $render->sum()]); + $summary = $sum->sumparser(); $this->showtemplate('info', ['version' => getversion(), 'manual' => $htmlman, 'summary' => $summary]); diff --git a/app/class/Element.php b/app/class/Element.php index d94c76f..1193c66 100644 --- a/app/class/Element.php +++ b/app/class/Element.php @@ -13,21 +13,24 @@ class Element extends Item protected $autolink = 0; protected $markdown = 1; protected $content = ''; + protected $minheaderid = 1; + protected $maxheaderid = 6; + protected $headerid = 1; // __________________________________________________ F U N ____________________________________________________________ - public function __construct($datas = [], $pageid) - { + public function __construct($datas = [], $pageid) + { $this->hydrate($datas); $this->analyse($pageid); } private function analyse(string $pageid) { - if(!empty($this->options)) { + if (!empty($this->options)) { $this->options = str_replace('*', $pageid, $this->options); parse_str($this->options, $datas); if (isset($datas['id'])) { @@ -88,6 +91,21 @@ class Element extends Item return $this->content; } + public function minheaderid() + { + return $this->minheaderid; + } + + public function maxheaderid() + { + return $this->maxheaderid; + } + + public function headerid() + { + return $this->headerid; + } + @@ -104,21 +122,21 @@ class Element extends Item public function settype(string $type) { $type = strtolower($type); - if(in_array($type, Model::TEXT_ELEMENTS)) { + if (in_array($type, Model::TEXT_ELEMENTS)) { $this->type = $type; } } public function setoptions(string $options) { - if(!empty($options)) { + if (!empty($options)) { $this->options = $options; } } public function setautolink(int $level) { - if($level >= 0 && $level <= 16) { + if ($level >= 0 && $level <= 16) { $this->autolink = $level; return true; } else { @@ -128,7 +146,7 @@ class Element extends Item public function setmarkdown(int $level) { - if($level >= 0 && $level <= 1) { + if ($level >= 0 && $level <= 1) { $this->markdown = $level; return true; } else { @@ -141,9 +159,14 @@ class Element extends Item $this->content = $content; } + public function setheaderid(string $headerid) + { + if ($headerid == 0) { + $this->headerid = 0; + } else { + preg_match('~([1-6])\-([1-6])~', $headerid, $out); + $this->minheaderid = intval($out[1]); + $this->maxheaderid = intval($out[2]); + } + } } - - - - -?> \ No newline at end of file diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php index 4719b56..35bd028 100644 --- a/app/class/Modelrender.php +++ b/app/class/Modelrender.php @@ -7,7 +7,7 @@ use Michelf\MarkdownExtra; class Modelrender extends Modelpage { - /** @var \AltoRouter */ + /** @var \AltoRouter */ protected $router; /** @var Page */ protected $page; @@ -18,8 +18,6 @@ class Modelrender extends Modelpage protected $internallinkblank = ''; protected $externallinkblank = ''; - const SUMMARY = '%SUMMARY%'; - const RENDER_VERBOSE = 1; public function __construct(\AltoRouter $router) @@ -29,11 +27,11 @@ class Modelrender extends Modelpage $this->router = $router; $this->pagelist = $this->list(); - if(Config::internallinkblank()) { + if (Config::internallinkblank()) { $this->internallinkblank = ' target="_blank" '; } - if(Config::externallinkblank()) { + if (Config::externallinkblank()) { $this->externallinkblank = ' target="_blank" '; } } @@ -44,12 +42,11 @@ class Modelrender extends Modelpage * @param string $text Input text in markdown * @return string html formated text */ - public function rendermanual(string $text) : string + public function rendermanual(string $text): string { $text = $this->markdown($text); - $text = $this->headerid($text, 5); + $text = $this->headerid($text, 1, 5); return $text; - } @@ -60,7 +57,7 @@ class Modelrender extends Modelpage * * @return string Relative URL */ - public function upage(string $id) : string + public function upage(string $id): string { return $this->router->generate('pageread/', ['page' => $id]); } @@ -88,10 +85,10 @@ class Modelrender extends Modelpage $head = $this->gethead(); $body = $this->getbody($this->readbody()); - $parsebody = $this->parser($body); + $parsebody = $this->bodyparser($body); $html = '' . PHP_EOL . '' . PHP_EOL . '' . PHP_EOL . $head . PHP_EOL . '' . PHP_EOL . $parsebody . PHP_EOL . ''; - + return $html; } @@ -101,7 +98,7 @@ class Modelrender extends Modelpage if (!empty($this->page->templatebody())) { $templateid = $this->page->templatebody(); $templatepage = $this->get($templateid); - if($templatepage !== false) { + if ($templatepage !== false) { $body = $templatepage->body(); } else { $body = $this->page->body(); @@ -123,7 +120,7 @@ class Modelrender extends Modelpage * * @return string as the full rendered BODY of the page */ - public function getbody(string $body) : string + public function getbody(string $body): string { // Elements that can be detected $types = ['HEADER', 'NAV', 'MAIN', 'ASIDE', 'FOOTER']; @@ -134,21 +131,19 @@ class Modelrender extends Modelpage $matches = $this->match($body, $regex); // First, analyse the synthax and call the corresponding methods - if(isset($matches)) { + if (isset($matches)) { foreach ($matches as $key => $match) { $element = new Element($match, $this->page->id()); $element->setcontent($this->getelementcontent($element->sources(), $element->type())); $element->setcontent($this->elementparser($element)); $element->addtags(); $body = str_replace($element->fullmatch(), $element->content(), $body); - } } return $body; - } /** @@ -161,23 +156,21 @@ class Modelrender extends Modelpage { $content = ''; $subseparator = PHP_EOL . PHP_EOL; - foreach($sources as $source) - { - if($source !== $this->page->id()) { + foreach ($sources as $source) { + if ($source !== $this->page->id()) { $subcontent = $this->getpageelement($source, $type); - if($subcontent !== false) { - if(empty($subcontent && self::RENDER_VERBOSE > 0)) { + if ($subcontent !== false) { + if (empty($subcontent && self::RENDER_VERBOSE > 0)) { $subcontent = PHP_EOL . '' . PHP_EOL; } } else { $read = '

Rendering error :

The page ' . $source . ', does not exist yet.

'; //throw new Exception($read); } - } else { $subcontent = $this->page->$type(); } - $content .= $subseparator . $subcontent; + $content .= $subseparator . $subcontent; } return $content . $subseparator; } @@ -189,12 +182,16 @@ class Modelrender extends Modelpage $content = $this->pagelist($content); $content = $this->date($content); $content = $this->thumbnail($content); - if($element->autolink()) { + if ($element->autolink()) { $content = $this->everylink($content, $element->autolink()); } - if($element->markdown()) { + if ($element->markdown()) { $content = $this->markdown($content); } + $content = $this->desctitle($content, $this->page->description(), $this->page->title()); + if($element->headerid()) { + $content = $this->headerid($content, $element->minheaderid(), $element->maxheaderid(), $element->type()); + } return $content; } @@ -246,15 +243,15 @@ class Modelrender extends Modelpage $head .= '' . PHP_EOL; $head .= '' . PHP_EOL; - - if(!empty($this->page->thumbnail())) { + + if (!empty($this->page->thumbnail())) { $head .= '' . PHP_EOL; - } elseif(!empty(Config::defaultthumbnail())) { + } elseif (!empty(Config::defaultthumbnail())) { $head .= '' . PHP_EOL; } - + $head .= '' . PHP_EOL; - + foreach ($this->page->externalcss() as $externalcss) { $head .= '' . PHP_EOL; @@ -262,7 +259,7 @@ class Modelrender extends Modelpage if (!empty($this->page->templatecss() && in_array('externalcss', $this->page->templateoptions()))) { $templatecss = $this->get($this->page->templatecss()); - if($templatecss !== false) { + if ($templatecss !== false) { foreach ($templatecss->externalcss() as $externalcss) { $head .= '' . PHP_EOL; @@ -304,7 +301,7 @@ class Modelrender extends Modelpage ' . PHP_EOL; } - + if (!empty($this->page->redirection())) { if (preg_match('%https?:\/\/\S*%', $this->page->redirection(), $out)) { $url = $out[0]; @@ -327,19 +324,16 @@ class Modelrender extends Modelpage } - public function parser(string $text) + public function bodyparser(string $text) { $text = $this->media($text); - - $text = $this->headerid($text); - + $text = $this->summary($text); $text = $this->wurl($text); $text = $this->wikiurl($text); - - $text = $this->desctitle($text, $this->page->description(), $this->page->title()); - + + $text = str_replace('href="http', "class=\"external\" $this->externallinkblank href=\"http", $text); @@ -352,7 +346,7 @@ class Modelrender extends Modelpage return $text; } - public function media(string $text) : string + public function media(string $text): string { $text = preg_replace('%(src|href)="([\w-_]+(\/([\w-_])+)*\.[a-z0-9]{1,5})"%', '$1="' . Model::mediapath() . '$2" target="_blank" class="media"', $text); if (!is_string($text)) { @@ -395,7 +389,7 @@ class Modelrender extends Modelpage $link = 'href="' . $rend->upage($matches[1]) . '"" title="' . Config::existnot() . '" class="internal existnot"' . $this->internallinkblank; } else { $linkto[] = $matchpage->id(); - $link = 'href="' . $rend->upage($matches[1]) . $matches[2] . '" title="' . $matchpage->description() . '" class="internal exist '. $matchpage->secure('string') .'"' . $this->internallinkblank; + $link = 'href="' . $rend->upage($matches[1]) . $matches[2] . '" title="' . $matchpage->description() . '" class="internal exist ' . $matchpage->secure('string') . '"' . $this->internallinkblank; } return $link; }, @@ -414,10 +408,10 @@ class Modelrender extends Modelpage function ($matches) use ($rend, &$linkto) { $matchpage = $rend->get($matches[1]); if (!$matchpage) { - return 'internallinkblank .' >' . $matches[1] . ''; + return 'internallinkblank . ' >' . $matches[1] . ''; } else { $linkto[] = $matchpage->id(); - return 'internallinkblank .' >' . $matchpage->title() . ''; + return 'internallinkblank . ' >' . $matchpage->title() . ''; } }, $text @@ -430,21 +424,25 @@ class Modelrender extends Modelpage * Add Id to html header elements and store the titles in the `$this->sum` var * * @param string $text Input html document to scan - * @param int $maxdeepness Maximum header deepness to look for. Min = 1 Max = 6 Default = 6 + * @param int $min Maximum header deepness to look for. Min = 1 Max = 6 Default = 1 + * @param int $max Maximum header deepness to look for. Min = 1 Max = 6 Default = 6 + * @param string $element Name of element being analysed * * @return string text with id in header */ - public function headerid($text, int $maxdeepness = 6) + public function headerid(string $text, int $min = 1, int $max = 6, string $element = 'body') : string { - if($maxdeepness > 6 || $maxdeepness < 1) { - $maxdeepness = 6; + if ($min > 6 || $min < 1) { + $min = 6; + } + if ($max > 6 || $max < 1) { + $max = 6; } - $sum = []; $text = preg_replace_callback( - '/(.+)<\/h[1-' . $maxdeepness . ']>/mU', + '/(.+)<\/h[' . $min . '-' . $max . ']>/mU', function ($matches) use (&$sum) { $cleanid = idclean($matches[4]); $sum[$cleanid][$matches[1]] = $matches[4]; @@ -452,7 +450,7 @@ class Modelrender extends Modelpage }, $text ); - $this->sum = $sum; + $this->sum[$element] = $sum; return $text; } @@ -493,9 +491,9 @@ class Modelrender extends Modelpage * * @return array $matches Ordered array containing an array of `fullmatch` and `filter` */ - public function match(string $text, string $include) : array + public function match(string $text, string $include): array { - preg_match_all('~\%(' . $include . ')(\?([a-zA-Z0-9\[\]\&=\-\/\%]*))?\%~', $text, $out); + preg_match_all('~\%(' . $include . ')(\?([a-zA-Z0-9\[\]\&=\-\/\%\+\*\;]*))?\%~', $text, $out); $matches = []; @@ -511,11 +509,11 @@ class Modelrender extends Modelpage * * @return string Output text */ - public function automedialist(string $text) : string + public function automedialist(string $text): string { $matches = $this->match($text, 'MEDIA'); - if(isset($matches)) { + if (isset($matches)) { foreach ($matches as $match) { $medialist = new Medialist($match); $medialist->readoptions(); @@ -531,12 +529,12 @@ class Modelrender extends Modelpage * * @return string Output text */ - public function summary(string $text) : string + public function summary(string $text): string { $matches = $this->match($text, 'SUMMARY'); - if(!empty($matches)) { + if (!empty($matches)) { foreach ($matches as $match) { $data = array_merge($match, ['sum' => $this->sum]); $summary = new Summary($data); @@ -548,7 +546,7 @@ class Modelrender extends Modelpage - public function date(string $text) : string + public function date(string $text): string { $page = $this->page; $text = preg_replace_callback('~\%DATE\%~', function ($matches) use ($page) { @@ -568,7 +566,7 @@ class Modelrender extends Modelpage * * @return string The rendered output */ - public function thumbnail(string $text) : string + public function thumbnail(string $text): string { $img = '' . $this->page->title() . ''; $img = PHP_EOL . $img . PHP_EOL; @@ -584,10 +582,10 @@ class Modelrender extends Modelpage * * @return string Conversion output */ - public function everylink(string $text, int $limit) : string + public function everylink(string $text, int $limit): string { $regex = '~([\w-_éêèùïüîçà]{' . $limit . ',})(?![^<]*>|[^<>]*<\/)~'; - $text = preg_replace_callback($regex , function ($matches) { + $text = preg_replace_callback($regex, function ($matches) { return '' . $matches[1] . ''; }, $text); return $text; @@ -600,12 +598,12 @@ class Modelrender extends Modelpage * * @return string text ouput */ - public function authenticate(string $text) : string + public function authenticate(string $text): string { $id = $this->page->id(); $regex = '~\%CONNECT(\?dir=([a-zA-Z0-9-_]+))?\%~'; $text = preg_replace_callback($regex, function ($matches) use ($id) { - if(isset($matches[2])) { + if (isset($matches[2])) { $id = $matches[2]; } $form = '
@@ -615,7 +613,6 @@ class Modelrender extends Modelpage
'; return $form; - }, $text); return $text; } @@ -623,13 +620,13 @@ class Modelrender extends Modelpage /** * Render pages list */ - public function pagelist(string $text) : string + public function pagelist(string $text): string { $matches = $this->match($text, 'LIST'); $modelhome = new Modelhome(); - if(isset($matches)) { + if (isset($matches)) { $pagelist = $this->getlister(); foreach ($matches as $match) { @@ -637,20 +634,20 @@ class Modelrender extends Modelpage $optlist->parsehydrate($match['options']); $pagetable = $modelhome->pagetable($pagelist, $optlist, '', []); - $content = '
    ' . PHP_EOL ; - foreach ($pagetable as $page ) { + $content = '
      ' . PHP_EOL; + foreach ($pagetable as $page) { $content .= '
    • ' . PHP_EOL; $content .= '' . $page->title() . '' . PHP_EOL; - if($optlist->description()) { + if ($optlist->description()) { $content .= '' . $page->description() . '' . PHP_EOL; } - if($optlist->date()) { + if ($optlist->date()) { $content .= '' . $page->date('pdate') . '' . PHP_EOL; } - if($optlist->time()) { + if ($optlist->time()) { $content .= '' . $page->date('ptime') . '' . PHP_EOL; } - if($optlist->author()) { + if ($optlist->author()) { $content .= $page->authors('string') . PHP_EOL; } $content .= '
    • '; @@ -679,5 +676,10 @@ class Modelrender extends Modelpage } + // _________________________ G E T ___________________________________ + public function sum() + { + return $this->sum; + } } diff --git a/app/class/Summary.php b/app/class/Summary.php index 288ad2d..373fb33 100644 --- a/app/class/Summary.php +++ b/app/class/Summary.php @@ -19,6 +19,9 @@ class Summary extends Item /** @var array Headers datas */ protected $sum = []; + /** @var string Name of element to display */ + protected $element = null; + @@ -32,7 +35,7 @@ class Summary extends Item public function readoptions() { - parse_str($this->options, $datas); + parse_str(htmlspecialchars_decode($this->options), $datas); $this->hydrate($datas); } @@ -44,38 +47,46 @@ class Summary extends Item */ public function sumparser() { - $filteredsum = []; - - foreach ($this->sum as $key => $menu) { - $deepness = array_keys($menu)[0]; - if($deepness >= $this->min && $deepness <= $this->max) { - $filteredsum[$key] = $menu; - } - } - - $sumstring = ''; - $last = 0; - foreach ($filteredsum as $title => $list) { - foreach ($list as $h => $link) { - if ($h > $last) { - for ($i = 1; $i <= ($h - $last); $i++) { - $sumstring .= '
        '; - } - $sumstring .= '
      • ' . $link . '
      • '; - } elseif ($h < $last) { - for ($i = 1; $i <= ($last - $h); $i++) { - $sumstring .= '
      '; - } - $sumstring .= '
    • ' . $link . '
    • '; - } elseif ($h = $last) { - $sumstring .= '
    • ' . $link . '
    • '; - } - $last = $h; - } - } - for ($i = 1; $i <= ($last); $i++) { - $sumstring .= '
    '; - } + $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) { + for ($i = 1; $i <= ($last - $h); $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; + } + + } + } return $sumstring; } @@ -94,6 +105,11 @@ class Summary extends Item return $this->options; } + public function element() + { + return $this->element; + } + // __________________________________________________ S E T ____________________________________________________________ @@ -132,8 +148,11 @@ class Summary extends Item $this->sum = $sum; } -} - - + public function setelement(string $element) + { + if(in_array($element, Model::TEXT_ELEMENTS)) { + $this->element = $element; + } + } -?> \ No newline at end of file +} -- cgit v1.2.3