Class: Decidim::ContentRenderers::BaseRenderer Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/content_renderers/base_renderer.rb

Overview

This class is abstract.

Subclass and override #render to implement a content renderer

Abstract base class for content renderers, so they have the same contract

Examples:

How to use a content renderer class

renderer = Decidim::ContentRenderers::CustomRenderer.new(content)
parser.render # returns the content formatted

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ BaseRenderer

Gets initialized with the ‘content` to format

Parameters:

  • content (String)

    content to be formatted



19
20
21
# File 'lib/decidim/content_renderers/base_renderer.rb', line 19

def initialize(content)
  @content = content || ""
end

Instance Attribute Details

#contentString (readonly)

Returns the content to be formatted.

Returns:

  • (String)

    the content to be formatted



14
15
16
# File 'lib/decidim/content_renderers/base_renderer.rb', line 14

def content
  @content
end

Instance Method Details

#render(_options = nil) ⇒ String

This method is abstract.

Subclass is expected to implement it

Format the content and return it ready to display

Examples:

Implementation to display prohibited words

def render
  content.gsub(/\~\~(.*?)\~\~/, '<del>\1</del>')
end

Returns:

  • (String)

    the content processed and ready to display



32
33
34
# File 'lib/decidim/content_renderers/base_renderer.rb', line 32

def render(_options = nil)
  content
end