Class: Panda::Editor::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/panda/editor/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, options = {}) ⇒ Renderer

Returns a new instance of Renderer.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/panda/editor/renderer.rb', line 10

def initialize(content, options = {})
  @content = content
  @options = options
  @custom_renderers = options.delete(:custom_renderers) || {}
  @cache_store = options.delete(:cache_store) || Rails.cache
  @validate_html = options.delete(:validate_html) || false
  autolink_urls = options.delete(:autolink_urls) || false
  markdown = options.delete(:markdown) || false
  @footnote_registry = FootnoteRegistry.new(autolink_urls: autolink_urls, markdown: markdown)
  @options[:footnote_registry] = @footnote_registry
end

Instance Attribute Details

#cache_storeObject (readonly)

Returns the value of attribute cache_store.



8
9
10
# File 'lib/panda/editor/renderer.rb', line 8

def cache_store
  @cache_store
end

#contentObject (readonly)

Returns the value of attribute content.



8
9
10
# File 'lib/panda/editor/renderer.rb', line 8

def content
  @content
end

#custom_renderersObject (readonly)

Returns the value of attribute custom_renderers.



8
9
10
# File 'lib/panda/editor/renderer.rb', line 8

def custom_renderers
  @custom_renderers
end

#footnote_registryObject (readonly)

Returns the value of attribute footnote_registry.



8
9
10
# File 'lib/panda/editor/renderer.rb', line 8

def footnote_registry
  @footnote_registry
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/panda/editor/renderer.rb', line 8

def options
  @options
end

Instance Method Details

#article(blocks, title: nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/panda/editor/renderer.rb', line 50

def article(blocks, title: nil)
  return '' if blocks.nil? || blocks.empty?

  content = { 'blocks' => blocks }
  rendered = self.class.new(content, options).render

  [
    '<article>',
    (title ? "<h1>#{title}</h1>" : ''),
    rendered,
    '</article>'
  ].join("\n")
end

#renderObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/panda/editor/renderer.rb', line 22

def render
  return '' if content.nil? || content == {}
  return content.to_s unless content.is_a?(Hash) && content['blocks'].is_a?(Array)

  rendered = content['blocks'].map do |block|
    render_block(block)
  end.join("\n")

  rendered = @validate_html ? validate_html(rendered) : rendered

  # Append sources section if footnotes were collected
  if @footnote_registry.any?
    sources_section = @footnote_registry.render_sources_section
    rendered = [rendered, sources_section].join("\n")
  end

  rendered.presence || ''
end

#section(blocks) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/panda/editor/renderer.rb', line 41

def section(blocks)
  return '' if blocks.nil? || blocks.empty?

  content = { 'blocks' => blocks }
  rendered = self.class.new(content, options).render

  "<section class=\"content-section\">#{rendered}</section>"
end