module Middleman module Syntax class << self def options @@options end def registered(app, options_hash={}) require 'rouge' @@options = options_hash yield @@options if block_given? app.send :include, Helper app.after_configuration do if markdown_engine == :redcarpet require 'middleman-core/renderers/redcarpet' Middleman::Renderers::MiddlemanRedcarpetHTML.send :include, MarkdownCodeRenderer elsif markdown_engine == :kramdown 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 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 %> # my code # <% end %> # # To produce the following structure: # #
#{your code}
#
#