Skip to content

Commit

Permalink
Upgrade YAML Converter
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed Oct 29, 2023
1 parent e769b09 commit 8e1b1c1
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 45 deletions.
2 changes: 1 addition & 1 deletion about.page
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
40 changes: 20 additions & 20 deletions engine/kernel/page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -150,35 +150,35 @@ 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)) {
$exist = true;
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);
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
15 changes: 9 additions & 6 deletions engine/plug/from.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

From::_('page', static function (?string $value, $eval = false): array {
From::_('page', static function (?string $value): array {
if (!$value = n($value)) {
return [];
}
if (0 !== strpos($value, YAML\SOH . "\n")) {
// Make sure page header is present even if it is empty
$value = YAML\SOH . "\n" . YAML\EOT . "\n\n" . $value;
if (3 === strspn($value, '-')) {
$value = trim(substr($value, 3), "\n");
}
$v = (array) From::YAML($value, ' ', P, $eval);
return a($v[0]) + ['content' => $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];
});
27 changes: 10 additions & 17 deletions engine/plug/to.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,30 @@
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 `<p>asdf.</p><p>asdf</p>` 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',
'p',
'pre',
'section',
'summary',
'td',
'th'
// Make sure to add space at the end of the block tag(s) that will be removed. To make `<p>asdf.</p><p>asdf</p>`
// becomes `asdf. asdf` and not `asdf.asdf`.
]) . ')>)\s*/i'], [' ', '$1 '], $value), [
't[dh]'
]) . ')>)\s*/i'], [' ', '$1 '], $value);
$value = strip_tags($value, [
'a',
'abbr',
'b',
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 8e1b1c1

Please sign in to comment.