dry_haml_handlebars 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ module DryHamlHandlebars
2
+ module Register
3
+
4
+ #ActionView::Base.send :include, DryHamlHandlebars::AssetHelper
5
+ ActionView::Template.register_template_handler(:haml, DryHamlHandlebars::Handler)
6
+ #raise "#{ActionView::Template.handler_for_extension :haml} is handling :haml"
7
+
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module DryHamlHandlebars
2
+ VERSION = "0.0.4"
3
+ end
@@ -0,0 +1,149 @@
1
+ require "dry_haml_handlebars/version"
2
+ require "v8"
3
+ require "handlebars_assets"
4
+ require 'haml'
5
+ require "dry_haml_handlebars/handler"
6
+ require "dry_haml_handlebars/register"
7
+ require "haml-rails"
8
+ require "rabl"
9
+ require "gon"
10
+ require_relative "action_view/base"
11
+ require_relative "action_view/helpers/capture_helper"
12
+ require_relative "action_controller/base"
13
+
14
+ module DryHamlHandlebars
15
+
16
+ class Railtie < Rails::Railtie
17
+
18
+ config.before_configuration do |app|
19
+ app.config.autoload_paths += %W(#{app.config.root}/app/views)
20
+ end
21
+
22
+ config.to_prepare do
23
+ # this is only called once in dev mode and not on every request as it is meant to
24
+ # just manually call DryHamlHandlebars.prepare_handlebars if you change/add a helper
25
+ # see https://github.com/rails/rails/issues/7152
26
+ DryHamlHandlebars.prepare_handlebars
27
+ end
28
+
29
+ end
30
+
31
+ class ContentCache
32
+
33
+ attr_accessor :store, :index
34
+
35
+ def initialize
36
+ @store = []
37
+ end
38
+
39
+ def add_item(name, path)
40
+ item = ContentItem.new(name.to_sym, path.to_s)
41
+ @store << item
42
+ end
43
+
44
+ def remove_item(item)
45
+ @store.delete item
46
+ end
47
+
48
+ def clear
49
+ initialize
50
+ end
51
+
52
+ end
53
+
54
+ class ContentItem
55
+
56
+ attr_accessor :content
57
+ attr_reader :name, :path
58
+
59
+ def initialize(name, path)
60
+ @name, @path = name, path
61
+ end
62
+
63
+ end
64
+
65
+ @content_cache = ContentCache.new
66
+
67
+ def self.content_cache
68
+ @content_cache
69
+ end
70
+
71
+ def self.prepare_handlebars(additional_javascripts = [])
72
+
73
+ additional_javascripts = Array.wrap(additional_javascripts)
74
+
75
+ compile_all_helper_coffeescripts
76
+ #load_i18n if defined? SimplesIdeias::I18n
77
+
78
+ hbs_context = HandlebarsAssets::Handlebars.send(:context)
79
+
80
+ templates_and_partials = Dir.glob(Rails.root.join('app', 'assets', 'compiled_templates', '**', '*.js'))
81
+ handlebars_helpers = Dir.glob(Rails.root.join('app', 'assets', 'handlebars_helpers', '**', '*.js'))
82
+
83
+ self_loading_assets = templates_and_partials + handlebars_helpers + additional_javascripts
84
+
85
+ self_loading_assets.each do |fname|
86
+ basename = File.basename(fname)
87
+ File.open(fname) do |file|
88
+ source = file.read
89
+ source.strip!
90
+ source.chomp!(";")
91
+ hbs_context.eval(source, basename)
92
+ end
93
+ end
94
+ end
95
+
96
+ def self.load_i18n
97
+
98
+ hbs_context = HandlebarsAssets::Handlebars.send(:context)
99
+
100
+ @i18n_js_path ||= Rails.application.config.assets.paths.find { |fname| fname.match(/i18n-js-[.\d]+\/vendor\/assets\/javascripts/) }
101
+ fname = "#{@i18n_js_path}/i18n.js"
102
+ source = File.read(fname).gsub(
103
+ "var I18n = I18n || {};",
104
+ "this.I18n || (this.I18n = {});"
105
+ )
106
+
107
+ json_translations = SimplesIdeias::I18n.translation_segments.each_with_object({}) do |(name, segment),translations|
108
+ translations.merge!(segment)
109
+ end.to_json
110
+
111
+ load_script = <<-JAVASCRIPT
112
+ (function(){
113
+ #{source}
114
+ I18n.translations = #{json_translations};
115
+ I18n.defaultLocale = #{I18n.default_locale.to_s.inspect};
116
+ I18n.fallbacksRules = #{I18n.fallbacks.to_json};
117
+ I18n.fallbacks = true;
118
+ }).call(this)
119
+ JAVASCRIPT
120
+
121
+ hbs_context.eval load_script
122
+ end
123
+
124
+ def self.compile_all_helper_coffeescripts
125
+ handlebars_helpers = Dir.glob(Rails.root.join('app', 'assets', 'handlebars_helpers', '*', '*.coffee'))
126
+ js_directory = Rails.root.join('app', 'assets', 'handlebars_helpers', 'javascripts').to_s
127
+ handlebars_helpers.each do |coffee_path|
128
+
129
+ #get expected js path
130
+ filename = File.basename(coffee_path).split('.').first + '.js'
131
+ js_path = File.join(js_directory, filename)
132
+
133
+ #see if the js exists and is older than the coffee
134
+ unless File.exist?(js_path) and File.mtime(js_path) > File.mtime(coffee_path)
135
+
136
+ #if so, compile coffee and overwrite/create the js
137
+ coffee = File.read(coffee_path)
138
+ javascript = CoffeeScript.compile(coffee).strip
139
+ javascript = javascript[0..-2] if javascript[-1] == ';' #remove trailing semi-colon because it makes execjs.eval cry
140
+
141
+ FileUtils.mkdir_p js_directory unless File.directory? js_directory
142
+ File.open(js_path, 'w+') { |f| f.write(javascript) }
143
+
144
+ end
145
+
146
+ end
147
+ end
148
+
149
+ end