Sha256: c5f63ab83b5033fc5fab9be7d374944d1d5246b19d3cace758a96aa9cc04966a

Contents?: true

Size: 957 Bytes

Versions: 1

Compression:

Stored size: 957 Bytes

Contents

require "redcarpet"
require "pygments"

module Blogdoor
  class Converter
    def initialize
      renderer = BlockCodeWithFilename.new
      @markdown = Redcarpet::Markdown.new(renderer, fenced_code_blocks: true)
    end

    def convert(content)
      @markdown.render(content)
    end

    class BlockCodeWithFilename < Redcarpet::Render::HTML
      INDENT = " " * 2

      def block_code(code, metadata)
        language, filename = metadata.split(":") if metadata
        rows = []
        rows << %(<div class="code-block">)
        if filename
          rows << %(#{INDENT}<div class="code-header">)
          rows << %(#{INDENT * 2}<span>#{filename}</span>)
          rows << %(#{INDENT}</div>)
        end
        rows << %(#{INDENT}<div class="code-body">)
        rows << %(#{INDENT * 2}#{Pygments.highlight(code, lexer: language)})
        rows << %(#{INDENT}</div>)
        rows << %(</div>)
        rows.join("\n")
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blogdoor-0.0.5 lib/blogdoor/converter.rb