Module: Card::Set::Format::HamlPaths

Included in:
All::Haml::Format, AbstractFormat::HamlViews
Defined in:
card/lib/card/set/format/haml_paths.rb

Overview

methods for handling paths to HAML templates

Constant Summary collapse

TMPSET_REGEXP =
%r{^.*/tmp(sets)?/set/[\w-]+/mod\d{3}-(?<modname>[^/]+)}

Instance Method Summary collapse

Instance Method Details

#deep_source(source) ⇒ Object



51
52
53
54
55
56
57
# File 'card/lib/card/set/format/haml_paths.rb', line 51

def deep_source source
  return source unless tmp_files?

  source&.sub TMPSET_REGEXP do
    "#{Cardio::Mod.fetch(Regexp.last_match[:modname]).path}/set"
  end
end

#each_template_path(source) ⇒ Object



40
41
42
43
44
45
46
47
# File 'card/lib/card/set/format/haml_paths.rb', line 40

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



66
67
68
69
70
71
# File 'card/lib/card/set/format/haml_paths.rb', line 66

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

Raises:



24
25
26
27
28
29
30
31
32
# File 'card/lib/card/set/format/haml_paths.rb', line 24

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



8
9
10
11
12
13
14
# File 'card/lib/card/set/format/haml_paths.rb', line 8

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.message}"
end

#template_location(debug_info) ⇒ Object



34
35
36
37
38
# File 'card/lib/card/set/format/haml_paths.rb', line 34

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



59
60
61
62
63
64
# File 'card/lib/card/set/format/haml_paths.rb', line 59

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}"
  path = ::File.expand_path(template_path, source_dir)
  path if ::File.exist? path
end

#with_template_path(path) ⇒ Object



16
17
18
19
20
21
22
# File 'card/lib/card/set/format/haml_paths.rb', line 16

def with_template_path path
  old_path = @template_path
  @template_path = path
  yield
ensure
  @template_path = old_path
end