Sha256: 638f40c9a4ee2f30c81f9753621c461ed28d6f5e17fcf894965e65d9879e1d12

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

require "github/markup/implementation"

module GitHub
  module Markup
    class Markdown < Implementation
      MARKDOWN_GEMS = {
        "commonmarker" => proc { |content, options: {}|
          commonmarker_opts = [:GITHUB_PRE_LANG].concat(options.fetch(:commonmarker_opts, []))
          CommonMarker.render_html(content, commonmarker_opts, [:tagfilter, :autolink, :table, :strikethrough])
        },
        "github/markdown" => proc { |content, options: {}|
          GitHub::Markdown.render(content)
        },
        "redcarpet" => proc { |content, options: {}|
          Redcarpet::Markdown.new(Redcarpet::Render::HTML).render(content)
        },
        "rdiscount" => proc { |content, options: {}|
          RDiscount.new(content).to_html
        },
        "maruku" => proc { |content, options: {}|
          Maruku.new(content).to_html
        },
        "kramdown" => proc { |content, options: {}|
          Kramdown::Document.new(content).to_html
        },
        "bluecloth" => proc { |content, options: {}|
          BlueCloth.new(content).to_html
        },
      }

      def initialize
        super(
          /md|mkdn?|mdwn|mdown|markdown|litcoffee/i,
          ["Markdown", "Literate CoffeeScript"])
      end

      def load
        return if @renderer
        MARKDOWN_GEMS.each do |gem_name, renderer|
          if try_require(gem_name)
            @renderer = renderer
            return
          end
        end
        raise LoadError, "no suitable markdown gem found"
      end

      def render(filename, content, options: {})
        load
        @renderer.call(content, options: options)
      end

      def name
        "markdown"
      end

    private
      def try_require(file)
        require file
        true
      rescue LoadError
        false
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
github-markup-3.0.4 lib/github/markup/markdown.rb
github-markup-3.0.3 lib/github/markup/markdown.rb
github-markup-3.0.2 lib/github/markup/markdown.rb
github-markup-3.0.1 lib/github/markup/markdown.rb