使用 Facebook PHP SDK Transformer 从图像中提取标题

Extracting Caption from Image using Facebook PHP SDK Transformer

我在使用 Facebook Instant Articles SDK Transformer 从图像标签中提取属性文本时遇到问题

我无法弄清楚从 alt 属性中提取文本并制作标题所需的 rules.json。

//MARKUP
<img src="https://upload.wikimedia.org/wikipedia/commons/8/84/Example.svg" alt="Foto By: Bla Bla"/>

//RULES.JSON
{
   "class": "ImageRule",
   "selector" : "img",
   "properties" : 
   {
      "image.url" : 
      {
         "type" : "string",
         "selector" : "img",
         "attribute": "src"
      },
      "image.caption" : 
      {
         "type" : "string",
         "selector" : "img",
         "attribute" : "alt"
      }
   }
}

Expected results are Facebook Instant Article compliant markup like:
<figure>
    <img src="https://upload.wikimedia.org/wikipedia/commons/8/84/Example.svg"/>
    <figcaption>Foto By: Bla Bla</figcaption>
</figure>

我得到的是未捕获错误:在第 305 行 /Facebook/InstantArticles/Transformer/Transformer.php 中的字符串上调用成员函数 hasChildNodes()。

图像以某种方式得到处理,标题得到处理,我得到了正确的值,但随后它再次递归地进入传递提取的 "alt" 字符串的转换函数,但它失败了,因为它需要一个 HTML节点输入不是字符串。

Facebook 关于此事的文档非常模糊,所以如果有人有处理 Facebook Instant Articles 的经验,请插话。

糟糕的文档可以在这里找到:
https://developers.facebook.com/docs/instant-articles/sdk/transformer/
https://developers.facebook.com/docs/instant-articles/sdk/transformer-rules

这里是SDK的主要提交者。

您可以检查我们在 SimpleTransformerTest.php 中的设置,它完全满足您的需要。您还可以使用任何测试来玩转 Transformer。

你做错的是 image.caption 的选择器应该是 element.

的类型

对于你的 Rules.json 它应该是这样的:

    {
        "class": "CaptionRule",
        "selector" : "//img[@alt]",
        "properties" : {
            "caption.default": {
                "type": "string",
                "selector": "img",
                "attribute": "alt"
            }
        }
    },
    {
       "class": "ImageRule",
       "selector" : "figure",
       "properties" : 
       {
          "image.url" : 
          {
             "type" : "string",
             "selector" : "img",
             "attribute": "src"
          },
          "image.caption" : 
          {
             "type" : "element",
             "selector" : "img"
          }
       }
    }

检查我是否使用了不同的策略而不是直接进入 ImageRule 上的 <img> 元素,我选择了 <figure> 标签,这样我们就可以保持转换器完好无损. 请注意,rules.json 是自下而上应用的。

让我知道这是否满足您的需要。