Sha256: 3f58feb734639e89ac6b9c155910cefa69646901e480883abf4aa0f5bba1a3d1

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

module Inverter
  class Renderer

    def self.markdown_input_type?(name)
      name_and_type = name.split(':')

      if name_and_type.size > 1
        return name_and_type[1].strip == 'markdown'
      end

      return false
    end

    def self.markdown
      # markdown configuration
      # https://github.com/vmg/redcarpet#and-its-like-really-simple-to-use
      @markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML,
                                            autolink: true,
                                            tables: true)
    end

    def self.render(html, blocks)
      offset = 0
      map = ::Inverter::Parser.map_blocks_for(html)

      map.each do |name, pos|

        block = blocks[name]

        if markdown_input_type?(name)
          block = markdown.render(block)
        end

        block = "#{ block }"

        html[offset+pos[0]..offset+pos[1]] = block

        block_size          = block.size
        template_block_size = pos[1] - pos[0]
        offset             += block_size - template_block_size - 1

      end

      return html
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
inverter-0.5.1 lib/inverter/renderer.rb
inverter-0.5.0 lib/inverter/renderer.rb
inverter-0.4.7 lib/inverter/renderer.rb