aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/class/Medialist.php18
-rw-r--r--app/class/Modelrender.php111
-rw-r--r--app/class/Summary.php139
3 files changed, 190 insertions, 78 deletions
diff --git a/app/class/Medialist.php b/app/class/Medialist.php
index 3395b58..b0964e3 100644
--- a/app/class/Medialist.php
+++ b/app/class/Medialist.php
@@ -7,8 +7,8 @@ class Medialist extends Item
/** @var string full regex match */
protected $fullmatch;
- /** @var string full filter code line */
- protected $filter = '';
+ /** @var string full options code line */
+ protected $options = '';
/** @var string directory of media */
protected $path = '';
@@ -45,9 +45,9 @@ class Medialist extends Item
$this->hydrate($datas);
}
- public function readfilter()
+ public function readoptions()
{
- parse_str($this->filter, $datas);
+ parse_str($this->options, $datas);
$this->hydrate($datas);
}
@@ -137,9 +137,9 @@ class Medialist extends Item
return $this->fullmatch;
}
- public function filter()
+ public function options()
{
- return $this->filter;
+ return $this->options;
}
/**
@@ -182,10 +182,10 @@ class Medialist extends Item
}
- public function setfilter(string $filter)
+ public function setoptions(string $options)
{
- if (!empty($filter)) {
- $this->filter = $filter;
+ if (!empty($options)) {
+ $this->options = $options;
}
}
diff --git a/app/class/Modelrender.php b/app/class/Modelrender.php
index bb6cf03..4719b56 100644
--- a/app/class/Modelrender.php
+++ b/app/class/Modelrender.php
@@ -129,16 +129,9 @@ class Modelrender extends Modelpage
$types = ['HEADER', 'NAV', 'MAIN', 'ASIDE', 'FOOTER'];
// First level regex
- $regex = '~\%(' . implode("|", $types) . ')(\?([\S]+))?\%~';
-
- // Match the first level regex
- preg_match_all($regex, $body, $out);
-
- // Create a list of all the elements that passed through the first level regex
- foreach ($out[0] as $key => $match) {
- $matches[$key] = ['fullmatch' => $match, 'type' => $out[1][$key], 'options' => $out[3][$key]];
- }
+ $regex = implode("|", $types);
+ $matches = $this->match($body, $regex);
// First, analyse the synthax and call the corresponding methods
if(isset($matches)) {
@@ -340,7 +333,7 @@ class Modelrender extends Modelpage
$text = $this->headerid($text);
- $text = str_replace(self::SUMMARY, $this->sumparser(), $text);
+ $text = $this->summary($text);
$text = $this->wurl($text);
$text = $this->wikiurl($text);
@@ -434,7 +427,7 @@ class Modelrender extends Modelpage
}
/**
- * Add Id to html header elements and store the titles in the `sum` parameter
+ * 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
@@ -493,23 +486,39 @@ class Modelrender extends Modelpage
}
/**
- * Check for media list call in the text and insert media list
- * @param string $text Text to scan and replace
+ * Match `%INCLUDE?params=values&...%`
*
- * @return string Output text
+ * @param string $text Input text to scan
+ * @param string $include word to match
+ *
+ * @return array $matches Ordered array containing an array of `fullmatch` and `filter`
*/
- public function automedialist(string $text)
+ public function match(string $text, string $include) : array
{
- preg_match_all('~\%MEDIA\?([a-zA-Z0-9\[\]\&=\-\/\%]*)\%~', $text, $out);
+ preg_match_all('~\%(' . $include . ')(\?([a-zA-Z0-9\[\]\&=\-\/\%]*))?\%~', $text, $out);
+
+ $matches = [];
foreach ($out[0] as $key => $match) {
- $matches[$key] = ['fullmatch' => $match, 'filter' => $out[1][$key]];
+ $matches[$key] = ['fullmatch' => $match, 'type' => $out[1][$key], 'options' => $out[3][$key]];
}
+ return $matches;
+ }
+
+ /**
+ * Check for media list call in the text and insert media list
+ * @param string $text Text to scan and replace
+ *
+ * @return string Output text
+ */
+ public function automedialist(string $text) : string
+ {
+ $matches = $this->match($text, 'MEDIA');
if(isset($matches)) {
foreach ($matches as $match) {
$medialist = new Medialist($match);
- $medialist->readfilter();
+ $medialist->readoptions();
$text = str_replace($medialist->fullmatch(), $medialist->generatecontent(), $text);
}
}
@@ -517,57 +526,29 @@ class Modelrender extends Modelpage
}
/**
- * Generate a Summary based on header ids. Need to use `$this->headerid` before to scan text
- *
- * @param int $min Minimum header deepness to start the summary : Between 1 and 6.
- * @param int $max Maximum header deepness to start the summary : Between 1 and 6.
+ * Check for Summary calls in the text and insert html summary
+ * @param string $text Text to scan and replace
*
- * @return string html list with anchor link
+ * @return string Output text
*/
- function sumparser(int $min = 1, int $max = 6) : string
+ public function summary(string $text) : string
{
- $min = $min >= 1 && $min <= 6 && $min <= $max ? $min : 1;
- $end = $max >=1 && $max <= 6 && $max >= $min ? $max : 6;
-
- $sum = $this->sum;
+ $matches = $this->match($text, 'SUMMARY');
- $filteredsum = [];
-
- foreach ($sum as $key => $menu) {
- $deepness = array_keys($menu)[0];
- if($deepness >= $min && $deepness <= $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 .= '<ul>';
- }
- $sumstring .= '<li><a href="#' . $title . '">' . $link . '</a></li>';
- } elseif ($h < $last) {
- for ($i = 1; $i <= ($last - $h); $i++) {
- $sumstring .= '</ul>';
- }
- $sumstring .= '<li><a href="#' . $title . '">' . $link . '</a></li>';
- } elseif ($h = $last) {
- $sumstring .= '<li><a href="#' . $title . '">' . $link . '</a></li>';
- }
- $last = $h;
+ if(!empty($matches)) {
+ foreach ($matches as $match) {
+ $data = array_merge($match, ['sum' => $this->sum]);
+ $summary = new Summary($data);
+ $text = str_replace($summary->fullmatch(), $summary->sumparser(), $text);
}
}
- for ($i = 1; $i <= ($last); $i++) {
- $sumstring .= '</ul>';
- }
- return $sumstring;
+ return $text;
}
- public function date(string $text)
+
+ public function date(string $text) : string
{
$page = $this->page;
$text = preg_replace_callback('~\%DATE\%~', function ($matches) use ($page) {
@@ -619,7 +600,7 @@ class Modelrender extends Modelpage
*
* @return string text ouput
*/
- public function authenticate(string $text)
+ public function authenticate(string $text) : string
{
$id = $this->page->id();
$regex = '~\%CONNECT(\?dir=([a-zA-Z0-9-_]+))?\%~';
@@ -644,11 +625,7 @@ class Modelrender extends Modelpage
*/
public function pagelist(string $text) : string
{
- preg_match_all('~\%LIST\?([a-zA-Z0-9\]\[\&=\-\/\%]*)\%~', $text, $out);
-
- foreach ($out[0] as $key => $match) {
- $matches[$key] = ['fullmatch' => $match, 'options' => $out[1][$key]];
- }
+ $matches = $this->match($text, 'LIST');
$modelhome = new Modelhome();
@@ -704,7 +681,3 @@ class Modelrender extends Modelpage
}
-
-
-
-?> \ No newline at end of file
diff --git a/app/class/Summary.php b/app/class/Summary.php
new file mode 100644
index 0000000..288ad2d
--- /dev/null
+++ b/app/class/Summary.php
@@ -0,0 +1,139 @@
+<?php
+
+namespace Wcms;
+
+class Summary extends Item
+{
+ /** @var string full regex match */
+ protected $fullmatch;
+
+ /** @var string full options code line */
+ protected $options = '';
+
+ /** @var int Minimum summary level*/
+ protected $min = 1;
+
+ /** @var int Maximum summary level*/
+ protected $max = 6;
+
+ /** @var array Headers datas */
+ protected $sum = [];
+
+
+
+
+
+ public function __construct(array $datas = [])
+ {
+ $this->hydrate($datas);
+ $this->readoptions();
+ }
+
+
+ public function readoptions()
+ {
+ parse_str($this->options, $datas);
+ $this->hydrate($datas);
+ }
+
+
+ /**
+ * Generate a Summary based on header ids. Need to use `$this->headerid` before to scan text
+ *
+ * @return string html list with anchor link
+ */
+ 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 .= '<ul>';
+ }
+ $sumstring .= '<li><a href="#' . $title . '">' . $link . '</a></li>';
+ } elseif ($h < $last) {
+ for ($i = 1; $i <= ($last - $h); $i++) {
+ $sumstring .= '</ul>';
+ }
+ $sumstring .= '<li><a href="#' . $title . '">' . $link . '</a></li>';
+ } elseif ($h = $last) {
+ $sumstring .= '<li><a href="#' . $title . '">' . $link . '</a></li>';
+ }
+ $last = $h;
+ }
+ }
+ for ($i = 1; $i <= ($last); $i++) {
+ $sumstring .= '</ul>';
+ }
+ return $sumstring;
+ }
+
+
+
+ // __________________________________________________ G E T ____________________________________________________________
+
+
+ public function fullmatch()
+ {
+ return $this->fullmatch;
+ }
+
+ public function options()
+ {
+ return $this->options;
+ }
+
+
+ // __________________________________________________ S E T ____________________________________________________________
+
+
+ public function setfullmatch(string $fullmatch)
+ {
+ $this->fullmatch = $fullmatch;
+ }
+
+
+ public function setoptions(string $options)
+ {
+ if (!empty($options)) {
+ $this->options = $options;
+ }
+ }
+
+ public function setmin($min)
+ {
+ $min = intval($min);
+ if($min >= 1 && $min <= 6) {
+ $this->min = $min;
+ }
+ }
+
+ public function setmax($max)
+ {
+ $max = intval($max);
+ if($max >= 1 && $max <= 6) {
+ $this->max = $max;
+ }
+ }
+
+ public function setsum(array $sum)
+ {
+ $this->sum = $sum;
+ }
+
+}
+
+
+
+?> \ No newline at end of file