将 'require' 传递给 haml 守卫

Passing 'require' to haml guard

手动编译 haml 到 html 需要外部文件的执行方式如下

haml --require .\stuff.rb  --require .\const.rb .\pages\exit.haml .\pages\exit.html

不过,我想改用haml guard。不幸的是,我无法找到正确的标志来让它工作。我需要一些类似的东西:

guard :haml, haml_options: {require: './stuff.rb ./const.rb'}  do
  watch(/^.+(\.haml)$/)
end

结果:

14:19:19 - ERROR - HAML compilation of pages/exit.haml failed! [#] Error: undefined method `html_safe' for nil:NilClass

这表明既没有包含方法也没有包含常量。

有什么想法吗?

编辑: 我正在使用 Ruby 2.3.1p112(2016-04-26 修订版 54768)[x64-mingw32] (Win10),Haml 4.0.7.

最小化示例:

test.haml

!!!
%html
    %header
    %body
        %p
            =$BT_OK.html_safe

const.rb

$BT_OK      = "
".html_safe

helpers.rb

class String
  def html_safe?
    defined?(@html_safe) && @html_safe
  end

  def html_safe
    @html_safe = true
    self
  end
end

require 'haml/helpers/xss_mods'
module Haml::Helpers
  include Haml::Helpers::XssMods
end

使用命令行输出haml .\debug\test.haml .\debug\test.html -r .\const.rb -r .\helpers.rb

<!DOCTYPE html>
<html>
  <header></header>
  <body>
    <p>
      &#13;
    </p>
  </body>
</html>

使用matt的解决方案时出错:

08:02:06 - ERROR - Invalid Guardfile, original error is:
> [#]
> [#] undefined method `html_escape' for module `Haml::Helpers',
> [#] backtrace:
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C
> [#]   (dsl)> C

guard-haml runs Haml “in process”,所以为了让这些文件在您的 Haml 脚本中可用,您需要在 Guardfile 中要求它们。您还需要首先要求 Haml,因为您的助手会引用 Haml 模块中的一些内容:

require 'haml'
require './helpers'
require './const'
guard :haml do
  watch(/^.+(\.haml)$/)
end