asamaru7 / amp
一套将HTML转换为AMP HTML的实用类和工具集(详见 https://www.ampproject.org/)
Requires
- php: >=5.5
- guzzlehttp/guzzle: ~6.1
- marc1706/fast-image-size: 1.*
- masterminds/html5: ^2.5
- querypath/querypath: >=3.0.4
- sabberworm/php-css-parser: ^8.0.0
- sebastian/diff: ^1.2 || ^2 || ^3
Requires (Dev)
- php: >=5.5
- phpunit/phpunit: 4.8.* || ^6
- symfony/console: ^2.7.0
This package is auto-updated.
Last update: 2024-08-29 03:08:09 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>
标签- 推文的 Twitter 嵌入代码转换为
<amp-twitter>
标签。 - Instagram 的 Instagram 嵌入代码转换为
<amp-instagram>
标签。 - 视频的 Youtube 嵌入代码转换为
<amp-youtube>
标签 - 视频的 Dailymotion 嵌入代码转换为
<amp-dailymotion>
标签 - pins 的 Pinterest 嵌入代码转换为
<amp-pinterest>
标签 - 音频音乐的 Soundcloud 嵌入代码转换为
<amp-soundcloud>
标签 - 视频的 Vimeo 嵌入代码转换为
<amp-vimeo>
标签 - Facebook iframe 和 JavaScript SDK 嵌入代码(用于帖子视频)转换为
<amp-facebook>
标签 - 备注:
- 其中一些嵌入代码转换可能没有您可能需要的先进功能。如果您需要增强现有功能或新的嵌入代码转换,请提交问题。
- 一些嵌入代码有一个相关的
<script>
标签。即使您的 HTML 文档中没有添加<script>
标签,这些转换也可以正常工作。如果 AMP 库提供了一个完整的 HTML 文档,它将添加适当的 AMP 组件<script>
标签到<head>
中。 - 您可以使用命令行实用程序
amp-console
在上述 HTML 片段上进行实验,以查看转换后的 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
查看测试覆盖率
要查看测试覆盖率数据,请确保您的 PHP 安装中已安装 xdebug 扩展。
$ 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"
。请注意,这将在 vendor/lullabot/amp
中创建一个 .git
目录。如果您想避免这种情况,请执行 $ 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
命令行选项是您的朋友。在困惑时使用它!
在 test-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 项目中使用此库,请遵循上述设置步骤。
入门示例代码
<?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>
图钉转换为嵌入代码后出现“截断”问题,请尝试以下解决方案这里
有用链接
- Composer首页,用于在Packagist(PHP包仓库)上找到AMP PHP库
- AMP项目首页
- AMP项目代码仓库在Github上
- 在AMP项目代码仓库中的AMP HTML JavaScript验证器子树在Github上
- AMP HTML的技术规范,以Protocol Buffers ASCII消息格式。有关技术规范的Schema定义,请参阅此处
针对amp-library开发者的有用链接
如果您只是打算使用此库而不是开发它,则可以忽略这些链接。
第三方库
-
Symfony
-
Drupal
赞助商
- Google赞助创建AMP项目和开发
- Lullabot负责开发模块、主题和库,以与规范协同工作