module DryHamlHandlebars

Constants

BLOCK_START
CONTENT
INDENT
VERSION

Attributes

on_next_request[RW]

Public Class Methods

compile_all_helper_coffeescripts() click to toggle source
# File lib/dry_haml_handlebars.rb, line 135
def self.compile_all_helper_coffeescripts
  handlebars_helpers = Dir.glob(Rails.root.join('app', 'assets', 'handlebars_helpers', '*', '*.coffee'))
  js_directory       = Rails.root.join('app', 'assets', 'handlebars_helpers', 'javascripts').to_s
  handlebars_helpers.each do |coffee_path|
    
    #get expected js path
    filename   = File.basename(coffee_path).split('.').first + '.js'
    js_path    = File.join(js_directory, filename)
    
    #see if the js exists and is older than the coffee
    unless File.exist?(js_path) and File.mtime(js_path) >= File.mtime(coffee_path)
      
      #if so, compile coffee and overwrite/create the js
      coffee     = File.read(coffee_path)
      javascript = CoffeeScript.compile(coffee).strip
      javascript = javascript[0..-2] if javascript[-1] == ';' #remove trailing semi-colon because it makes execjs.eval cry
      
      FileUtils.mkdir_p js_directory unless File.directory? js_directory
      File.open(js_path, 'w+') { |f| f.write(javascript) }
      
    end
      
  end
end
content_cache() click to toggle source
# File lib/dry_haml_handlebars.rb, line 78
def self.content_cache
  @content_cache
end
dedent_hbs(source) click to toggle source
# File lib/dry_haml_handlebars/handler.rb, line 9
def self.dedent_hbs(source)
  lines = source.lines.to_a
  mod_lines = lines.clone
  lines.each_with_index do |line1, i1|
    
    if (l1_match = line1.match BLOCK_START)
      
      l1_index  = i1
      l1_indent = l1_match[:indent] ? l1_match[:indent].length : 0
      l1_text   = l1_match[:start]
      
      #binding.pry if i1 == 7
  
      next_match = nil
      next_line = lines[l1_index+1..-1].detect { |l| next_match = l.match(CONTENT) }
      
      next unless next_match
      next_line_indent = next_match[:indent] ? next_match[:indent].length : 0
      next unless (indent = next_line_indent - l1_indent) > 0
      
      
      l2_text   = l1_text.sub("{{#", "{{/")
      else_text = " " * l1_indent + "{{else}}"
      else_index = nil
      l2_index = lines[l1_index+1..-1].each_with_index.each do |line2, i2|
        else_index = i2 if line2.starts_with? else_text
        break i2 + l1_index+1 if line2.starts_with? l2_text
      end
  
      (l1_index+1..l2_index-1).each_with_index do |index, i|
        next if i == else_index
        mod_lines[index] = mod_lines[index][indent..-1]
      end
      
    end
  end
  mod_lines.join
end
load_i18n() click to toggle source
# File lib/dry_haml_handlebars.rb, line 107
  def self.load_i18n
    
    hbs_context = HandlebarsAssets::Handlebars.send(:context)
    
    @i18n_js_path ||= Rails.application.config.assets.paths.find { |fname| fname.match(/i18n-js-[.\d]+\/vendor\/assets\/javascripts/) }
    fname    = "#{@i18n_js_path}/i18n.js"
    source   = File.read(fname).gsub(
      "var I18n = I18n || {};",
      "this.I18n || (this.I18n = {});"
    )
    
    json_translations = SimplesIdeias::I18n.translation_segments.each_with_object({}) do |(name, segment),translations|
      translations.merge!(segment)
    end.to_json

    load_script = <<-JAVASCRIPT
      (function(){
        #{source}
        I18n.translations   = #{json_translations};
        I18n.defaultLocale  = #{I18n.default_locale.to_s.inspect};
        I18n.fallbacksRules = #{I18n.fallbacks.to_json};
        I18n.fallbacks      = true;
      }).call(this)
    JAVASCRIPT
    
    hbs_context.eval load_script
  end
prepare_handlebars(additional_javascripts = []) click to toggle source
# File lib/dry_haml_handlebars.rb, line 82
def self.prepare_handlebars(additional_javascripts = [])
  
  additional_javascripts = Array.wrap(additional_javascripts)
  
  compile_all_helper_coffeescripts if ["development", "test"].include?(Rails.env.to_s)
  #load_i18n if defined? SimplesIdeias::I18n
  
  hbs_context = HandlebarsAssets::Handlebars.send(:context)
  
  templates_and_partials = Dir.glob(Rails.root.join('app', 'assets', 'compiled_templates', '**', '*.js'))
  handlebars_helpers     = Dir.glob(Rails.root.join('app', 'assets', 'handlebars_helpers', '**', '*.js'))

  self_loading_assets = templates_and_partials + handlebars_helpers + additional_javascripts
  
  self_loading_assets.each do |fname|
    basename = File.basename(fname)
    File.open(fname) do |file|
      source = file.read
      source.strip!
      source.chomp!(";")
      hbs_context.eval(source, basename)
    end
  end
end