lib/middleman-syntax/extension.rb in middleman-syntax-2.0.0 vs lib/middleman-syntax/extension.rb in middleman-syntax-2.1.0

- old
+ new

@@ -1,12 +1,16 @@ require 'rouge' +require 'middleman-syntax/highlighter' +require 'middleman-syntax/redcarpet_code_renderer' +require 'middleman-syntax/haml_monkey_patch' module Middleman module Syntax class SyntaxExtension < Extension option :css_class, 'highlight', 'Class name applied to the syntax-highlighted output.' option :line_numbers, false, 'Generate line numbers.' + option :start_line, 1, 'Start the line numbering (if enabled) at the desired integer' option :inline_theme, nil, 'A Rouge::CSSTheme that will be used to highlight the output with inline styles instead of using CSS classes.' option :wrap, true, 'Wrap the highlighted content in a container (<pre> or <div>, depending on whether :line_numbers is on).' option :lexer_options, {}, 'Options for the Rouge lexers.' def after_configuration @@ -28,22 +32,23 @@ end helpers do # Output highlighted code. Use like: # - # <% code('ruby') do %> + # <% code('ruby', :line_numbers => true, :start_line => 7) do %> # my code # <% end %> # # To produce the following structure: # - # <div class="highlight"> - # <pre>#{your code} - # </pre> - # </div> + # <pre class="highlight ruby"> + # <code>#{your code}</code> + # </pre> # - # @param [String] language the Rouge lexer to use + # If no language is provided, then the language name is `plaintext`. + # + # @param [String] language that the Rouge lexer should use # @param [Hash] Options to pass to the Rouge formatter & lexer, overriding global options set by :highlighter_options. def code(language=nil, options={}, &block) raise 'The code helper requires a block to be provided.' unless block_given? # Save current buffer for later @@ -55,58 +60,10 @@ # Reset stored buffer @_out_buf = _buf_was end content = content.encode(Encoding::UTF_8) - concat_content Middleman::Syntax::Highlighter.highlight(content, language).html_safe - end - end - end - - # A mixin for the Redcarpet Markdown renderer that will highlight - # code. - module RedcarpetCodeRenderer - def block_code(code, language) - Middleman::Syntax::Highlighter.highlight(code, language) - end - end - - module Highlighter - mattr_accessor :options - - # A helper module for highlighting code - def self.highlight(code, language=nil, opts={}) - lexer = Rouge::Lexer.find_fancy(language, code) || Rouge::Lexers::PlainText - - highlighter_options = options.to_h.merge(opts) - highlighter_options[:css_class] = [ highlighter_options[:css_class], lexer.tag ].join(' ') - lexer_options = highlighter_options.delete(:lexer_options) - - formatter = Rouge::Formatters::HTML.new(highlighter_options) - formatter.format(lexer.lex(code, options.lexer_options)) - end - end - end -end - -# If Haml is around, define a :code filter that can be used to more conveniently output highlighted code. -if defined? Haml - module Haml - module Filters - module Code - include Base - - def render(code) - code = code.encode(Encoding::UTF_8) - - # Allow language to be specified via a special comment like: - # # lang: ruby - if code.lines.first =~ /\A\s*#\s*lang:\s*(\w+)$/ - language = $1 - code = code.lines.to_a[1..-1].join # Strip first line - end - - Middleman::Syntax::Highlighter.highlight(code, language) + concat_content Middleman::Syntax::Highlighter.highlight(content, language, options).html_safe end end end end end