Class: Coradoc::Html::Renderer

Inherits:
Object
  • Object
show all
Includes:
TemplateCaching
Defined in:
lib/coradoc/html/renderer.rb

Constant Summary collapse

DEFAULT_TEMPLATE_DIR =
TemplateLocator::DEFAULT_TEMPLATE_DIR

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_dirs: nil, **options) ⇒ Renderer

Returns a new instance of Renderer.



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/coradoc/html/renderer.rb', line 15

def initialize(template_dirs: nil, **options)
  @template_dirs = normalize_dirs(template_dirs)
  @options = options
  @template_cache = {}
  @locator = TemplateLocator.new(
    user_dirs: @template_dirs,
    default_dir: DEFAULT_TEMPLATE_DIR
  )
  @section_numbers = {}
  @layout_renderer = LayoutRenderer.new
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/coradoc/html/renderer.rb', line 13

def options
  @options
end

#template_dirsObject (readonly)

Returns the value of attribute template_dirs.



13
14
15
# File 'lib/coradoc/html/renderer.rb', line 13

def template_dirs
  @template_dirs
end

Instance Method Details

#available_templatesObject



62
63
64
# File 'lib/coradoc/html/renderer.rb', line 62

def available_templates
  @locator.available_templates
end

#find_template(type_name) ⇒ Object



70
71
72
# File 'lib/coradoc/html/renderer.rb', line 70

def find_template(type_name)
  find_and_load_template(type_name)
end

#render(element) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/coradoc/html/renderer.rb', line 27

def render(element)
  result = Drop::DropFactory.create(element)
  case result
  when Drop::Base then render_drop(result)
  when Array then result.map { |r| r.is_a?(Drop::Base) ? render_drop(r) : r }.join("\n")
  when nil then ''
  else result
  end
end

#render_drop(drop) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/coradoc/html/renderer.rb', line 37

def render_drop(drop)
  return '' if drop.nil?
  return drop.to_s unless drop.is_a?(Drop::Base)

  annotate_section_number(drop)

  template_type = drop.template_type
  template = find_and_load_template(template_type)
  return render_fallback_drop(drop) unless template

  assigns = { 'element' => drop }
  template.render(assigns, registers: { renderer: self, section_numbers: @section_numbers }).strip
end

#render_html5(document, **options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/coradoc/html/renderer.rb', line 51

def render_html5(document, **options)
  @section_numbers = compute_section_numbers(document, options)
  body_html = render(document)

  if options[:layout] == :spa
    render_spa_layout(document, body_html, options)
  else
    render_static_layout(document, body_html, options)
  end
end

#template_exists?(type_name) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/coradoc/html/renderer.rb', line 66

def template_exists?(type_name)
  @locator.exists?(type_name)
end