Class: PandaCms::EditorJs::Renderer

Inherits:
Object
  • Object
show all
Defined in:
app/lib/panda_cms/editor_js/renderer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Renderer.



6
7
8
9
10
11
12
# File 'app/lib/panda_cms/editor_js/renderer.rb', line 6

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
end

Instance Attribute Details

#cache_storeObject (readonly)

Returns the value of attribute cache_store.



4
5
6
# File 'app/lib/panda_cms/editor_js/renderer.rb', line 4

def cache_store
  @cache_store
end

#contentObject (readonly)

Returns the value of attribute content.



4
5
6
# File 'app/lib/panda_cms/editor_js/renderer.rb', line 4

def content
  @content
end

#custom_renderersObject (readonly)

Returns the value of attribute custom_renderers.



4
5
6
# File 'app/lib/panda_cms/editor_js/renderer.rb', line 4

def custom_renderers
  @custom_renderers
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'app/lib/panda_cms/editor_js/renderer.rb', line 4

def options
  @options
end

Instance Method Details

#article(blocks, title: nil) ⇒ Object



27
28
29
30
31
32
# File 'app/lib/panda_cms/editor_js/renderer.rb', line 27

def article(blocks, title: nil)
  content = []
  content << "<h1>#{title}</h1>" if title
  content << render_blocks(blocks)
  "<article>#{content.join("\n")}</article>"
end

#renderObject



14
15
16
17
18
19
20
21
# File 'app/lib/panda_cms/editor_js/renderer.rb', line 14

def render
  return "" if content.nil? || !content.is_a?(Hash) || !content["blocks"]

  blocks = remove_empty_paragraphs(content["blocks"])
  rendered = blocks.map { |block| render_block_with_cache(block) }.join("\n")

  @validate_html ? validate_html(rendered) : rendered
end

#section(blocks) ⇒ Object



23
24
25
# File 'app/lib/panda_cms/editor_js/renderer.rb', line 23

def section(blocks)
  "<section class=\"content-section\">#{render_blocks(blocks)}</section>"
end