2

私は HAML と CoffeeScript に非常に興奮しており、Rails 以外の環境でそれらを使用する方法を示すチュートリアルに取り組んでいます。したがって、haml には使いやすいコマンドライン ユーティリティがあります。

haml input.haml output.html.

そして、素晴らしいことに、 CoffeeScript を HAML ファイル内の JS に変換するカスタム フィルターを提供することを目的としたプロジェクト (多くのフォークの 1 つ: https://github.com/aussiegeek/coffee-haml-filter ) が存在します。残念ながら (何か足りないのでしょうか?)、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

便利だと思ったら自由に使ってください

4

3 に答える 3

1

--require/オプションは-r、CoffeeScript フィルターをロードするために機能するはずです。最新バージョンにはありませんが、これはバグです。次のリリースで修正される予定です。

于 2010-12-29T19:58:05.657 に答える
0

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
于 2010-12-28T21:59:41.350 に答える