From 8e1b1c13aa47bf584716f640c879c78449609adb Mon Sep 17 00:00:00 2001 From: Taufik Nurrohman Date: Sun, 29 Oct 2023 13:30:16 +0700 Subject: [PATCH] Upgrade YAML Converter --- about.page | 2 +- composer.json | 2 +- engine/kernel/page.php | 40 ++++++++++++++++++++-------------------- engine/plug/from.php | 15 +++++++++------ engine/plug/to.php | 27 ++++++++++----------------- 5 files changed, 41 insertions(+), 45 deletions(-) diff --git a/about.page b/about.page index 13f2fcb..b7528f9 100755 --- a/about.page +++ b/about.page @@ -5,7 +5,7 @@ icon: 'M19 2L14 6.5V17.5L19 13V2M6.5 5C4.55 5 2.45 5.4 1 6.5V21.16C1 21.41 1.25 color: '#75522f' author: Taufik Nurrohman type: Markdown -version: 3.0.0 +version: 3.1.0 ... This extension uses the structure of the page file location in the `.\lot\page` folder to enable the basic functions of diff --git a/composer.json b/composer.json index df7dc4d..5233d5d 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "name": "mecha-cms/x.page", "require": { "mecha-cms/x.layout": "^2.0.0", - "mecha-cms/x.y-a-m-l": "^2.0.0" + "mecha-cms/x.y-a-m-l": "^3.0.0" }, "suggest": { "ext-mbstring": "It would be great if `ext-mbstring` is installed. We will use other ways if `ext-mbstring` is not available." diff --git a/engine/kernel/page.php b/engine/kernel/page.php index 2d9d2af..dff80df 100644 --- a/engine/kernel/page.php +++ b/engine/kernel/page.php @@ -139,8 +139,8 @@ public function offsetGet($key) { } if ('content' === $key) { $content = n(file_get_contents($path)); - if (YAML\SOH === strtok($content, " \n\t#")) { - $content = trim(explode("\n" . YAML\EOT . "\n", $content . "\n", 2)[1] ?? "", "\n"); + if (3 === strspn($content, '-')) { + $content = trim(explode("\n...\n", $content . "\n", 2)[1] ?? "", "\n"); } else { $content = trim($content, "\n"); } @@ -150,16 +150,16 @@ public function offsetGet($key) { $exist = false; foreach (stream($path) as $k => $v) { // No `---\n` part at the start of the stream means no page header at all - if (0 === $k && (YAML\SOH . "\n" !== $v || YAML\SOH !== strtok($v, " \n\t#"))) { + if (0 === $k && "---\n" !== $v && 3 !== strspn($v, '-')) { break; } // Has reached the `...\n` part in the stream means the end of the page header - if (YAML\EOT . "\n" === $v) { + if ("...\n" === $v) { break; } // Test for `{ asdf: asdf }` part in the stream if ($v && '{' === $v[0]) { - $v = trim(substr(trim(strtok($v, '#')), 1, -1)); + $v = trim(substr(trim(strstr($v, '#', true) ?: $v), 1, -1)); } // Test for `"asdf": asdf` part in the stream if ($v && '"' === $v[0] && preg_match('/^"' . x(strtr($key, ['"' => '\"'])) . '"\s*:/', $v)) { @@ -167,18 +167,18 @@ public function offsetGet($key) { break; } // Test for `'asdf': asdf` part in the stream - if ($v && "'" === $v[0] && preg_match("/^'" . x(strtr($key, ["'" => "\\'"])) . "'\\s*:/", $v)) { + if ($v && "'" === $v[0] && preg_match("/^'" . x(strtr($key, ["'" => "''"])) . "'\\s*:/", $v)) { $exist = true; break; } // Test for `asdf: asdf` part in the stream - if ($v && $key === strtok($v, " \n\t:")) { + if ($v && $key === strtok($v, " :\n\t")) { $exist = true; break; } } if ($exist) { - $lot = From::page(file_get_contents($path), true); + $lot = From::page(file_get_contents($path)); $this->lot = array_replace_recursive($this->lot ?? [], $lot); } } @@ -220,22 +220,22 @@ public function parent(array $lot = []) { } public function time(string $format = null) { - $name = $this->_name(); + $name = (string) $this->_name(); // Set `time` value from the page’s file name - if ( - is_string($name) && ( - // `2017-04-21.page` - 2 === substr_count($name, '-') || - // `2017-04-21-14-25-00.page` - 5 === substr_count($name, '-') - ) && - is_numeric(strtr($name, ['-' => ""])) && - preg_match('/^[1-9]\d{3,}-(0\d|1[0-2])-(0\d|[1-2]\d|3[0-1])(-([0-1]\d|2[0-4])(-([0-5]\d|60)){2})?$/', $name) - ) { + if ($name && ( + // `2017-04-21.page` + 10 === strspn($name, '-0123456789') && 2 === substr_count($name, '-') || + // `2017-04-21-14-25-00.page` + 19 === strspn($name, '-0123456789') && 5 === substr_count($name, '-') + ) && preg_match('/^[1-9]\d{3,}-(0\d|1[0-2])-(0\d|[1-2]\d|3[0-1])(-([0-1]\d|2[0-4])(-([0-5]\d|60)){2})?$/', $name)) { $time = new Time($name); // Else… } else { - $time = new Time($this->offsetGet('time') ?? parent::time()); + $time = $this->offsetGet('time') ?? parent::time(); + if (\is_object($time) && $time instanceof \DateTime) { + $time = $time->format('Y-m-d H:i:s'); + } + $time = new Time($time); } return $format ? $time($format) : $time; } diff --git a/engine/plug/from.php b/engine/plug/from.php index 8febb09..7481ff1 100644 --- a/engine/plug/from.php +++ b/engine/plug/from.php @@ -1,13 +1,16 @@ $v[P] ?? null]; + $v = explode("\n...\n", $value . "\n", 2); + if (is_array($v[0] = From::YAML(trim($v[0], "\n"), true))) { + $v[1] = trim($v[1] ?? "", "\n"); + return array_replace("" !== $v[1] ? ['content' => $v[1]] : [], $v[0]); + } + return ['content' => $value]; }); \ No newline at end of file diff --git a/engine/plug/to.php b/engine/plug/to.php index 20629b5..76efa84 100644 --- a/engine/plug/to.php +++ b/engine/plug/to.php @@ -4,25 +4,20 @@ if (!$value) { return null; } - $value = strip_tags(preg_replace(['/\s+/', '/\s*(<\/(?:' . implode('|', [ + // Add a space at the end of the block tag(s) that will be removed. + // So that `

