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}" \ "\n" end def codespan(text) if /^\$\$(.*)\$\$$/m =~ text o = ["
", Regexp.last_match[1], '
'] Markdown.has_math = true elsif /^\$(.*)\$$/m =~ text o = ["", Regexp.last_match[1], ''] Markdown.has_math = true else o = ['', 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 = '

  • ' str << text.strip str << "
  • \n" end def render_aside(type, text) <<-HTML

    #{type.underscore.humanize}

    #{text}
    \n") str.gsub(%r{\n?