使用 PHP 解析 YAML Front Matter
我有 YAML Front Matter 我想用 PHP 解析:
---
title = A nice title goes here
tags = tag1 tag2 tag3
---
This is the content of this entry...
Line2
Line3
我知道它是关于某种 Ruby gem 的,但我想在 PHP 中使用它来创建一个用户友好的平面文件博客引擎。
我还有一个名为 Phrozn 的项目的片段。也许你们可以方便地看到它,以便尽可能地帮助我解决问题。
private function parse()
{
if (isset($this->template, $this->frontMatter)) {
return $this;
}
$source = $this->readSourceFile();
$parts = preg_split('/[\n]*[-]{3}[\n]/', $source, 2);
if (count($parts) === 2) {
$this->frontMatter = Yaml::load($parts[0]);
$this->template = trim($parts[1]);
} else {
$this->frontMatter = null;
$this->template = trim($source);
}
return $this;
}
I have YAML Front Matter that I want to parse with PHP:
---
title = A nice title goes here
tags = tag1 tag2 tag3
---
This is the content of this entry...
Line2
Line3
I know it's about a Ruby gem of some kind, but I want use this in PHP to create a user-friendly flatfile blog engine.
I also have a snippet from a project called Phrozn. Maybe it can be handy for you guys to see it in order to help me with the problem as best as possible.
private function parse()
{
if (isset($this->template, $this->frontMatter)) {
return $this;
}
$source = $this->readSourceFile();
$parts = preg_split('/[\n]*[-]{3}[\n]/', $source, 2);
if (count($parts) === 2) {
$this->frontMatter = Yaml::load($parts[0]);
$this->template = trim($parts[1]);
} else {
$this->frontMatter = null;
$this->template = trim($source);
}
return $this;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你的问题是你试图将具有三个部分的东西分成两个部分。如果将第三个参数删除到
preg_split
,您将得到一个包含三个元素的数组。第一部分(当由---
分隔时):为空,第二部分是 YAML,第三部分是内容。试试这个:
还有一个实时测试用例: http://ideone.com/LYLxZ
如果你想匹配 Phrozn似乎正在做,那么你的输入将如下所示:
你的 PHP 将是这样的:
以及此版本的实时测试用例: http://ideone.com/a9a6C
I think your problem is that you're trying to split the something with three parts into two parts. If you drop the third argument to
preg_split
, you'll get an array with three elements. The first piece of this (when delimited by---
):Is empty, the second is the YAML, and third is the content. Try this:
And a live test case: http://ideone.com/LYLxZ
If you want to match what Phrozn seems to be doing then your input would look like this:
And your PHP would be this:
And a live test case for this version: http://ideone.com/a9a6C
我遇到了同样的问题,并且对未经测试的正则表达式和可用的稀有软件包非常不满意。
我写了一个库(Composer、TDD、PSR-4)来处理这个问题。该库还处理解析 YAML 和 Markdown:FrontYAML< /strong>
YAML 和 Markdown 解析器可以被覆盖。默认情况下,使用 Symfony YAML 和 Parsedown。
I faced the same problem and was quite unhappy with an untested regex and the rare packages that were available.
I wrote a library (Composer, TDD, PSR-4) to handle that. The library also handles parsing the YAML and the Markdown: FrontYAML
The YAML and Markdown parser can be overridden. By default, Symfony YAML and Parsedown are used.
我是这样做的:
它删除了可能导致解析器出错的无关缩进,并将所有换行符转换为
,无论它们是 Win (CRLF)、Nix (LF) 还是 Mac (CR)” \n"
。I did it thus:
It removes the extraneous indentation one might get which would trip the parser, and it converts all newlines, whether they be Win (CRLF), Nix (LF), or Mac (CR), to just
"\n"
.