TextMate 中的 ⌃⇧H 为“Tidy”; HTML 导致 NoMethodError

发布于 2024-10-26 08:31:52 字数 2125 浏览 2 评论 0原文

昨天我第一次尝试在 HTML 文档中使用“Tidy”,结果...

/tmp/temp_textmate.Z2P0KX:30:in `<main>': undefined method `empty?' for nil:NilClass (NoMethodError)

我没有对包中的代码执行任何操作...

#!/usr/bin/env ruby -wKU

require ENV['TM_SUPPORT_PATH'] + '/lib/ui.rb'
require ENV['TM_SUPPORT_PATH'] + '/lib/exit_codes.rb'

result = `"${TM_TIDY:-tidy}" -f /tmp/tm_tidy_errors -iq -utf8 \
          -wrap 0 --tab-size $TM_TAB_SIZE --indent-spaces $TM_TAB_SIZE \
        --indent yes \
          ${TM_XHTML:+-asxhtml --output-xhtml yes} \
          ${TM_SELECTED_TEXT:+--show-body-only yes} \
          --enclose-text yes \
          --doctype strict \
        --wrap-php no \
          --tidy-mark no`
status = $?.exitstatus

at_exit { File.unlink('/tmp/tm_tidy_errors') } # Clean up error log

if status == 2 # Errors

  msg = "Errors: " + File.read('/tmp/tm_tidy_errors')
  TextMate.exit_show_tool_tip msg

elsif status == 1 # Warnings - use output but also display notification with warnings

  log = File.read('/tmp/tm_tidy_errors').to_a.select do |line|
    ! (ENV['TM_SELECTED_TEXT'] and (line.include?('Warning: missing <!DOCTYPE> declaration') or line.include?("Warning: inserting missing 'title' element")))
  end.join rescue nil

  unless log.empty?
    options = {
      :title   => "Tidy Warnings",
      :summary => "Warnings for tidying your document (press escape to close):",
      :log     => log
    }
    TextMate::UI.simple_notification(options)
  end

end

if ENV['TM_SOFT_TABS'] == "YES"
  print result
else
  in_pre = false
  result.each_line do |line|
    unless in_pre
      tab_size = ENV["TM_TAB_SIZE"].to_i
      space, text = /( *)(.*)/m.match(line)[1..2]
      line = "\t" * (space.length / tab_size).floor + " "  * (space.length % tab_size) + text
    end

    print line

    in_pre = true  if line.include?("<pre>")
    in_pre = false if line.include?("</pre>")
  end
end

问题行是 unless log.empty?.

我在 OS X 10.6.6 上运行 TextMate 1.5.10 (1631)。我最近安装了 rvm 并将默认 Ruby 升级到 1.9.2,但强制 TextMate 使用 1.8.7 并没有解决问题。

I tried using 'Tidy' in an HTML document for the first time yesterday, and got...

/tmp/temp_textmate.Z2P0KX:30:in `<main>': undefined method `empty?' for nil:NilClass (NoMethodError)

I've not done anything to the code in the bundle...

#!/usr/bin/env ruby -wKU

require ENV['TM_SUPPORT_PATH'] + '/lib/ui.rb'
require ENV['TM_SUPPORT_PATH'] + '/lib/exit_codes.rb'

result = `"${TM_TIDY:-tidy}" -f /tmp/tm_tidy_errors -iq -utf8 \
          -wrap 0 --tab-size $TM_TAB_SIZE --indent-spaces $TM_TAB_SIZE \
        --indent yes \
          ${TM_XHTML:+-asxhtml --output-xhtml yes} \
          ${TM_SELECTED_TEXT:+--show-body-only yes} \
          --enclose-text yes \
          --doctype strict \
        --wrap-php no \
          --tidy-mark no`
status = $?.exitstatus

at_exit { File.unlink('/tmp/tm_tidy_errors') } # Clean up error log

if status == 2 # Errors

  msg = "Errors: " + File.read('/tmp/tm_tidy_errors')
  TextMate.exit_show_tool_tip msg

elsif status == 1 # Warnings - use output but also display notification with warnings

  log = File.read('/tmp/tm_tidy_errors').to_a.select do |line|
    ! (ENV['TM_SELECTED_TEXT'] and (line.include?('Warning: missing <!DOCTYPE> declaration') or line.include?("Warning: inserting missing 'title' element")))
  end.join rescue nil

  unless log.empty?
    options = {
      :title   => "Tidy Warnings",
      :summary => "Warnings for tidying your document (press escape to close):",
      :log     => log
    }
    TextMate::UI.simple_notification(options)
  end

end

if ENV['TM_SOFT_TABS'] == "YES"
  print result
else
  in_pre = false
  result.each_line do |line|
    unless in_pre
      tab_size = ENV["TM_TAB_SIZE"].to_i
      space, text = /( *)(.*)/m.match(line)[1..2]
      line = "\t" * (space.length / tab_size).floor + " "  * (space.length % tab_size) + text
    end

    print line

    in_pre = true  if line.include?("<pre>")
    in_pre = false if line.include?("</pre>")
  end
