indieweb/comments

0.2.0 2024-05-12 19:12 UTC

This package is auto-updated.

Last update: 2024-09-12 19:53:05 UTC


README

给定一个 h-entry,返回作者信息以及适合显示的截断帖子文本。

安装

通过composer安装

{
  "indieweb/comments": "0.1.*",
}

或者只需要一个文件

require_once('src/indieweb/comments.php');

用法

该函数接受一个由 microformats2 解析器 返回的格式为PHP的数组,并返回一个看起来如下所示的新数组

原始HTML

<div class="h-entry">
  <div class="p-author h-card">
    <img src="http://aaronparecki.com/images/aaronpk.png" class="u-photo">
    <a href="http://aaronparecki.com">Aaron Parecki</a>
  </div>
  <h3 class="p-name">Example Note</h3>
  <p class="e-content">this text is displayed as the comment</p>
  <time class="dt-published" datetime="2014-02-16T18:48:17-0800">Feb 16, 6:48pm</time>
  <a href="http://caseorganic.com/post/1" class="u-in-reply-to">in reply to caseorganic.com</a>
</div>

解析的微格式

{
    "type": [
        "h-entry"
    ],
    "properties": {
        "author": [
            {
                "type": [
                    "h-card"
                ],
                "properties": {
                    "photo": [
                        "http:\/\/aaronparecki.com\/images\/aaronpk.png"
                    ],
                    "name": [
                        "Aaron Parecki"
                    ],
                    "url": [
                        "http:\/\/aaronparecki.com"
                    ]
                },
                "value": "Aaron Parecki"
            }
        ],
        "name": [
            "Example Note"
        ],
        "published": [
            "2014-02-16T18:48:17-0800"
        ],
        "in-reply-to": [
            "http:\/\/caseorganic.com\/post\/1"
        ],
        "content": [
            {
                "html": "this text is displayed as the comment",
                "value": "this text is displayed as the comment"
            }
        ]
    }
}

解析用于显示评论

$result = IndieWeb\comments\parse($input, $refURL, $maxLength, $maxLines);

结果PHP数组

  $result = array(
    'type' => 'reply',
    'author' => array(
      'name' => 'Aaron Parecki',
      'photo' => 'http://aaronparecki.com/images/aaronpk.png',
      'url' => 'http://aaronparecki.com/'
    ),
    'published' => '2014-02-16T18:48:17-0800',
    'name' => 'Example Note',
    'text' => 'this text is displayed as the comment',
    'url' => 'http://aaronparecki.com/post/1'
  )

此函数将返回包含上述所有键的数组。一个或多个值可能为空,具体取决于帖子中可用的信息,例如作者名称/照片。

text 属性将始终在传递给 parse() 函数的最大所需长度内。

该函数遵循在 comments-presentation 中描述的算法,以决定是否显示 p-namep-summarye-content 属性,并进行适当的截断。

帖子类型

解析器还试图根据主URL确定帖子类型。

将始终返回一个名为 type 的键,其值可以是以下之一

  • 提及 - 默认
  • 回复 - 当帖子包含显式的 in-reply-to 标记时
  • rsvp - 如果帖子包含 RSVP 是/否/可能是的值
  • 喜欢
  • 转发

当类型是 "rsvp" 时,还会设置一个 rsvp 键,其值为 RSVP 的值,通常是 "是"、"否" 或 "可能是"。

帖子名称

如果帖子有一个 "name" 属性,且该属性与内容不同,则它也将包含在解析结果中。这样,调用代码可以选择显示帖子名称的链接,而不是内容。

  $result = array(
    'type' => 'mention',
    'author' => array(
      'name' => 'Aaron Parecki',
      'photo' => 'http://aaronparecki.com/images/aaronpk.png',
      'url' => 'http://aaronparecki.com/'
    ),
    'published' => '2014-02-16T18:48:17-0800',
    'name' => 'Post Name',
    'text' => 'this is the text of the article',
    'url' => 'http://aaronparecki.com/post/1'
  )

测试

请参阅 测试,以获取解析不同帖子的更完整示例。

许可证

版权 2014 年由 Aaron Parecki 拥有

根据 Apache 许可证 2.0 版(“许可证”);除非根据适用的法律或书面同意,否则不得使用此文件,除非遵守许可证。您可以在以下位置获得许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则在许可证下分发的软件按“原样”基础分发,不提供任何明示或暗示的保证或条件。有关许可证的具体语言,请参阅许可证。