Sha256: 0ab9d32ea7f4aa5f8bf752bac9e3cb0882145b567408598855d1351b8a6b41ba

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

module Middleman
  module Syntax
    class << self

      def options
        @@options
      end

      def registered(app, options_hash={})
        require 'pygments'

        @@options = options_hash
        yield @@options if block_given?

        app.send :include, Helper

        require 'middleman-core/renderers/redcarpet'
        Middleman::Renderers::MiddlemanRedcarpetHTML.send :include, MarkdownCodeRenderer
      end
      alias :included :registered
    end

    module Helper

      # Output highlighted code. Use like:
      #
      #    <% code('ruby') do %>
      #      my code
      #    <% end %>
      #
      # To produce the following structure:
      #
      #    <div class="highlight">
      #      <pre>#{your code}
      #      </pre>
      #    </div>
      #
      # @param [String] language the Pygments lexer to use
      def code(language=nil, &block)
        # Save current buffer for later
        @_out_buf, _buf_was = "", @_out_buf

        begin
          content = if block_given?
            capture_html(&block)
          else
            ""
          end
        ensure
          # Reset stored buffer
          @_out_buf = _buf_was
        end

        options = ::Middleman::Syntax.options.merge :lexer => language
        concat_content Pygments.highlight(code, :options => options)
      end
    end

    module MarkdownCodeRenderer
      def block_code(code, language)
        options = ::Middleman::Syntax.options
        options.merge! :lexer => language if language
        Pygments.highlight(code, :options => options)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
middleman-syntax-1.1.1 lib/middleman-syntax/extension.rb