Class: Jazzy::Markdown::JazzyHTML

Inherits:
Redcarpet::Render::HTML
  • Object
show all
Includes:
Footnotes, Redcarpet::Render::SmartyPants, Rouge::Plugins::Redcarpet
Defined in:
lib/jazzy/jazzy_markdown.rb

Overview

rubocop:disable Metrics/ClassLength

Direct Known Subclasses

JazzyDeclarationHTML

Constant Summary collapse

UNIQUELY_HANDLED_CALLOUTS =
%w[parameters
parameter
returns].freeze
GENERAL_CALLOUTS =
%w[attention
author
authors
bug
complexity
copyright
date
experiment
important
invariant
keyword
mutatingvariant
nonmutatingvariant
note
postcondition
precondition
recommended
recommendedover
remark
remarks
requires
see
seealso
since
todo
throws
version
warning].freeze
SPECIAL_LIST_TYPES =
(UNIQUELY_HANDLED_CALLOUTS + GENERAL_CALLOUTS).freeze
SPECIAL_LIST_TYPE_REGEX =
%r{
\A\s* # optional leading spaces
(<p>\s*)? # optional opening p tag
# any one of our special list types
(#{SPECIAL_LIST_TYPES.map(&Regexp.method(:escape)).join('|')})
[\s:] # followed by either a space or a colon
}ix.freeze
ELIDED_LI_TOKEN =
'7wNVzLB0OYPL2eGlPKu8q4vITltqh0Y6DPZf659TPMAeYh49o'
DOCC_CALLOUTS =
%w[note
important
warning
tip
experiment].freeze
DOCC_CALLOUT_REGEX =
%r{
  \A\s* # optional leading spaces
  (?:<p>\s*)? # optional opening p tag
  # any one of the callout names
  (#{DOCC_CALLOUTS.map(&Regexp.method(:escape)).join('|')})
  : # followed directly by a colon
}ix.freeze

Instance Attribute Summary collapse

Attributes included from Footnotes

#footnotes_hash

Instance Method Summary collapse

Methods included from Footnotes

#footnote_def, #footnote_ref, #map_footnote, next_footnote, #reset

Instance Attribute Details

#default_languageObject

Returns the value of attribute default_language.



54
55
56
# File 'lib/jazzy/jazzy_markdown.rb', line 54

def default_language
  @default_language
end

Instance Method Details

#block_code(code, language) ⇒ Object



190
191
192
# File 'lib/jazzy/jazzy_markdown.rb', line 190

def block_code(code, language)
  super(code, language || default_language)
end

#block_quote(html) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/jazzy/jazzy_markdown.rb', line 181

def block_quote(html)
  if html =~ DOCC_CALLOUT_REGEX
    type = Regexp.last_match[1]
    render_aside(type, html.sub(/#{Regexp.escape(type)}:\s*/, ''))
  else
    "\n<blockquote>\n#{html}</blockquote>\n"
  end
end

#codespan(text) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/jazzy/jazzy_markdown.rb', line 67

def codespan(text)
  case text
  when /^\$\$(.*)\$\$$/m
    o = ["</p><div class='math m-block'>",
         Regexp.last_match[1],
         '</div><p>']
    Markdown.has_math = true
  when /^\$(.*)\$$/m
    o = ["<span class='math m-inline'>", Regexp.last_match[1], '</span>']
    Markdown.has_math = true
  else
    o = ['<code>', text.to_s, '</code>']
  end

  o[0] + CGI.escapeHTML(o[1]) + o[2]
end

#header(text, header_level) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/jazzy/jazzy_markdown.rb', line 56

def header(text, header_level)
  text_slug = text.gsub(/[^[[:word:]]]+/, '-')
    .downcase
    .sub(/^-/, '')
    .sub(/-$/, '')

  "<h#{header_level} id='#{text_slug}' class='heading'>" \
    "#{text}" \
    "</h#{header_level}>\n"
end

#list(text, list_type) ⇒ Object



156
157
158
159
160
161
162
163
# File 'lib/jazzy/jazzy_markdown.rb', line 156

def list(text, list_type)
  elided = text.gsub!(ELIDED_LI_TOKEN, '')
  return if text =~ /\A\s*\Z/ && elided

  tag = list_type == :ordered ? 'ol' : 'ul'
  "\n<#{tag}>\n#{text}</#{tag}>\n"
    .gsub(%r{\n?<ul>\n?</ul>}, '')
end

#list_item(text, _list_type) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/jazzy/jazzy_markdown.rb', line 130

def list_item(text, _list_type)
  if text =~ SPECIAL_LIST_TYPE_REGEX
    type = Regexp.last_match(2)
    if UNIQUELY_HANDLED_CALLOUTS.include? type.downcase
      return ELIDED_LI_TOKEN
    end

    return render_list_aside(type,
                             text.sub(/#{Regexp.escape(type)}:\s+/, ''))
  end
  "<li>#{text.strip}</li>\n"
end

#render_aside(type, text) ⇒ Object



147
148
149
150
151
152
153
154
# File 'lib/jazzy/jazzy_markdown.rb', line 147

def render_aside(type, text)
  <<-HTML
<div class="aside aside-#{type.underscore.tr('_', '-')}">
    <p class="aside-title">#{type.underscore.humanize}</p>
    #{text}
</div>
  HTML
end

#render_list_aside(type, text) ⇒ Object



143
144
145
# File 'lib/jazzy/jazzy_markdown.rb', line 143

def render_list_aside(type, text)
  "</ul>#{render_aside(type, text).chomp}<ul>\n"
end

#rouge_formatter(lexer) ⇒ Object



194
195
196
# File 'lib/jazzy/jazzy_markdown.rb', line 194

def rouge_formatter(lexer)
  Highlighter::Formatter.new(lexer.tag)
end