end

The problem line is unless log.empty?.

I'm running TextMate 1.5.10 (1631) on OS X 10.6.6. I recently installed rvm and upgraded default Ruby to 1.9.2, though forcing TextMate to use 1.8.7 did not fix the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女皇必胜 2024-11-02 08:31:53

我也有同样的问题。我已将 Textmate 设置为使用 RVM 版本的 ruby​​,以便我可以快速测试脚本。

我通过取消选中我创建的环境变量的“TM_RUBY”解决了该问题。

似乎发生的情况是,当使用 OSX 附带的 ruby​​ 版本以外的 ruby​​ 版本时,包装 /usr/bin/tidy 命令的 Textmate 脚本无法正确执行。

我很好奇狮子出来后会发生什么。希望 Textmate 能够重新审视这些内置脚本,并对它们进行一些“除尘”。

I had the same problem. I have setup my Textmate to use the RVM version of ruby so that I can quickly test scripts.

I solved the problem by unchecking the "TM_RUBY" for the environment variable I had created.

What appears to be happening is the Textmate scripts that wrapper the /usr/bin/tidy command are not executing properly when using a ruby version other than the one that ships with OSX.

I'm curious to see what happens when Lion comes out. Hopefully, Textmate will take another look at these build-in scripts and give them a little "dusting-off".

明媚殇 2024-11-02 08:31:53

如果您查看 log 的分配,您会看到以下内容:

log = File.read('/tmp/tm_tidy_errors').to_a.select do |line| ... end.join rescue nil

末尾的 rescue nil 会将 nil 放入 如果 /tmp/tm_tidy_errors 文件不存在或无法读取或发生其他情况,则记录。然后脚本将调用 nil 上的 .empty? 方法,但 nil 对象没有这样的方法,脚本崩溃并终止。

您可以通过将 rescue nil 更改为 rescue '' 或将 unless log.empty? 更改为 unless log.nil 来抑制该问题? || log.empty? 但这可能不是真正的问题。

您是否设置了 TM_TIDY 环境变量?您的 PATH 中有 tidy 命令吗?看起来您的 Tidy 安装不正确(或者可能根本不存在)。我的 OSX 有 /usr/bin/tidy ,显然这是标准的。尝试在终端中手动运行大型 tidy 命令,看看会发生什么。

If you look at the assignment to log, you'll see this:

log = File.read('/tmp/tm_tidy_errors').to_a.select do |line| ... end.join rescue nil

The rescue nil at the end will put a nil into log if the /tmp/tm_tidy_errors file isn't there or it can't be read or what ever. Then the script will call the .empty? method on nil but the nil object has no such method and the script falls over and dies.

You can suppress the problem by changing rescue nil to rescue '' or by changing unless log.empty? to unless log.nil? || log.empty? but that might not be the real problem.

Do you have a TM_TIDY environment variable set? Is there a tidy command in your PATH? Looks like your Tidy install isn't right (or possible not there at all). My OSX has /usr/bin/tidy and apparently that's standard. Try running that big tidy command by hand in a terminal and see what happens.

哎呦我呸! 2024-11-02 08:31:53

我也遇到了同样的问题,在一台运行 OS X 10.9.5 的机器上,Ruby 升级到了 ruby​​ 2.0.0。通过将 mu is Too Short 的建议更改为 unless log.empty?unless long.nil? 来修复此问题。 || log.empty?。这使得 Tidy 能够正常运行,但我的 HTML 选择的顶部仍然显示恼人的错误:

ruby: warning: -K is specified; it is for 1.8 compatibility and may cause odd behavior
/Applications/TextMate.app/Contents/SharedSupport/Support/lib/ui.rb:129: warning: assigned but unused variable - pid

我通过更改脚本的第一行来关闭它 #!/usr/bin/env ruby​​ -wKU 到<代码>#!/usr/bin/env ruby​​ -wKU -W0。显然,问题仍然存在,但对于一些有用但不是必需的东西,就像这个功能一样,我认为它已经足够好了。

I had the same problem too, on a machine running OS X 10.9.5 with Ruby upgraded to ruby 2.0.0. Fixed it by taking mu is too short's suggestion to change unless log.empty? to unless long.nil? || log.empty?. That allowed Tidy to run properly, but the top of my HTML selection was still showing me annoying errors:

ruby: warning: -K is specified; it is for 1.8 compatibility and may cause odd behavior
/Applications/TextMate.app/Contents/SharedSupport/Support/lib/ui.rb:129: warning: assigned but unused variable - pid

I shut that up by changing the first line of the script from #!/usr/bin/env ruby -wKU to #!/usr/bin/env ruby -wKU -W0. Obviously the problems are still there under the hood, but for something helpful but not essential, as this functionality is, I think it's plenty good enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文