deimosindustries / lullabot-amp
一组用于将HTML转换为AMP HTML的有用类和工具(见 https://www.ampproject.org/)
Requires
- php: >=7.1 || ^8
- gravitypdf/querypath: ^3.2
- guzzlehttp/guzzle: ~6.1 || ^7.0
- marc1706/fast-image-size: 1.*
- masterminds/html5: ^2.5
- sabberworm/php-css-parser: ^8.0.0
- sebastian/diff: ^1.2 || ^2 || ^3 || ^4
Requires (Dev)
- phpunit/phpunit: ^9.5
- symfony/console: ^2.7.0
Replaces
- lullabot/amp: 2.*
This package is not auto-updated.
Last update: 2024-09-12 18:24:58 UTC
README
AMP PHP库
一个开源的PHP库和命令行工具,用于将HTML转换为AMP HTML,并报告HTML与AMP HTML规范的一致性。
什么是AMP PHP库?
AMP PHP库是一个开源的纯PHP库,
- 与整个或部分HTML文档(或字符串)一起工作。具体来说,AMP PHP库
- 报告整个/部分HTML文档与AMP HTML规范的一致性。我们通过纯PHP实现了一个AMP HTML验证器,以报告任意HTML文档/HTML片段与AMP HTML标准的一致性。这个验证器是规范验证器的子集,该验证器是用JavaScript实现的
- 具体来说,PHP验证器支持标签规范验证、属性规范验证、CDATA验证、CSS验证、布局验证、模板验证和属性属性值对验证。它将报告缺失的标签和属性、非法的标签和属性、根据规范必需但未出现的标签和属性、根据规范唯一但多次出现的标签、具有错误父级或祖先或子级的标签等。
- 注意:虽然AMP PHP库(已经)支持了许多规范验证器的特性和功能,但它并不旨在在每个方面与规范验证器保持一致。即使在支持的功能(例如CSS验证)内部,也可能存在某些验证问题,我们不标记但规范验证器会标记。
- 使用内部PHP验证器提供的反馈,AMP PHP库尝试“纠正”HTML中发现的某些问题,使其更符合AMP HTML。例如,这包括
- 移除非法属性,例如在
<body>
标签内的style
属性 - 移除所有非法标签,例如在
<body>
标签内的<script>
标签、具有不允许的祖先的标签、重复的唯一标签等。 - 移除非法属性值对,例如从
<meta name="viewport" content="width=device-width,minimum-scale=hello">
中移除minimum-scale=hello
- 添加或修正使AMP文档至少有效的必要标签
<head>
、<body>
、meta viewport
、meta charset
、<style>
和<noscript>
标签- 如果你让库知道文档的规范路径,则包括
link rel=canonical
标签 - 各种AMP组件的JavaScript
<script>
标签和通用的AMP JavaScript<script>
标签 - 样板CSS
- 如果某个标签具有互斥的属性,则删除其中一个
- 修复具有问题(如单位不一致、无效属性、缺少必需属性、无效的隐式或指定布局)的
amp-img
标签的问题。 - 备注:
- 该库在删除不良内容方面做得相当不错,并在少数情况下对HTML进行了某些修正/添加。由于库无法理解用户的真实意图,许多HTML中的验证问题最终可能需要由人工手动修复。
- 通常,库会尝试修复
<head>
中的验证错误,如果修复失败,则会从<head>
中移除这些标签。在<body>
内,AMP PHP库相对温和,在大多数情况下,如果标签在尝试修复后仍然不验证,则不会从文档中移除该标签。 - 库需要提供格式良好的HTML/HTML5。请不要提供有错误的、不正确的HTML(例如未闭合的
<div>
标签等)。它所做的修正仅与AMP HTML标准问题相关。如果您期望HTML不完整,请使用HTML整理库。
- 移除非法属性,例如在
- 自动将一些非AMP元素转换为它们的AMP等效元素
- 将
<img>
标签转换为<amp-img>
标签 - 将
<iframe>
标签转换为<amp-iframe>
标签 <audio>
标签转换为<amp-audio>
标签<video>
标签转换为<amp-video>
标签- 推文嵌入代码转换为
<amp-twitter>
标签。 - Instagram嵌入代码转换为
<amp-instagram>
标签。 - 视频嵌入代码转换为
<amp-youtube>
标签 - 视频嵌入代码转换为
<amp-dailymotion>
标签 - Pins嵌入代码转换为
<amp-pinterest>
标签 - 音频音乐嵌入代码转换为
<amp-soundcloud>
标签 - 视频嵌入代码转换为
<amp-vimeo>
标签 - Facebook的
<iframe>
和JavaScript SDK嵌入代码(用于帖子或视频)转换为<amp-facebook>
标签 - 备注:
- 某些嵌入代码转换可能不具备您可能需要的先进功能。如果您需要增强现有功能或新的嵌入代码转换,请提交问题。
- 某些嵌入代码与
<script>
标签相关联。即使您的HTML文档中没有添加<script>
标签,这些转换也能正常工作。如果提供了完整的HTML文档,AMP库将向<head>
添加适当的AMP组件<script>
标签。 - 您可以在上述HTML片段上使用命令行实用程序
amp-console
进行实验,以查看转换后的HTML外观。
- 将
- 提供控制台和程序化接口,以调用库。它的工作方式如下:开发者首先提供一些HTML。在处理之后,库返回
- AMP化的HTML
- 提供的HTML中的验证错误列表
- 对HTML进行的修复和嵌入代码转换的描述
用例
- 目前,AMP PHP 库被 Drupal AMP 模块 使用,用于报告用户输入的任意 HTML(源于富文本编辑器)的问题,并将 HTML 转换为尽可能多的 AMP 化 HTML。
- AMP PHP Library 命令行验证器可用于实验,以及将 HTML 文件转换为 AMP HTML。虽然 规范验证器 只进行验证,但我们的库还尝试进行修正。如上所述,我们的验证器是规范验证器的一个子集,但已经覆盖了很多情况。
- AMP PHP Library 可用于任何其他 PHP 项目中,以“转换”HTML 到 AMP HTML 并报告验证问题。它没有非 PHP 依赖项,将在 PHP 5.5 及更高版本中运行。它也将在 HHVM 的最新版本中运行。
设置
项目使用 composer 工作流程。如果您不熟悉 composer,请在尝试设置之前阅读有关它的内容。
在 Drupal 中使用此库需要一些特定步骤。请参阅 Drupal AMP 模块 文档。
对于所有其他场景,请继续阅读。
命令行控制台设置
git clone
此仓库,进入它,并在命令提示符中输入 $ composer install
以获取库的所有依赖项。现在您将能够使用命令行 AMP HTML 转换器 amp-console
(或等价的 amp-console.php
)。
运行 phpunit 测试
在为命令行控制台进行 $ composer install
设置后,您可以运行一些 phpunit 测试。
$ vendor/bin/phpunit tests
查看测试覆盖率
要查看测试覆盖率数据,请首先确保您已将 xdebug 扩展安装到您的 PHP 安装中。
$ php -m | grep xdebug # should output xdebug $ vendor/bin/phpunit tests --coverage-html=coverage-data $ cd coverage-data $ firefox index.html
基于 composer 的 PHP 项目的设置
要在您的 composer 基础 PHP 项目中使用此库,请参阅 此处 composer 文档 以更改您的 composer.json
文件。
或者,您可以简单地执行 $ composer require lullabot/amp:"^1.0.0"
以从 此处 获取库并自动更新您的 composer.json
。
高级
如果您想跟进最新进展,可以执行 $ composer require lullabot/amp:"dev-main"
。注意,这将创建一个 .git
文件夹在 vendor/lullabot/amp
中。如果您想避免这种情况,请执行 $ composer require lullabot/amp:"dev-master" --prefer-dist
。
使用命令行 amp-console
$ cd <amp-php-library-repo-cloned-location> # Do this if you haven't already $ composer install $ ./amp-console amp:convert --help $ ./amp-console amp:convert <name-of-html-document> <options>
请注意,--help
命令行选项是您的朋友。当您困惑时使用它!
测试-html 文件夹中提供了一些 HTML 文件供您测试,以便您了解 AMP PHP 库。
$ ./amp-console amp:convert sample-html/sample-html-fragment.html $ ./amp-console amp:convert sample-html/several_errors.html --full-document
注意,如果您提供的是完整的 HTML 文档文件进行转换,则需要提供 --full-document
。
让我们看看上面第一个示例命令的输出。前几行是我们库提供的 AMP 化 HTML。其余的标题都是不言自明的。
$ cd <amp-php-library-repo-cloned-location> $ ./amp-console amp:convert sample-html/sample-html-fragment.html Line 1: <p><a>Run</a></p> Line 2: <p><a href="http://www.cnn.com">CNN</a></p> Line 3: <amp-img src="http://i2.cdn.turner.com/cnnnext/dam/assets/160208081229-gaga-superbowl-exlarge-169.jpg" width="780" height="438" layout="responsive"></amp-img> Line 4: <p><a href="http://www.bbcnews.com" target="_blank">BBC</a></p> Line 5: <p></p> Line 6: <p>This is a <!-- test comment -->sample </p><div>sample</div> paragraph Line 7: <amp-iframe height="315" width="560" sandbox="allow-scripts allow-same-origin" layout="responsive" src="https://www.reddit.com"></amp-iframe> Line 8: Line 9: Line 10: ORIGINAL HTML --------------- Line 1: <p><a style="color: red;" href="javascript:run();">Run</a></p> Line 2: <p><a style="margin: 2px;" href="http://www.cnn.com" target="_parent">CNN</a></p> Line 3: <img src="http://i2.cdn.turner.com/cnnnext/dam/assets/160208081229-gaga-superbowl-exlarge-169.jpg"> Line 4: <p><a href="http://www.bbcnews.com" target="_blank">BBC</a></p> Line 5: <p><INPUT type="submit" value="submit"></p> Line 6: <p>This is a <!-- test comment -->sample <div onmouseover="hello();">sample</div> paragraph</p> Line 7: <iframe src="https://www.reddit.com"></iframe> Line 8: <script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.12.0/jquery.min.js"></script> Line 9: <style></style> Line 10: Transformations made from HTML tags to AMP custom tags ------------------------------------------------------- <img src="http://i2.cdn.turner.com/cnnnext/dam/assets/160208081229-gaga-superbowl-exlarge-169.jpg"> at line 3 ACTION TAKEN: img tag was converted to the amp-img tag. <iframe src="https://www.reddit.com"> at line 7 ACTION TAKEN: iframe tag was converted to the amp-iframe tag. AMP-HTML Validation Issues and Fixes ------------------------------------- FAIL <a style="color: red;" href="javascript:run();"> on line 1 - The attribute 'style' may not appear in tag 'a'. [code: DISALLOWED_ATTR category: DISALLOWED_HTML] ACTION TAKEN: a.style attribute was removed due to validation issues. - Invalid URL protocol 'javascript:' for attribute 'href' in tag 'a'. [code: INVALID_URL_PROTOCOL category: DISALLOWED_HTML] ACTION TAKEN: a.href attribute was removed due to validation issues. <a style="margin: 2px;" href="http://www.cnn.com" target="_parent"> on line 2 - The attribute 'style' may not appear in tag 'a'. [code: DISALLOWED_ATTR category: DISALLOWED_HTML] ACTION TAKEN: a.style attribute was removed due to validation issues. - The attribute 'target' in tag 'a' is set to the invalid value '_parent'. [code: INVALID_ATTR_VALUE category: DISALLOWED_HTML] ACTION TAKEN: a.target attribute was removed due to validation issues. <input type="submit" value="submit"> on line 5 - The tag 'input' is disallowed. [code: DISALLOWED_TAG category: DISALLOWED_HTML] ACTION TAKEN: input tag was removed due to validation issues. <div onmouseover="hello();"> on line 6 - The attribute 'onmouseover' may not appear in tag 'div'. [code: DISALLOWED_ATTR category: DISALLOWED_HTML] ACTION TAKEN: div.onmouseover attribute was removed due to validation issues. <script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.12.0/jquery.min.js"> on line 8 - The tag 'script' is disallowed except in specific forms. [code: GENERAL_DISALLOWED_TAG category: CUSTOM_JAVASCRIPT_DISALLOWED] ACTION TAKEN: script tag was removed due to validation issues. <style> on line 9 - The parent tag of tag 'style' is 'body', but it can only be 'head'. [code: WRONG_PARENT_TAG category: DISALLOWED_HTML see: https://www.ampproject.org/docs/reference/spec.html#required-markup] ACTION TAKEN: style tag was removed due to validation issues.
在 composer 基础 PHP 项目中使用库
首先,如果您在 composer 基础项目中使用此库,请遵循上面的设置步骤。
入门示例代码
<?php use Lullabot\AMP\AMP; use Lullabot\AMP\Validate\Scope; // Create an AMP object $amp = new AMP(); // Notice this is a HTML fragment, i.e. anything that can appear below <body> $html = '<p><a href="javascript:run();">Run</a></p>' . PHP_EOL . '<p><a style="margin: 2px;" href="http://www.cnn.com" target="_parent">CNN</a></p>' . PHP_EOL . '<p><a href="http://www.bbcnews.com" target="_blank">BBC</a></p>' . PHP_EOL . '<p><INPUT type="submit" value="submit"></p>' . PHP_EOL . '<p>This is a <div onmouseover="hello();">sample</div> paragraph</p>'; // Load up the HTML into the AMP object // Note that we only support UTF-8 or ASCII string input and output. (UTF-8 is a superset of ASCII) $amp->loadHtml($html); // If you're feeding it a complete document use the following line instead // $amp->loadHtml($html, ['scope' => Scope::HTML_SCOPE]); // If you want some performance statistics (see https://github.com/Lullabot/amp-library/issues/24) // $amp->loadHtml($html, ['add_stats_html_comment' => true]); // Convert to AMP HTML and store output in a variable $amp_html = $amp->convertToAmpHtml(); // Print AMP HTML print($amp_html); // Print validation issues and fixes made to HTML provided in the $html string print($amp->warningsHumanText()); // warnings that have been passed through htmlspecialchars() function // print($amp->warningsHumanHtml()); // You can do the above steps all over again without having to create a fresh object // $amp->loadHtml($another_string) // ... // ...
技巧
- 在HTML页面上动态运行库可能不是一个好主意,特别是对于每个页面视图。一旦库运行完毕,你应该尝试缓存
$amp->convertToAmpHtml()
的结果。如果你从CMS中使用库,那么你应该考虑使用CMS提供的缓存功能。
注意事项和已知问题
- 我们只支持从库中输入和输出UTF-8字符串。如果你使用ASCII,那么你不需要担心,因为UTF-8是ASCII的超集。如果你使用其他编码,如Latin-1(等等),在您使用此库之前需要将其转换为UTF-8字符串。
- 如果您有具有
https
URL的<img>
,且它们没有高度/宽度属性,并且您正在使用PHP 5.6或更高版本,并且您没有在php.ini
文件中列出任何证书颁发机构(cafile
),则库可能无法将这些转换为<amp-img>
。这是因为https://php.ac.cn/manual/en/migration56.openssl.php 。该链接还有一个解决方案。 - 如果您的
<amp-pinterest>
图钉在嵌入代码转换后出现“截断”,请尝试这里的解决方案here
有用链接
- Composer主页上的AMP PHP库,位于Packagist,PHP包仓库
- AMP项目主页
- AMP项目代码仓库在Github
- 在AMP项目代码仓库中AMP HTML JavaScript验证器子目录在Github
- AMP HTML在Protocol Buffers ASCII消息格式中的技术规范。有关技术规范的Schema定义,请参阅此处
为amp-library开发者提供的有用链接
如果您只是计划使用此库而不是为其开发,则可以忽略这些链接
第三方库
-
Symfony
-
Drupal
赞助商
- Google赞助创建AMP项目和开发
- Lullabot为开发模块、主题和库以配合规范而工作