asdf.

asdf

` becomes `asdf. asdf` and not `asdf.asdf`. + $value = preg_replace(['/\s+/', '/\s*(<\/(?:' . implode('|', [ 'address', 'article', 'blockquote', - 'dd', + 'd[dt]', 'details', 'div', - 'dt', 'figcaption', 'figure', 'footer', - 'h1', - 'h2', - 'h3', - 'h4', - 'h5', - 'h6', + 'h[123456r]', 'header', - 'hr', 'li', 'main', 'nav', @@ -30,11 +25,9 @@ 'pre', 'section', 'summary', - 'td', - 'th' - // Make sure to add space at the end of the block tag(s) that will be removed. To make `

asdf.

asdf

` - // becomes `asdf. asdf` and not `asdf.asdf`. - ]) . ')>)\s*/i'], [' ', '$1 '], $value), [ + 't[dh]' + ]) . ')>)\s*/i'], [' ', '$1 '], $value); + $value = strip_tags($value, [ 'a', 'abbr', 'b', @@ -136,8 +129,8 @@ } $content = $value['content'] ?? ""; unset($value['content']); - $value = rtrim(To::YAML([0 => $value, P => $content], ' ', P) ?? "", "\n"); - return "" !== $value ? $value : null; + $value = rtrim("---\n" . To::YAML($value, 2) . "\n...\n\n" . $content, "\n"); + return "---\n\n..." !== $value ? $value : null; }); To::_('title', static function (?string $value): ?string {