lib/middleman-syntax/extension.rb in middleman-syntax-1.1.1 vs lib/middleman-syntax/extension.rb in middleman-syntax-1.2.0
- old
+ new
@@ -5,23 +5,50 @@
def options
@@options
end
def registered(app, options_hash={})
- require 'pygments'
+ require 'rouge'
@@options = options_hash
yield @@options if block_given?
app.send :include, Helper
- require 'middleman-core/renderers/redcarpet'
- Middleman::Renderers::MiddlemanRedcarpetHTML.send :include, MarkdownCodeRenderer
+ if app.markdown_engine == :redcarpet
+ begin
+ require 'middleman-core/renderers/redcarpet'
+ Middleman::Renderers::MiddlemanRedcarpetHTML.send :include, MarkdownCodeRenderer
+ rescue LoadError
+ end
+ else
+ begin
+ require 'kramdown'
+ Kramdown::Converter::Html.class_eval do
+ def convert_codeblock(el, indent)
+ attr = el.attr.dup
+ language = extract_code_language!(attr)
+ Middleman::Syntax::Highlighter.highlight(el.value, language)
+ end
+ end
+ rescue LoadError
+ end
+ end
end
alias :included :registered
end
+ module Highlighter
+ # A helper module for highlighting code
+ def self.highlight(code, language)
+ opts = ::Middleman::Syntax.options.dup
+ lexer = Rouge::Lexer.find_fancy(language, code) || Rouge::Lexers::Text
+ formatter = Rouge::Formatters::HTML.new(opts.reverse_merge({ :css_class => "highlight #{lexer.tag}" }))
+ formatter.format(lexer.lex(code, opts))
+ end
+ end
+
module Helper
# Output highlighted code. Use like:
#
# <% code('ruby') do %>
@@ -49,19 +76,16 @@
ensure
# Reset stored buffer
@_out_buf = _buf_was
end
- options = ::Middleman::Syntax.options.merge :lexer => language
- concat_content Pygments.highlight(code, :options => options)
+ concat_content Middleman::Syntax::Highlighter.highlight(content, language)
end
end
module MarkdownCodeRenderer
def block_code(code, language)
- options = ::Middleman::Syntax.options
- options.merge! :lexer => language if language
- Pygments.highlight(code, :options => options)
+ Middleman::Syntax::Highlighter.highlight(code, language)
end
end
end
end