Ditching the .archive, .data, .draft, and .page File Extension(s)
#302
Replies: 7 comments 7 replies
-
|
Typical page listing method: // Native PHP
foreach (glob(LOT . D . 'page' . D . 'article' . D . '*.{ini,json,php,xml,yaml,yml}', GLOB_BRACE | GLOB_NOSORT) as $k => $v) { /* … */ }
// Feature in the CMS
foreach (g(LOT . D . 'page' . D . 'article', 'ini,json,php,xml,yaml,yml') as $k => $v) { /* … */ }The problem arises:
My current solution is to use a mechanism similar to the one in my Image extension to create a list of functions named after the file extension it will support: namespace {
if (!\function_exists("\\yaml_parse")) {
function yaml_parse(string $input, int $pos = 0, int &$ndocs = 0, ?array $callbacks = null) {
// <https://github.com/mecha-cms/x.y-a-m-l/blob/v3.1.2/engine/plug/from.php#L7>
return \From::YAML($input, true);
}
}
}
namespace x\page\from {
function ini(string $path) {
$r = \parse_ini_string(\file_get_contents($path), true, \INI_SCANNER_NORMAL);
return \is_array($r) ? $r : [];
}
function json(string $path) {
$r = \json_decode(\file_get_contents($path), true);
return \is_array($r) ? $r : [];
}
function php(string $path) {
$r = require $path;
return \is_array($r) ? $r : [];
}
function xml(string $path) { /* … */ }
function yaml(string $path) {
$r = yaml_parse(\file_get_contents($path), 0);
return \is_array($r) ? $r : [];
}
function yml(string $path) {
return yaml($path);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
The following is an example of how the content of the page file might change if the Before---
title: Page Title
description: Page description.
type: Markdown
...
### Lorem Ipsum
Lorem ipsum **dolor** sit amet.Aftertitle: Page Title
description: Page description.
type: Markdown
content: |
### Lorem Ipsum
Lorem ipsum **dolor** sit amet. |
Beta Was this translation helpful? Give feedback.
-
|
To generate list of supported file extensions: function get_supported_page_extensions() {
static $cache = null;
if (null !== $cache) {
return $cache;
}
$prefix = "x\\page\\from\\";
$r = [];
foreach (get_defined_functions()['user'] as $v) {
if (0 === strpos($v, $prefix)) {
$r[] = substr($v, strlen($prefix));
}
}
return ($cache = implode(',', $r));
} |
Beta Was this translation helpful? Give feedback.
-
INItitle = Page Title
description = Page description.
type = Markdown
content = "
### Lorem Ipsum
Lorem ipsum **dolor** sit amet.
"JSON{
"title": "Page Title",
"description": "Page description.",
"type": "Markdown",
"content": "### Lorem Ipsum\n\nLorem ipsum **dolor** sit amet."
}PHP<?php return [
'title' => 'Page Title',
'description' => 'Page description.',
'type' => 'Markdown',
'content' => <<<'S'
### Lorem Ipsum
Lorem ipsum **dolor** sit amet.
S
];XML<page>
<title>Page Title</title>
<description>Page description.</description>
<type>Markdown</type>
<content><![CDATA[
### Lorem Ipsum
Lorem ipsum **dolor** sit amet.
]]></content>
</page> |
Beta Was this translation helpful? Give feedback.
-
|
Hmmm… In this situation, how to tell if .\
└── lot\
└── page\
├── lorem-ipsum\
│ └── time.yaml ✔
└── lorem-ipsum.yaml |
Beta Was this translation helpful? Give feedback.
-
|
I have an idea that would keep the existing page files working by simply replacing the file extension with
With the new YAML parsing result specification, both formats in the example below would produce the same result. However, special handling is needed to parse the older format, which I want to keep since it is the most ✨ aesthetic ✨, in my opinion. Style 1This is a valid YAML document. No additional processing is necessary. Parsing it with title: Page Title
description: Page description.
type: Markdown
content: |
### Lorem Ipsum
Lorem ipsum **dolor** sit amet.$lot = From::YAML($content, true);Style 2This is also a valid YAML document that will produce an array consisting of a single document. Any text after the ---
title: Page Title
description: Page description.
type: Markdown
...
### Lorem Ipsum
Lorem ipsum **dolor** sit amet.$lot = From::YAML($content, true);
if (is_array($lot = $lot[0] ?? [])) {
if (!array_key_exists('content', $lot)) {
$lot['content'] = trim(explode("\n...\n", $content . "\n", 2)[1] ?? "", "\n");
}
} else {
$lot = [];
} |
Beta Was this translation helpful? Give feedback.
-
|
The new specification for drafted files is to name them with a .\
└── lot\
└── page\
├── lorem-ipsum\
├── ~test.yaml ✔
└── lorem-ipsum.yamlThere is still no specific convention for archived files. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I’m thinking about adding support for multiple page file extensions that are widely known as data store files, such as
.ini,.json,.php,.xml, and.yaml. Those are commonly used to store key-value pair data. Some allow nesting, some don’t. But the most important thing is that some of them do have their own parser, which is available by default provided by PHP. This makes it possible to store a page file with the.jsonextension, for example, so that parsing would be as simple as:Pros
.jsonand.yaml, you can basically just throw random JSON and YAML files into the.\lot\pagefolder, and then they will become valid page files.parse_ini_string()function.json_decode()function.include()orrequire()function.simplexml_load_string()function.yaml_parse()function. 1Cons
.yamland expect it to work.Footnotes
This requires the PHP
yamlextension, which is not installed by default, as far as I know. ↩Beta Was this translation helpful? Give feedback.
All reactions