Class: Panda::Editor::Renderer
- Inherits:
-
Object
- Object
- Panda::Editor::Renderer
- Defined in:
- lib/panda/editor/renderer.rb
Instance Attribute Summary collapse
-
#cache_store ⇒ Object
readonly
Returns the value of attribute cache_store.
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#custom_renderers ⇒ Object
readonly
Returns the value of attribute custom_renderers.
-
#footnote_registry ⇒ Object
readonly
Returns the value of attribute footnote_registry.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #article(blocks, title: nil) ⇒ Object
-
#initialize(content, options = {}) ⇒ Renderer
constructor
A new instance of Renderer.
- #render ⇒ Object
- #section(blocks) ⇒ Object
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, = {}) @content = content @options = @custom_renderers = .delete(:custom_renderers) || {} @cache_store = .delete(:cache_store) || Rails.cache @validate_html = .delete(:validate_html) || false autolink_urls = .delete(:autolink_urls) || false markdown = .delete(:markdown) || false @footnote_registry = FootnoteRegistry.new(autolink_urls: autolink_urls, markdown: markdown) @options[:footnote_registry] = @footnote_registry end |
Instance Attribute Details
#cache_store ⇒ Object (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 |
#content ⇒ Object (readonly)
Returns the value of attribute content.
8 9 10 |
# File 'lib/panda/editor/renderer.rb', line 8 def content @content end |
#custom_renderers ⇒ Object (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_registry ⇒ Object (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 |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/panda/editor/renderer.rb', line 8 def @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, ).render [ '<article>', (title ? "<h1>#{title}</h1>" : ''), rendered, '</article>' ].join("\n") end |
#render ⇒ Object
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, ).render "<section class=\"content-section\">#{rendered}</section>" end |