Module: Card::Set::Format::HamlPaths
- Included in:
- AbstractFormat::HamlViews
- Defined in:
- lib/card/set/format/haml_paths.rb
Overview
methods for handling paths to HAML templates
Constant Summary collapse
- CORE_MODS =
::Set.new %w[core admin].freeze
- TEMPLATE_DIR =
%w[template set].freeze
- TMPSET_REGEXP =
%r{(?<carddir>/card)/tmp(sets)?/set/mod\d{3}-(?<modname>[^/]+)/}
Instance Method Summary collapse
- #deep_source(source) ⇒ Object
- #each_template_path(source) ⇒ Object
- #haml_block_locals(&block) ⇒ Object
- #haml_template_path(view = nil, source = nil) ⇒ Object
- #haml_to_html(haml, locals = {}, a_binding = nil, debug_info = {}) ⇒ Object
- #source_mod_dir(modname, carddir) ⇒ Object
- #template_location(debug_info) ⇒ Object
- #try_haml_template_path(template_path, view, source_dir, ext = "haml") ⇒ Object
- #with_template_path(path) ⇒ Object
Instance Method Details
#deep_source(source) ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/card/set/format/haml_paths.rb', line 54 def deep_source source return source unless tmp_files? source&.gsub TMPSET_REGEXP do match = Regexp.last_match source_mod_dir match[:modname], match[:carddir] end end |
#each_template_path(source) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/card/set/format/haml_paths.rb', line 43 def each_template_path source source = deep_source(source) || source_location basename = ::File.basename source, ".rb" source_dir = ::File.dirname source ["./#{basename}", "."].each do |template_dir| yield template_dir, source_dir end end |
#haml_block_locals(&block) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/card/set/format/haml_paths.rb', line 82 def haml_block_locals &block instance_exec(&block) if block_given? instance_variables.each_with_object({}) do |var, h| h[var.to_s.tr("@", "").to_sym] = instance_variable_get var end end |
#haml_template_path(view = nil, source = nil) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/card/set/format/haml_paths.rb', line 27 def haml_template_path view=nil, source=nil each_template_path(source) do |template_dir, source_dir| path = try_haml_template_path template_dir, view, source_dir return path if path end msg = "can't find haml template" msg += " for #{view}" if view.present? raise Card::Error, msg end |
#haml_to_html(haml, locals = {}, a_binding = nil, debug_info = {}) ⇒ Object
11 12 13 14 15 16 17 |
# File 'lib/card/set/format/haml_paths.rb', line 11 def haml_to_html haml, locals={}, a_binding=nil, debug_info={} a_binding ||= binding ::Haml::Engine.new(haml).render a_binding, locals || {} rescue Haml::SyntaxError => e raise Card::Error, "haml syntax error #{template_location(debug_info)}: #{e.}" end |
#source_mod_dir(modname, carddir) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/card/set/format/haml_paths.rb', line 63 def source_mod_dir modname, carddir # TODO: handle non-standard mod dirs dir = "/mod/#{modname}/set/" return dir unless modname.in? CORE_MODS "#{carddir}/#{dir}/" end |
#template_location(debug_info) ⇒ Object
37 38 39 40 41 |
# File 'lib/card/set/format/haml_paths.rb', line 37 def template_location debug_info return "" unless debug_info[:path] Pathname.new(debug_info[:path]).relative_path_from(Pathname.new(Dir.pwd)) end |
#try_haml_template_path(template_path, view, source_dir, ext = "haml") ⇒ Object
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/card/set/format/haml_paths.rb', line 71 def try_haml_template_path template_path, view, source_dir, ext="haml" template_path = File.join(template_path, view.to_s) if view.present? template_path += ".#{ext}" TEMPLATE_DIR.each do |template_dir| path = ::File.(template_path, source_dir) .sub(%r{(/mod/[^/]+)/set/}, "\\1/#{template_dir}/") return path if ::File.exist?(path) end false end |
#with_template_path(path) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/card/set/format/haml_paths.rb', line 19 def with_template_path path old_path = @template_path @template_path = path yield ensure @template_path = old_path end |