module Markdpwn::Code

Code formatting.

Public Class Methods

pygment(code, options = {}) click to toggle source

The raw Pygments output for parsing some code.

# File lib/markdpwn/code.rb, line 29
def self.pygment(code, options = {})
  lexer = pygments_lexer options
  pygments_args = { formatter: 'html',
      options: { encoding: 'utf-8', nowrap: true } }
  pygments_args[:lexer] = lexer if lexer
  Pygments.highlight code, pygments_args
end
pygments_lexer(options = {}) click to toggle source

The name of the Python lexer that is most suitable for some code.

@param [Hash] options code properties that help choose the formatter @option options [String] :code the code to be parsed, or a sample of the

code; if code is provided, a language will always be returned

@see Markdpwn::Code#render for other available options @return [String] the name of a Pygments lexer that can parse the code

# File lib/markdpwn/code.rb, line 44
def self.pygments_lexer(options = {})
  if language = options[:language]
    lexer = begin
      Pygments.lexer_name_for lexer: language
    rescue IOError
      nil
    end
    return lexer if lexer
  end

  if mime_type = options[:mime_type]
    lexer = begin
      Pygments.lexer_name_for mimetype: mime_type
    rescue IOError
      nil
    end
    return lexer if lexer
  end

  if file_name = options[:file_name]
    lexer = begin
      Pygments.lexer_name_for filename: file_name
    rescue IOError
      nil
    end
    return lexer if lexer
  end

  if code = options[:code]
    lexer = begin
      Pygments.lexer_name_for code
    rescue IOError
      nil
    end
    return lexer if lexer
  end

  nil
end
render(code, options = {}) click to toggle source

Marks up code.

@param [String] code the code to be formatted @param [Hash] options code properties that help choose the formatter @option options [String] :mime_type the MIME type of the code file; e-mail

attachments and git blobs have MIME types

@option options [String] :file_name the name of the file containing the

piece of code; meaningful for files in version control repositories,
e-mail attachments, and code fetched from links

@option options [String] :language the name of the code's language; GFM

code blocks can include a language name

@return [String] a HTML fragment containing the formatted code

# File lib/markdpwn/code.rb, line 19
def self.render(code, options = {})
  [
    %Q|<div class="markdpwn-parsed-code">|,
    pygment(code, options),
    "</div>"
  ].join ''
end