Module: DryHamlHandlebars

Defined in:
lib/dry_haml_handlebars.rb,
lib/dry_haml_handlebars/handler.rb,
lib/dry_haml_handlebars/version.rb,
lib/dry_haml_handlebars/register.rb,
lib/dry_haml_handlebars/view_helpers/action_view.rb,
lib/dry_haml_handlebars/controller_helpers/action_controller.rb

Defined Under Namespace

Modules: ControllerHelpers, Register, ViewHelpers Classes: ContentCache, ContentItem, Handler, Railtie, Runner

Constant Summary collapse

INDENT =
/\A(?<indent>\s*)/
CONTENT =
/#{INDENT}\S+.*\Z/
BLOCK_START =
/(?<start>#{INDENT}{{#(?<keyword>\w+))/
VERSION =
"0.0.13"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.on_next_requestObject

Returns the value of attribute on_next_request.



49
50
51
# File 'lib/dry_haml_handlebars/handler.rb', line 49

def on_next_request
  @on_next_request
end

Class Method Details

.compile_all_helper_coffeescriptsObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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_cacheObject



78
79
80
# File 'lib/dry_haml_handlebars.rb', line 78

def self.content_cache
  @content_cache
end

.dedent_hbs(source) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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_i18nObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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 = []) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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