Class: Middleman::HashiCorp::RedcarpetHTML

Inherits:
Renderers::MiddlemanRedcarpetHTML
  • Object
show all
Defined in:
lib/middleman-hashicorp/redcarpet.rb

Overview

Our custom Markdown parser - extends middleman’s customer parser so we pick up all the magic.

Constant Summary collapse

REDCARPET_OPTIONS =

Custom RedCarpet options.

{
  autolink:           true,
  fenced_code_blocks: true,
  tables:             true,
  no_intra_emphasis:  true,
  with_toc_data:      true,
  xhtml:              true,
  strikethrough:      true,
  superscript:        true,
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RedcarpetHTML

Returns a new instance of RedcarpetHTML.



21
22
23
# File 'lib/middleman-hashicorp/redcarpet.rb', line 21

def initialize(options = {})
  super(options.merge(REDCARPET_OPTIONS))
end

Instance Method Details

#block_html(raw) ⇒ Object

Override block_html to support parsing nested markdown blocks.

Parameters:

  • raw (String)


67
68
69
70
71
72
73
74
75
76
# File 'lib/middleman-hashicorp/redcarpet.rb', line 67

def block_html(raw)
  raw = unindent(raw)

  if md = raw.match(/\<(.+?)\>(.*)\<(\/.+?)\>/m)
    open_tag, content, close_tag = md.captures
    "<#{open_tag}>\n#{recursive_render(unindent(content))}<#{close_tag}>"
  else
    raw
  end
end

#header(title, level) ⇒ Object

Override headers to add custom links.



28
29
30
31
32
33
34
35
36
37
# File 'lib/middleman-hashicorp/redcarpet.rb', line 28

def header(title, level)
  anchor = anchor_link(title)

  return <<-EOH.gsub(/^ {6}/, "")
    <h#{level} id="#{anchor}">
      <a name="#{anchor}" class="anchor" href="##{anchor}">&raquo;</a>
      #{title}
    </h#{level}>
  EOH
end

#list_item(text, list_type) ⇒ Object

Override list_item to automatically add links for documentation

Parameters:

  • text (String)
  • list_type (String)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/middleman-hashicorp/redcarpet.rb', line 45

def list_item(text, list_type)
  @anchors ||= {}

  md = text.match(/\A(?:<p>)?(<code>(.+?)<\/code>)/)
  linked = !text.match(/\A(<p>)?<a(.+?)>(.+?)<\/a>\s*?[-:]?/).nil?

  if !md.nil? && !linked
    container, name = md.captures
    anchor = anchor_link(name)

    replace = %|<a name="#{anchor}" /><a href="##{anchor}">#{container}</a>|
    text.sub!(container, replace)
  end

  "<li>#{text}</li>\n"
end

#paragraph(text) ⇒ String

Override paragraph to support custom alerts.

Parameters:

  • text (String)

Returns:

  • (String)


84
85
86
# File 'lib/middleman-hashicorp/redcarpet.rb', line 84

def paragraph(text)
  add_alerts("<p>#{text.strip}</p>\n")
end