Class: Brandish::Markup::Redcarpet::Format

Inherits:
Object
  • Object
show all
Defined in:
lib/brandish/markup/redcarpet/format.rb

Overview

Formats text with Redcarpet. This is extracted into a seperate object in order to provide highlighting properties. This is format-independant.

This class can take the following options:

  • All Markdown extension options that are listed on https://github.com/vmg/redcarpet, e.g. :no_intra_emphasis, :tables, etc.
  • All HTML formatter options that are listed on https://github.com/vmg/redcarpet, e.g. :filter_html, :no_images, etc.
  • :highlight - Optional. The highlighting engine to use. Can be one of :rouge, :coderay, :pygments, and :none. Remember to include the requisite highlighting engine in your Gemfile. They will automatically be required as needed. Defaults to :none.

Constant Summary collapse

MARKDOWN_OPTIONS =

The options that are passed over to the markdown engine. These are extracted from the options that are passed to the markup engine.

Returns:

  • (<::Symbol>)
%i(
  no_intra_emphasis tables fenced_code_blocks autolink
  disable_indented_code_blocks strikethrough lax_spacing
  space_after_headers superscript underline quote footnotes
).freeze
MARKDOWN_DEFAULTS =

The default options for the markdown engine as passed by this markup engine.

Returns:

  • ({::Symbol => ::Object})
{
  fenced_code_blocks: true, tables: true, autolink: true,
  strikethrough: true, superscript: true, underline: true,
  footnotes: true, space_after_headers: true
}.freeze
FORMAT_OPTIONS =

The options that are passed over to the formatting engine. These are Extracted from the options that are passed to the markup engine.

Returns:

  • (<::Symbol>)
%i(
  filter_html no_images no_links no_styles escape_html safe_links_only
  with_toc_data hard_wrap xhtml prettify link_attributes highlight
).freeze
FORMAT_DEFAULTS =

The default options for the formatting engine as passed by this markup engine.

Returns:

  • ({::Symbol => ::Object})
{ highlight: :none }.freeze
HIGHLIGHTERS =

The highlighting engines that are supported by this markup engine. The key is the value passed by the :highlight option, and the value is the require file name. If the value is nil, no requirement is performed.

Returns:

  • ({::Symbol => ::Object, nil})
{
  rouge: "rouge", coderay: "coderay", pygments: "pygments",
  none: nil
}.freeze
FORMATTERS =

The formating engines that can be used by this markup engine.

Returns:

  • ({::Symbol => ::Class})
{ html: HTML }.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Format

Initialize the markup engine for Redcarpet. For the available options, see Brandish::Markup::Redcarpet::Format.

Parameters:

  • options (::Hash)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/brandish/markup/redcarpet/format.rb', line 79

def initialize(options)
  @context = options.fetch(:context)
  @format = options.fetch(:format)
  @markdown_options = MARKDOWN_DEFAULTS
                      .merge(extract_options(MARKDOWN_OPTIONS, options))
  @formatter_options = FORMAT_DEFAULTS
                       .merge(extract_options(FORMAT_OPTIONS, options))
  @highlight = @formatter_options[:highlight]
  load_highlighter
  load_engine
end

Instance Method Details

#render(string) ⇒ ::String

Renders the given text using the engine.

Parameters:

  • string (::String)

    The value to render.

Returns:

  • (::String)

    The rendered value.



95
96
97
# File 'lib/brandish/markup/redcarpet/format.rb', line 95

def render(string)
  @engine.render(string)
end