Class: PageHub::Markdown::HTMLWithAlbino

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Defined in:
lib/pagehub-markdown/markdown.rb

Overview

a renderer that uses Albino to highlight syntax

Instance Method Summary collapse

Instance Method Details

#block_code(code, language) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/pagehub-markdown/markdown.rb', line 121

def block_code(code, language)
  begin
    # TODO: try to figure out whether @language is valid
    out = Pygments.highlight(code, { lexer: language.to_s, encoding: 'utf-8' })
  rescue Exception => e
    # return "-- INVALID CODE BLOCK, MAKE SURE YOU'VE SURROUNDED CODE WITH ```"
    puts "[warn] pagehub-markdown: unable to highlight code block for language #{language}. Reason: #{e.message}"
    out = ""
  end

  # just render the code as plain text if the language is invalid
  out.empty? ? block_code(code, "text") : out
end