HTML 美化器将输入标签放在单独的行上
我必须处理一些看起来非常丑陋的标记,并且我正在 ruby 上通过 Tidy 运行它。在大多数情况下,它工作得很好,除了它将标记中的大量隐藏输入集中到一行上这一事实之外。我知道有一个列换行的设置,但如果它只是将同级输入放在单独的行上会更好。这很重要,因为在查看标记并快速查看这些隐藏输入中的信息时,它可以简化调试。
我还没有找到一个工具可以做到这一点。那么到底是有什么事情还是我很愚蠢呢?
我还应该补充一点,很多问题源于我最初得到的错误标记,并且在它到达我之前我无法采取任何措施来清理它。我尝试 Nokogiri-pretty 来清理它,它非常接近完美,但它将脚本标签变成了自闭合标签,这不好。
现在我正在整理源代码,然后(我知道这很糟糕)gsub(/]*>/, '\0'+"\n")
。我喜欢这样一个事实:我必须将捕获与换行符连接起来。
I have to work with some really ugly looking markup and I am running it through Tidy on ruby. For the most part it works great except for the fact that it lumps a ton of hidden inputs that are in the markup on to one line. I know there is a setting for a column wrap but it would be nicer if it just put sibling inputs on separate lines. It is important because it would simplify debugging when looking at the markup and seeing the info quickly in those hidden inputs.
I have yet to find a tool that does this. So is there anything out there or am I being foolish?
I should also add that a lot of the issues stem from the bad markup I get initially and there is nothing I can do to clean it up before it gets to me. I tried Nokogiri-pretty to clean it up and it was so close to being perfect but it turned script tags in to self closing tags which is no good.
Right now I am settling with Tidying the source and then (I know this is terrible) gsub(/<input[^>]*>/, '\0'+"\n")
. I love the fact that I had to concat the capture with the newline.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Tidy 在 Ruby 中往往会出现问题。据报告存在内存泄漏、不兼容 1.9 等问题。但是,您可以跳过 Tidy完全通过使用 Nokogiri 和
nokogiri-pretty
gem。假设您有一个 Nokogiri 文档:
除了其他整理之外,所有
标签都将独占一行并正确缩进。
Tidy tends to be problematic in Ruby. It has been reported to leak memory, it isn't 1.9 compatible, etc. However, you may be able to skip Tidy altogether by using Nokogiri and the
nokogiri-pretty
gem.Assuming you have a Nokogiri doc:
In addition to other tidying, all
<input>
tags will be on their own line and properly indented.Nokogiri 可以轻松做到这一点:
Nokogiri can do that easy enough: