Class: Decidim::ContentRenderers::BaseRenderer Abstract

Inherits:
Object
  • Object
show all
Includes:
Decidim::ContentProcessor::Common
Defined in:
decidim-core/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

Methods included from Decidim::ContentProcessor::Common

#html_content?, #html_fragment

Constructor Details

#initialize(content) ⇒ BaseRenderer

Gets initialized with the ‘content` to format

Parameters:

  • content (String)

    content to be formatted



21
22
23
# File 'decidim-core/lib/decidim/content_renderers/base_renderer.rb', line 21

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

Instance Attribute Details

#contentString (readonly)

Returns the content to be formatted.

Returns:

  • (String)

    the content to be formatted



16
17
18
# File 'decidim-core/lib/decidim/content_renderers/base_renderer.rb', line 16

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



34
35
36
# File 'decidim-core/lib/decidim/content_renderers/base_renderer.rb', line 34

def render(_options = nil)
  content
end