Sha256: 179aba52258d93cf726576d6ec59e222ca940a646b14da15b53966e4399aa944

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require_relative './text_renderers'
require_relative './block_renderers'
require_relative './document_renderers'
require_relative './base_node_renderer'
require_relative './null_renderer'

# RichTextRenderer Namespace
module RichTextRenderer
  # Renderer for RichText field from Contentful
  class Renderer < BaseNodeRenderer
    # Default Renderers
    DEFAULT_MAPPINGS = {
      nil => NullRenderer,
      'document' => DocumentRenderer,
      'heading-1' => HeadingOneRenderer,
      'heading-2' => HeadingTwoRenderer,
      'heading-3' => HeadingThreeRenderer,
      'heading-4' => HeadingFourRenderer,
      'heading-5' => HeadingFiveRenderer,
      'heading-6' => HeadingSixRenderer,
      'blockquote' => BlockQuoteRenderer,
      'hyperlink' => HyperlinkRenderer,
      'paragraph' => ParagraphRenderer,
      'list-item' => ListItemRenderer,
      'ordered-list' => OrderedListRenderer,
      'unordered-list' => UnorderedListRenderer,
      'embedded-entry-block' => EntryBlockRenderer,
      'embedded-asset-block' => AssetBlockRenderer,
      'asset-hyperlink' => AssetHyperlinkRenderer,
      'hr' => HrRenderer,
      'text' => TextRenderer,
      'bold' => BoldRenderer,
      'code' => CodeRenderer,
      'italic' => ItalicRenderer,
      'underline' => UnderlineRenderer,
      'superscript' => SuperscriptRenderer,
      'subscript' => SubscriptRenderer,
      'table' => TableRenderer,
      'table-row' => TableRowRenderer,
      'table-cell' => TableCellRenderer,
      'table-header-cell' => TableHeaderCellRenderer
    }

    def initialize(mappings = {})
      @mappings = DEFAULT_MAPPINGS.merge(mappings)
    end

    # Returns a rendered RichText document
    def render(document)
      renderer = find_renderer(document)
      renderer.render(document) unless renderer.nil?
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rich_text_renderer-0.3.2 lib/rich_text_renderer/renderer.rb