使用 Perl 进行 HTML 缩进
我有一个带有 HTML 代码的变量(例如 $content)(没有换行符 - 之前已删除)。如何处理 HTML 代码,在每个开始标签后添加 TAB 缩进并在每个结束标签后减少缩进级别?
PS 我不需要外部脚本或程序(比如 tidy)。我需要用我自己的脚本来做这个。
例如: 来源内容:
<!DOCTYPE html><html><head><title>test</title></head> <body> <h1>hello!</h1><p>It works!</p></body></html>
所需结果:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>hello!</h1>
<p>It works!</p>
</body>
</html>
I have a variable (ex. $content) with HTML code (without line breaks - removed before). How to process HTML code with adding TAB indent after each open tag and decrease indent level after each closing tag?
P.S. I don't need external script or programm (like tidy). I need to make this in my own script.
For example:
source content:
<!DOCTYPE html><html><head><title>test</title></head> <body> <h1>hello!</h1><p>It works!</p></body></html>
needed result:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
</head>
<body>
<h1>hello!</h1>
<p>It works!</p>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
手册页说
tidy
不会产生以下输出:包含选项卡。但通过对输出进行后处理来处理这个问题就足够简单了。使用现有工具肯定是比自己发明更好的解决方案。但如果您坚持,那么您至少应该使用预先编写的解析器,例如 Perl 的 HTML::Parser。
我还应该指出,您对问题的说明似乎不正确。您说您想在每个开始标签后添加一个选项卡。但是您的示例输出并未对
The manual page says that
tidy
won't produce output that contains tabs. But it's simple enough to post-process the output to deal with that.Using an existing tool has to be a far better solution than inventing it yourself. But if you insist then you should, at least, use a pre-written parser like Perl's HTML::Parser.
I should also point out that your specification of the problem seems to be incorrect. You say you want to add a tab after each opening tag. But your sample output doesn't do that for the <title>, <h1> or &p> tags.
我使用的选项是 CGI::Pretty< /a>.
An option I've used is CGI::Pretty.
您还可以尝试 Marpa::R2::HTML 参考其同伴的来源/demo 实用程序 html_fmt 查看如何针对文档的特定部分进行操作。我还没有使用过它,今天也无法尝试,因为需要 5.10,但看起来它可能是一个很好的匹配。
You could also try Marpa::R2::HTML referring to the source of its companion/demo utility html_fmt to see how to target specific parts of the document for manipulation. I haven't used it and can't try today for want of 5.10 but it looks like it could be a good match.