使用 PHP 解析 YAML Front Matter

发布于 2024-11-29 10:45:32 字数 878 浏览 0 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不语却知心 2024-12-06 10:45:33

我认为你的问题是你试图将具有三个部分的东西分成两个部分。如果将第三个参数删除到 preg_split,您将得到一个包含三个元素的数组。第一部分(当由 --- 分隔时):

---
title = A nice title goes here
tags = tag1 tag2 tag3
---
This is the content of this entry...
Line2
Line3

为空,第二部分是 YAML,第三部分是内容。试试这个:

$parts = preg_split('/[\n]*[-]{3}[\n]/', $source, 3);

还有一个实时测试用例: http://ideone.com/LYLxZ

如果你想匹配 Phrozn似乎正在做,那么你的输入将如下所示:

title = A nice title goes here
tags = tag1 tag2 tag3
---
This is the content of this entry...
Line2
Line3

你的 PHP 将是这样的:

$parts = preg_split('/[\n]*[-]{3}[\n]/', $source, 2);

以及此版本的实时测试用例: 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 ---):

---
title = A nice title goes here
tags = tag1 tag2 tag3
---
This is the content of this entry...
Line2
Line3

Is empty, the second is the YAML, and third is the content. Try this:

$parts = preg_split('/[\n]*[-]{3}[\n]/', $source, 3);

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:

title = A nice title goes here
tags = tag1 tag2 tag3
---
This is the content of this entry...
Line2
Line3

And your PHP would be this:

$parts = preg_split('/[\n]*[-]{3}[\n]/', $source, 2);

And a live test case for this version: http://ideone.com/a9a6C

可可 2024-12-06 10:45:33

我遇到了同样的问题,并且对未经测试的正则表达式和可用的稀有软件包非常不满意。

我写了一个库(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.

青巷忧颜 2024-12-06 10:45:33

我是这样做的:

// $string contains the full file.

$split = preg_split("/[\n]*[-]{3}[\n]/", $string, 3, PREG_SPLIT_NO_EMPTY);
try {
  // Strip extra, non-indentation, whitespace from beginning of lines
  $i = 0; $yfm = "";
  while ($split[0][$i] == " ") {$i++;}
  foreach(preg_split("/((\r?\n)|(\r\n?))/", $split[0]) as $line){
    $yfm .= substr($line, $i) . "\n";
  }
  // Using symfony's YAML parser
  $data = sfYaml::load($yfm);
} catch(InvalidArgumentException $e) {
  // This is not YAML
}

它删除了可能导致解析器出错的无关缩进,并将所有换行符转换为 ,无论它们是 Win (CRLF)、Nix (LF) 还是 Mac (CR)” \n"

I did it thus:

// $string contains the full file.

$split = preg_split("/[\n]*[-]{3}[\n]/", $string, 3, PREG_SPLIT_NO_EMPTY);
try {
  // Strip extra, non-indentation, whitespace from beginning of lines
  $i = 0; $yfm = "";
  while ($split[0][$i] == " ") {$i++;}
  foreach(preg_split("/((\r?\n)|(\r\n?))/", $split[0]) as $line){
    $yfm .= substr($line, $i) . "\n";
  }
  // Using symfony's YAML parser
  $data = sfYaml::load($yfm);
} catch(InvalidArgumentException $e) {
  // This is not YAML
}

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文