【问题标题】:Are there JavaScript or Ruby versions of "HTML tidy"? [closed]是否有 JavaScript 或 Ruby 版本的“HTML tidy”? [关闭]
【发布时间】:2011-05-13 12:15:54
【问题描述】:

是否存在类似于 HTML tidy (http://tidy.sourceforge.net/) 的库,它不是特定于操作系统的(需要在每个主机上编译)。基本上我只想验证/清理用户发送给我的 HTML。

<p>hello</p></p><br>

应该变成

<p>hello</p>
<br/>

javascript 或 ruby​​ 中的某些东西对我有用。 谢谢!

【问题讨论】:

    标签: javascript jquery ruby-on-rails ruby tidy


    【解决方案1】:

    在 Ruby 中,您可以在 Nokogiri 中解析 HTML,这将让您检查错误,然后让它输出 HTML,这将清除丢失的结束标签等。注意在下面的 HTML 中,title 和 p 标签没有正确关闭,但是 Nokogiri 添加了结束标签。

    require 'nokogiri'
    
    html = '<html><head><title>the title</head><body><p>a paragraph</body></html>'
    doc = Nokogiri::HTML(html)
    puts "Errors found" if (doc.errors.any?)
    puts doc.to_html
    # >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
    # >> <html>
    # >> <head>
    # >> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    # >> <title>the title</title>
    # >> </head>
    # >> <body><p>a paragraph</p></body>
    # >> </html>
    

    或者,您可以打开到/usr/bin/tidy 的连接并告诉它做脏活:

    require 'open3'
    
    html = '<html><head><title>the title</head><body><p>a paragraph</body></html>'
    
    stdin, stdout, stderr = Open3.popen3('/usr/bin/tidy -qi')
    stdin.puts html
    stdin.close
    puts stdout.read
    # >> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
    # >> 
    # >> <html>
    # >> <head>
    # >>   <meta name="generator" content=
    # >>   "HTML Tidy for Mac OS X (vers 31 October 2006 - Apple Inc. build 15.3.6), see www.w3.org">
    # >> 
    # >>   <title>the title</title>
    # >> </head>
    # >> 
    # >> <body>
    # >>   <p>a paragraph</p>
    # >> </body>
    # >> </html>
    

    【讨论】:

    • Loofah 是另一个建立在 Nokogiri 上的宝石,它可以进行消毒。
    • sanitize gem (github.com/rgrove/sanitize) 使用 nokogiri 并且有效(谢谢!)。有谁知道可以在客户端为他们提供即时反馈的东西。
    • 当您说“即时反馈”时,您的设想是什么?他们的浏览器中的即时更新?那将是一个 AJAX 调用来更新 &lt;div&gt; 块的内容。
    • true 但这将包括将输入保存回服务器,然后再次渲染(例如,不可能在每次击键时更新)。如果真的没有 javascript sanitizers 我会在 AJAX 中实现一些东西。谢谢
    【解决方案2】:

    你以前检查过这个吗? http://tidy.rubyforge.org/

    【讨论】:

    • 是的,但问题在于:Tidy.path = '/usr/lib/tidylib.so' 需要为主机编译
    【解决方案3】:

    html-tidy 已编译为 javascript(使用 emscripten)。

    查看the demo并下载tidy.js

    如果你足够勇敢,你可以自己编译成 javascript,有你想要的选项。见https://github.com/lovasoa/tidy-html5

    【讨论】:

      【解决方案4】:

      有一个 java 端口 JTidy 但我不知道其他端口,可能有某种方法可以从 Ruby 调用 HTML tidy 对你有用,prahaps 在命令行上从你的 ruby​​ webapp 调用 html tidy 应用程序.

      【讨论】:

      • 是的,我知道!我只是提到它是我所知道的唯一港口。
      • 不幸的是,我无法在我的主机上安装 java(否则可以工作),但谢谢
      【解决方案5】:

      W3 Validator 对你有用吗?

      或者你想要一些东西来修复错误?

      【讨论】:

        【解决方案6】:

        如果你只是想要一个美化器,请使用 Pretty Diff。

        http://prettydiff.com/?m=beautify&html

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-04-29
          • 1970-01-01
          • 2011-01-19
          • 1970-01-01
          • 1970-01-01
          • 2010-09-28
          • 1970-01-01
          • 2012-04-10
          相关资源
          最近更新 更多