2

我昨天第一次尝试在 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 并没有解决问题。

4

3 回答 3

2

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

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

似乎正在发生的是包装 /usr/bin/tidy 命令的 Textmate 脚本在使用不同于 OSX 附带的 ruby​​ 版本时无法正确执行。

我很想知道当狮子出来时会发生什么。希望 Textmate 能够再看看这些内置脚本,并给它们一些“除尘”。

于 2011-07-09T17:28:09.833 回答
0

我也遇到了同样的问题,在运行 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。显然问题仍然存在,但是对于一些有用但不是必需的东西,因为这个功能,我认为它已经足够好了。

于 2015-01-14T15:15:30.200 回答
0

如果您查看对 的分配log,您会看到:

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

如果文件不存在或无法读取或其他任何rescue nil情况,最后将放入nil。然后脚本将调用该方法,但对象没有这样的方法,脚本会倒下并死掉。log/tmp/tm_tidy_errors.empty?nilnil

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

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

于 2011-03-25T17:49:58.370 回答