防止 HTML Tidy 弄乱元标记(架构标记)
Prevent HTML Tidy from messing meta tags ( schema markup )
我在使用 HTML Tidy(最新版本 -- https://html-tidy.org)时遇到严重问题。
简而言之:HTML 整齐地转换这些行 HTML 代码
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
<meta property="position" content="1">
</span>
</div>
进入这些代码行 -- 请仔细查看 META TAGS placement。
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
</span>
<meta property="position" content="1">
</div>
这会导致模式验证出现一些严重问题。您可以在此处查看代码:https://search.google.com/structured-data/testing-tool/u/0/
由于这个问题,客户端的(URL:https://techswami.in)面包屑导航在搜索结果中不可见。
我在美化什么?
我的客户希望我使 his/her 网站的源代码看起来 "clean, readable and tidy"。
所以我使用这些代码行使其适用于 him/her。
注意:此代码在以下 WordPress 设置上 100% 完美运行。
- 带有 FastCGI 的 Nginx Cache/MariaDB
- PHP7
- Ubuntu 18.04.1
- 最新的 WordPress 并与每个缓存插件兼容。
代码:
if( !is_user_logged_in() || !is_admin() ) {
function callback($buffer) {
$tidy = new Tidy();
$options = array('indent' => true, 'markup' => true, 'indent-spaces' => 2, 'tab-size' => 8, 'wrap' => 180, 'wrap-sections' => true, 'output-html' => true, 'hide-comments' => true, 'tidy-mark' => false);
$tidy->parseString("$buffer", $options);
$tidy->cleanRepair();
$buffer = $tidy;
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { if (ob_get_length()) ob_end_flush(); }
add_action('wp_loaded', 'buffer_start');
add_action('shutdown', 'buffer_end');
}
我需要你们提供什么帮助?
你能告诉我如何防止 HTML Tidy 弄乱 META 标签吗?我需要参数。
谢谢。
<meta>
标签只能在父元素中使用:<head>
、<meta charset>
、<meta http-equiv>
此外,<meta>
元素中没有 property
属性。
这些很可能是 HTML-Tidy 清理标记的原因。
来源
首先,我衷心感谢所有试图帮助我的人。
我找到了解决方案,我的解决方案的唯一问题是它没有解决 HTML-Tidy 问题。
所以,现在我没有使用 HTML-Tody,而是使用了这个:https://github.com/ivanweiler/beautify-html/blob/master/beautify-html.php
我的新密码是:
if( !is_user_logged_in() || !is_admin() ) {
function callback($buffer) {
$html = $buffer;
$beautify = new Beautify_Html(array(
'indent_inner_html' => false,
'indent_char' => " ",
'indent_size' => 2,
'wrap_line_length' => 32786,
'unformatted' => ['code', 'pre'],
'preserve_newlines' => false,
'max_preserve_newlines' => 32786,
'indent_scripts' => 'normal' // keep|separate|normal
));
$buffer = $beautify->beautify($html);
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { if (ob_get_length()) ob_end_flush(); }
add_action('wp_loaded', 'buffer_start');
add_action('shutdown', 'buffer_end');
}
现在所有与架构标记相关的问题都已得到修复,客户站点的源代码也得到了美化。
只是为了透视,我尝试基于以下内容实现一个最小的独立示例:
- 你的代码
- 整洁的示例由 PHP reference Tidy examples
提供
我得到了以下代码:
<?php
ob_start();
?>
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
<meta property="position" content="1">
</span>
</div>
</div>
<?php
$buffer = ob_get_clean();
$tidy = new Tidy();
$options = array(
'indent' => true,
'markup' => true,
'indent-spaces' => 2,
'tab-size' => 8,
'wrap' => 180,
'wrap-sections' => true,
'output-html' => true,
'hide-comments' => true,
'tidy-mark' => false
);
$tidy->parseString("$buffer", $options);
$tidy->cleanRepair();
echo $tidy;
?>
关于 Tidy 如何重组您的 HTML 的输出信息非常丰富。开始了:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta property="position" content="1">
<title></title>
</head>
<body>
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class=
"taxonomy category"><span property="name">Codes</span></a> </span>
</div>
</div>
</body>
</html>
元标记并没有消失,而是被推到了它应该属于的地方,正如其他评论者所指出的那样。
如果您希望 Tidy 只处理 HTML 结构,请添加选项 'input-xml' 并将其设置为 true,例如:
$options = array(
'indent' => true,
'markup' => true,
'indent-spaces' => 2,
'tab-size' => 8,
'wrap' => 180,
'wrap-sections' => true,
'output-html' => true,
'hide-comments' => true,
'tidy-mark' => false,
'input-xml' => true
);
这将输出以下内容:
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
<meta property="position" content="1"></meta>
</span>
</div>
</div>
我在使用 HTML Tidy(最新版本 -- https://html-tidy.org)时遇到严重问题。
简而言之:HTML 整齐地转换这些行 HTML 代码
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
<meta property="position" content="1">
</span>
</div>
进入这些代码行 -- 请仔细查看 META TAGS placement。
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
</span>
<meta property="position" content="1">
</div>
这会导致模式验证出现一些严重问题。您可以在此处查看代码:https://search.google.com/structured-data/testing-tool/u/0/
由于这个问题,客户端的(URL:https://techswami.in)面包屑导航在搜索结果中不可见。
我在美化什么?
我的客户希望我使 his/her 网站的源代码看起来 "clean, readable and tidy"。
所以我使用这些代码行使其适用于 him/her。
注意:此代码在以下 WordPress 设置上 100% 完美运行。
- 带有 FastCGI 的 Nginx Cache/MariaDB
- PHP7
- Ubuntu 18.04.1
- 最新的 WordPress 并与每个缓存插件兼容。
代码:
if( !is_user_logged_in() || !is_admin() ) {
function callback($buffer) {
$tidy = new Tidy();
$options = array('indent' => true, 'markup' => true, 'indent-spaces' => 2, 'tab-size' => 8, 'wrap' => 180, 'wrap-sections' => true, 'output-html' => true, 'hide-comments' => true, 'tidy-mark' => false);
$tidy->parseString("$buffer", $options);
$tidy->cleanRepair();
$buffer = $tidy;
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { if (ob_get_length()) ob_end_flush(); }
add_action('wp_loaded', 'buffer_start');
add_action('shutdown', 'buffer_end');
}
我需要你们提供什么帮助?
你能告诉我如何防止 HTML Tidy 弄乱 META 标签吗?我需要参数。
谢谢。
<meta>
标签只能在父元素中使用:<head>
、<meta charset>
、<meta http-equiv>
此外,<meta>
元素中没有 property
属性。
这些很可能是 HTML-Tidy 清理标记的原因。
来源
首先,我衷心感谢所有试图帮助我的人。
我找到了解决方案,我的解决方案的唯一问题是它没有解决 HTML-Tidy 问题。
所以,现在我没有使用 HTML-Tody,而是使用了这个:https://github.com/ivanweiler/beautify-html/blob/master/beautify-html.php
我的新密码是:
if( !is_user_logged_in() || !is_admin() ) {
function callback($buffer) {
$html = $buffer;
$beautify = new Beautify_Html(array(
'indent_inner_html' => false,
'indent_char' => " ",
'indent_size' => 2,
'wrap_line_length' => 32786,
'unformatted' => ['code', 'pre'],
'preserve_newlines' => false,
'max_preserve_newlines' => 32786,
'indent_scripts' => 'normal' // keep|separate|normal
));
$buffer = $beautify->beautify($html);
return $buffer;
}
function buffer_start() { ob_start("callback"); }
function buffer_end() { if (ob_get_length()) ob_end_flush(); }
add_action('wp_loaded', 'buffer_start');
add_action('shutdown', 'buffer_end');
}
现在所有与架构标记相关的问题都已得到修复,客户站点的源代码也得到了美化。
只是为了透视,我尝试基于以下内容实现一个最小的独立示例:
- 你的代码
- 整洁的示例由 PHP reference Tidy examples 提供
我得到了以下代码:
<?php
ob_start();
?>
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
<meta property="position" content="1">
</span>
</div>
</div>
<?php
$buffer = ob_get_clean();
$tidy = new Tidy();
$options = array(
'indent' => true,
'markup' => true,
'indent-spaces' => 2,
'tab-size' => 8,
'wrap' => 180,
'wrap-sections' => true,
'output-html' => true,
'hide-comments' => true,
'tidy-mark' => false
);
$tidy->parseString("$buffer", $options);
$tidy->cleanRepair();
echo $tidy;
?>
关于 Tidy 如何重组您的 HTML 的输出信息非常丰富。开始了:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<meta property="position" content="1">
<title></title>
</head>
<body>
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem"><a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class=
"taxonomy category"><span property="name">Codes</span></a> </span>
</div>
</div>
</body>
</html>
元标记并没有消失,而是被推到了它应该属于的地方,正如其他评论者所指出的那样。
如果您希望 Tidy 只处理 HTML 结构,请添加选项 'input-xml' 并将其设置为 true,例如:
$options = array(
'indent' => true,
'markup' => true,
'indent-spaces' => 2,
'tab-size' => 8,
'wrap' => 180,
'wrap-sections' => true,
'output-html' => true,
'hide-comments' => true,
'tidy-mark' => false,
'input-xml' => true
);
这将输出以下内容:
<div class="breadcrumbs" typeof="BreadcrumbList" vocab="http://schema.org/">
<div class="wrap">
<span property="itemListElement" typeof="ListItem">
<a property="item" typeof="WebPage" title="Codes Category" href="https://mysite.works/codes/" class="taxonomy category">
<span property="name">Codes</span>
</a>
<meta property="position" content="1"></meta>
</span>
</div>
</div>