将 HAML 与自定义过滤器结合使用

发布于 2024-10-09 04:10:26 字数 1197 浏览 1 评论 0原文

我对 HAML 和 CoffeeScript 感到非常兴奋,并且正在研究如何在非 Rails 环境中使用它们的教程。 因此,haml 具有易于使用的命令行实用程序

haml input.haml output.html.

而且,最棒的是,存在一个项目(许多分支之一:https://github.com/aussiegeek/coffee-haml-filter)旨在提供自定义过滤器,将 CoffeeScript 转换为 HAML 文件内的 JS。 不幸的是(或者我错过了什么?) haml 不允许在命令行或某些配置文件中指定自定义过滤器。

我(不是 Ruby 粉丝,甚至不是足够了解它)设法用这个辅助脚本解决了这个问题(基于 SO 上某处的一些巧妙建议): haml.rb

require 'rubygems'
require 'active_support/core_ext/object/blank'
require 'haml'
require 'haml/filters/coffee'

template = ARGV.length > 0 ? File.read(ARGV.shift) : STDIN.read
haml_engine = Haml::Engine.new(template)
file = ARGV.length > 0 ? File.open(ARGV.shift, 'w') : STDOUT
file.write(haml_engine.render)
file.close

这非常简单,除了开头的 require 之外。

现在的问题是:

1)我真的应该使用它,还是有另一种方法可以使用自定义过滤器按需将 HAML 编译为 HTML?

2)HAML监视模式怎么样?非常棒而且方便。当然,我可以在 python 中创建一个轮询脚本来监视目录更改并调用此 .rb 脚本,但它看起来像是一个肮脏的解决方案。

除了Heikki的回复之外,我的解决方案如下: https://gist.github.com/759002

如果您觉得有用,请随意使用

I feel quite excited about HAML and CoffeeScript and am working on tutorial showing how to use them in non-Rails environment.
So, haml has easy to use command-line utility

haml input.haml output.html.

And, what is great, there exist a project (one of many forks: https://github.com/aussiegeek/coffee-haml-filter) aimed at providing custom filter that converts CoffeeScript into JS inside of HAML files.
Unfortunately (or am I missing something?) haml doesn't allow specifying custom filters on the command line or with some configuration file.

I (not being a Ruby fan or even knowing it enough) managed to solve it (based on some clever suggestion somewhere on SO) with this helper script:
haml.rb

require 'rubygems'
require 'active_support/core_ext/object/blank'
require 'haml'
require 'haml/filters/coffee'

template = ARGV.length > 0 ? File.read(ARGV.shift) : STDIN.read
haml_engine = Haml::Engine.new(template)
file = ARGV.length > 0 ? File.open(ARGV.shift, 'w') : STDOUT
file.write(haml_engine.render)
file.close

Which is quite straightforward, except of requires in the beginning.

Now, the questions are:

1) should I really use it, or is there another way to have on-demand HAML to HTML compilation with custom filters?

2) What about HAML watch mode? It's great and convenient. I can, of course, create a polling script in python that will watch the directory changes and call this .rb script, but it looks like a dirty solution.

In addition to reply by Heikki, my solution follows:
https://gist.github.com/759002

Feel free to use, if you find it useful

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

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

发布评论

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

评论(3

感受沵的脚步 2024-10-16 04:10:26

好消息:有一个新的 CoffeeScript Haml 过滤器,设计为独立于 Rails 工作!

https://github.com/paulnicholson/coffee-filter

绝对推荐使用 Coffee-haml-filter ,从来没有真正积极维护过。

Good news: There's a new CoffeeScript Haml filter that's designed to work independently of Rails!

https://github.com/paulnicholson/coffee-filter

Definitely recommended over coffee-haml-filter, which was never really actively maintained.

中性美 2024-10-16 04:10:26

1)我会说是的。 (我在命令行选项方面也没有运气)

2)我得到了这个使用咖啡脚本过滤器的示例。文件监视是通过 fssm gem 完成的。它在输入文件夹中递归地跟踪 HAML 文件的更改,并将它们呈现到具有 .html 文件扩展名的输出文件夹。

watch.rb:

require 'rubygems'
require 'fssm'
require 'haml'
require 'coffee-haml-filter'
require 'active_support/core_ext/object/blank'

def render(input_dir, output_dir, relative)
  input_path = File.join(input_dir, relative)
  output_path = File.join(output_dir, relative).gsub(/\.haml$/, ".html")
  haml_engine = Haml::Engine.new(File.read(input_path))
  puts "Rendering #{input_path} -> #{output_path}"
  FileUtils.makedirs(File.dirname(output_path))
  File.open(output_path, 'w') do |file|
    file.write(haml_engine.render)
  end
end

input_dir = File.expand_path(ARGV.length > 0 ? ARGV.shift : '.')
output_dir = File.expand_path(ARGV.length > 0 ? ARGV.shift : input_dir)

puts "Input folder:  '#{input_dir}'"
puts "Output folder: '#{output_dir}'"

FSSM.monitor(input_dir, '**/*.haml') do
  create {|base, relative| render(input_dir, output_dir, relative) }
  update {|base, relative| render(input_dir, output_dir, relative) }
  delete {|base, relative|
    output_path = File.join(output_dir, relative).gsub(/\.haml$/, ".html")
    puts "Deleting #{output_path}"
    File.delete(output_path)
  }
end

用法:

ruby watch.rb input_folder output_folder

1) I'd say yes. (I didn't have luck with command line options either)

2) I got this example working with coffee script filter. File watching is done with fssm gem. It tracks changes to HAML files recursively in input folder and renders them to output folder with .html file extension.

watch.rb:

require 'rubygems'
require 'fssm'
require 'haml'
require 'coffee-haml-filter'
require 'active_support/core_ext/object/blank'

def render(input_dir, output_dir, relative)
  input_path = File.join(input_dir, relative)
  output_path = File.join(output_dir, relative).gsub(/\.haml$/, ".html")
  haml_engine = Haml::Engine.new(File.read(input_path))
  puts "Rendering #{input_path} -> #{output_path}"
  FileUtils.makedirs(File.dirname(output_path))
  File.open(output_path, 'w') do |file|
    file.write(haml_engine.render)
  end
end

input_dir = File.expand_path(ARGV.length > 0 ? ARGV.shift : '.')
output_dir = File.expand_path(ARGV.length > 0 ? ARGV.shift : input_dir)

puts "Input folder:  '#{input_dir}'"
puts "Output folder: '#{output_dir}'"

FSSM.monitor(input_dir, '**/*.haml') do
  create {|base, relative| render(input_dir, output_dir, relative) }
  update {|base, relative| render(input_dir, output_dir, relative) }
  delete {|base, relative|
    output_path = File.join(output_dir, relative).gsub(/\.haml$/, ".html")
    puts "Deleting #{output_path}"
    File.delete(output_path)
  }
end

Usage:

ruby watch.rb input_folder output_folder
红衣飘飘貌似仙 2024-10-16 04:10:26

--require/-r 选项应该适用于加载 CoffeeScript 过滤器。最新版本中没有,但这是一个错误;它将在下一个版本中修复。

The --require/-r option should work for loading the CoffeeScript filter. It doesn't in the most recent version, but that's a bug; it will be fixed in the next release.

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