Class: Jazzy::Markdown::JazzyHTML

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

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
ELIDED_LI_TOKEN =
'7wNVzLB0OYPL2eGlPKu8q4vITltqh0Y6DPZf659TPMAeYh49o'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#default_languageObject

Returns the value of attribute default_language.



11
12
13
# File 'lib/jazzy/jazzy_markdown.rb', line 11

def default_language
  @default_language
end

Instance Method Details

#block_code(code, language) ⇒ Object



102
103
104
# File 'lib/jazzy/jazzy_markdown.rb', line 102

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

#header(text, header_level) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/jazzy/jazzy_markdown.rb', line 13

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



92
93
94
95
96
97
98
99
100
# File 'lib/jazzy/jazzy_markdown.rb', line 92

def list(text, list_type)
  elided = text.gsub!(ELIDED_LI_TOKEN, '')
  return if text =~ /\A\s*\Z/ && elided
  str = "\n"
  str << (list_type == :ordered ? "<ol>\n" : "<ul>\n")
  str << text
  str << (list_type == :ordered ? "</ol>\n" : "</ul>\n")
  str.gsub(%r{\n?<ul>\n<\/ul>}, '')
end

#list_item(text, _list_type) ⇒ Object



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

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_aside(type, text.sub(/#{Regexp.escape(type)}:\s+/, ''))
  end
  str = '<li>'
  str << text.strip
  str << "</li>\n"
end

#render_aside(type, text) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/jazzy/jazzy_markdown.rb', line 83

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

#rouge_formatter(lexer) ⇒ Object



106
107
108
# File 'lib/jazzy/jazzy_markdown.rb', line 106

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