require 'redcarpet'
require 'rouge'
require 'rouge/plugins/redcarpet'
module Jazzy
module Markdown
# Publish if generated HTML needs math support
class << self; attr_accessor :has_math; end
class JazzyHTML < Redcarpet::Render::HTML
include Redcarpet::Render::SmartyPants
include Rouge::Plugins::Redcarpet
attr_accessor :default_language
def header(text, header_level)
text_slug = text.gsub(/[^[[:word:]]]+/, '-')
.downcase
.sub(/^-/, '')
.sub(/-$/, '')
"
', text, '
']
end
o[0] + CGI.escapeHTML(o[1]) + o[2]
end
# List from
# https://github.com/apple/swift/blob/master/include/swift/Markup/SimpleFields.def
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
(\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 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 = '
#{type.underscore.humanize}
#{text}\s*)?)'.freeze
OUTRO_PAT = '(? #{body} ')
# call smartypants for pretty quotes etc.
postprocess(body)
end
end
def self.renderer
@renderer ||= JazzyDeclarationHTML.new
end
def self.markdown
@markdown ||= Redcarpet::Markdown.new(renderer, REDCARPET_OPTIONS)
end
def self.render(markdown_text, default_language = nil)
renderer.reset
renderer.default_language = default_language
markdown.render(markdown_text)
end
def self.rendered_returns
renderer.returns
end
def self.rendered_parameters
renderer.parameters
end
class JazzyCopyright < Redcarpet::Render::HTML
def link(link, _title, content)
%(#{content})
end
end
def self.copyright_markdown
@copyright_markdown ||= Redcarpet::Markdown.new(
JazzyCopyright,
REDCARPET_OPTIONS,
)
end
def self.render_copyright(markdown_text)
copyright_markdown.render(markdown_text)
end
end
end