Class: Brandish::Markup::Redcarpet::HTML

Inherits:
Redcarpet::Render::SmartyHTML
  • Object
show all
Includes:
Hanami::Helpers::EscapeHelper, Hanami::Helpers::HtmlHelper
Defined in:
lib/brandish/markup/redcarpet/html.rb

Overview

An HTML renderer for redcarpet. This provides integrations with Brandish, as well as code highlighting.

Constant Summary collapse

TAGS =

The tags for each level of header.

Returns:

  • (<::Symbol>)
%i(h1 h2 h3 h4 h5 h6).freeze

Instance Method Summary collapse

Constructor Details

#initialize(context, options) ⇒ HTML

Initialize the renderer with the given context and options.

Parameters:

Options Hash (options):

  • :highlight (::Symbol) — default: :none

    Which highlighter to use. Possible values are :rouge, :coderay, :pygments, and :none.



28
29
30
31
32
# File 'lib/brandish/markup/redcarpet/html.rb', line 28

def initialize(context, options)
  @context = context
  @highlighter = options.fetch(:highlight)
  super(options)
end

Instance Method Details

#block_code(code, language) ⇒ ::String

Highlights a block of code.

Parameters:

  • code (::String)

    The code to highlight

  • language (::String, nil)

    The language that the code was in, or nil if it was not or could not be provided.

Returns:

  • (::String)


53
54
55
56
57
58
59
60
61
62
# File 'lib/brandish/markup/redcarpet/html.rb', line 53

def block_code(code, language)
  basic_language = language || "unknown"
  case @highlighter
  when :rouge then rouge_highlight_code(code, language)
  when :coderay then coderay_highlight_code(code, language)
  when :pygments then pygments_highlight_code(code, language)
  when :none
    html.pre { html.code(code, class: "language-#{basic_language}") }
  end.to_s
end

#header(text, level) ⇒ ::String

Creates a header with the given text and level. If the text ends in /#([\w-]+)/, that is removed, and used as the ID for the header; otherwise, it is automagically assumed from the text.

Parameters:

  • text (::String)
  • level (::Numeric)

Returns:

  • (::String)


41
42
43
44
45
# File 'lib/brandish/markup/redcarpet/html.rb', line 41

def header(text, level)
  text, id = split_text(text)

  html.tag(TAGS.fetch(level), raw(text), id: id).to_s
end