class Downr::Render

This class wraps both pygmentize and RailsEmoji gems to create a custom renderer

Public Class Methods

new(opt) click to toggle source

Initializes the Render object

@option opts [Boolean] :pygmentize Code colors @option opts [Boolean] :emojify Icons

Calls superclass method
# File lib/downr/render.rb, line 16
def initialize(opt)
  opt = clean_options(opt)
  @options = opt
  super
end

Public Instance Methods

block_code(code, language) click to toggle source

Hook for Redcarpet render

@param [String] code the code snippet to parse @param [String] language the coding language

@return [String] html

# File lib/downr/render.rb, line 31
def block_code(code, language)
  if(@options[:pygmentize])
    return pygmentize(code, language)
  end

  code
end
preprocess(full_document) click to toggle source

Hook for Redcarpet render

@param [String] full_document the complete doc

@return [String] html

Calls superclass method
# File lib/downr/render.rb, line 46
def preprocess(full_document)
  if(@options[:emojify])
    return emojify(full_document)
  end

  super full_document
end

Private Instance Methods

clean_options(opts) click to toggle source

Ensures we have options defined

@private

@option opts [Boolean] :pygmentize Code colors @option opts [Boolean] :emojify Icons

@return [options] the new hash

# File lib/downr/render.rb, line 63
def clean_options opts
  a = {}

  if(opts.has_key?(:pygmentize))
    a[:pygmentize] = opts[:pygmentize]
  else
    a[:pygmentize] = false
  end

  if(opts.has_key?(:emojify))
    a[:emojify] = opts[:emojify]
  else
    a[:emojify] = false
  end
  
  return a
end
emojify(content) click to toggle source

Uses RailsEmoji to insert icons

@private

@param [String] content the string to parse

@return [String] with icons

# File lib/downr/render.rb, line 88
def emojify(content)
  content.to_str.gsub(/:([a-z0-9\+\-_]+):/) do |match|
    RailsEmoji.render match, size: '20x20'
  end
end
pygmentize(code, language) click to toggle source

Uses Pygmentize to color code

@private

@param [String] code the code snippet to parse
@param [String] language the coding language

@return [String] html
# File lib/downr/render.rb, line 102
def pygmentize(code, language)
  language = language.nil? ? :sh : language
  Pygmentize.process(code, language)
end