首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有可能在Compass (样式表创作工具)中“查看”Haml文件?

是否有可能在Compass (样式表创作工具)中“查看”Haml文件?
EN

Stack Overflow用户
提问于 2010-06-22 14:30:24
回答 1查看 923关注 0票数 3

目前,Compass正在监视src文件夹中的文件,并自动更新cs文件。(输入compass watch myproject)。

有没有办法在“监视过程”中包括haml文件?

(我无法安装StaticMatic,因为我不想安装Ruby1.8)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-15 12:50:49

首先,您应该使用RVM。安装任何版本的Ruby都是无痛的。

其次,我只是为自己写了这篇文章,使用了Compass用来看文件的相同的“fssm”创业板。将其添加到/创建一个Rakefile:

代码语言:javascript
复制
require 'rubygems'
require 'fssm'
require 'haml'

class HamlWatcher
  class << self
    def watch
      refresh
      puts ">>> HamlWatcher is watching for changes. Press Ctrl-C to Stop."
      FSSM.monitor('haml', '**/*.haml') do
        update do |base, relative|
          puts ">>> Change detected to: #{relative}"
          HamlWatcher.compile(relative)
        end
        create do |base, relative|
          puts ">>> File created: #{relative}"
          HamlWatcher.compile(relative)
        end
        delete do |base, relative|
          puts ">>> File deleted: #{relative}"
          HamlWatcher.remove(relative)
        end
      end
    end

    def output_file(filename)
      # './haml' retains the base directory structure
      filename.gsub(/\.html\.haml$/,'.html')
    end

    def remove(file)
      output = output_file(file)
      File.delete output
      puts "\033[0;31m   remove\033[0m #{output}"
    end

    def compile(file)
      output_file_name = output_file(file)
      origin = File.open(File.join('haml', file)).read
      result = Haml::Engine.new(origin).render
      raise "Nothing rendered!" if result.empty?
      # Write rendered HTML to file
      color, action = File.exist?(output_file_name) ? [33, 'overwrite'] : [32, '   create']
      puts "\033[0;#{color}m#{action}\033[0m #{output_file_name}"
      File.open(output_file_name,'w') {|f| f.write(result)}
    end

    # Check that all haml templates have been rendered.
    def refresh
      Dir.glob('haml/**/*.haml').each do |file|
        file.gsub!(/^haml\//, '')
        compile(file) unless File.exist?(output_file(file))
      end
    end
  end
end


namespace :haml do
  desc "Watch the site's HAML templates and recompile them when they change"
  task :watch do
    require File.join(File.dirname(__FILE__), 'lib', 'haml_watcher')
    HamlWatcher.watch
  end
end

运行它时:

代码语言:javascript
复制
rake haml:watch
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3094131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档