Sha256: 1da5c29036ea7d5be2e28328979c0e61d7f5955cd2ad2936d99912c09d928585

Contents?: true

Size: 1.48 KB

Versions: 49

Compression:

Stored size: 1.48 KB

Contents

require 'digest/md5'
module Haml::Filters
  remove_filter("Markdown")

  module Markdown
    include Haml::Filters::Base
    # require "rdiscount"

    def render(text)
      return "" if text.nil?

      text = text.encode('UTF-8')
      kramdown text
    end

    def redcarpet(text)
      markdown = Redcarpet::Markdown.new(
        Redcarpet::Render::HTML, 
        no_intra_emphasis: true,
        tables: true,
        fenced_code_blocks: true,
        autolink: true, 
        disable_indented_code_blocks: true, 
        strikethrough: true, 
        lax_spacing: true, 
        space_after_headers: true, 
        superscript: true, 
        # underline: true, 
        highlight: true, 
        quote: true, 
        footnotes: true, 
      )
      markdown.render text
    end

    def kramdown(text)
      Kramdown::Document.new(text).to_html
    end

    # taken from https://help.github.com/articles/github-flavored-markdown

    def gfm(text)
      # Extract pre blocks
      extractions = {}
      text.gsub!(%r{<pre>.*?</pre>}m) do |match|
        md5 = Digest::MD5.hexdigest(match)
        extractions[md5] = match
        "{gfm-extraction-#{md5}}"
      end

      # in very clear cases, let newlines become <br /> tags
      text.gsub!(/^[\w\<][^\n]*\n+/) do |x|
        x =~ /\n{2}/ ? x : (x.strip!; x << "  \n")
      end

      # Insert pre block extractions
      text.gsub!(/\{gfm-extraction-([0-9a-f]{32})\}/) do
        "\n\n" + extractions[$1]
      end

      text
    end
  end

end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
tawork-0.0.50 config/initializers/haml_gfm.rb
tawork-0.0.49 config/initializers/haml_gfm.rb
tawork-0.0.48 config/initializers/haml_gfm.rb
tawork-0.0.47 config/initializers/haml_gfm.rb
tawork-0.0.46 config/initializers/haml_gfm.rb
tawork-0.0.45 config/initializers/haml_gfm.rb
tawork-0.0.44 config/initializers/haml_gfm.rb
tawork-0.0.43 config/initializers/haml_gfm.rb
tawork-0.0.42 config/initializers/haml_gfm.rb
tawork-0.0.41 config/initializers/haml_gfm.rb
tawork-0.0.40 config/initializers/haml_gfm.rb
tawork-0.0.39 config/initializers/haml_gfm.rb
tawork-0.0.38 config/initializers/haml_gfm.rb
tawork-0.0.37 config/initializers/haml_gfm.rb
tawork-0.0.36 config/initializers/haml_gfm.rb
tawork-0.0.35 config/initializers/haml_gfm.rb
tawork-0.0.34 config/initializers/haml_gfm.rb
tawork-0.0.33 config/initializers/haml_gfm.rb
tawork-0.0.31 config/initializers/haml_gfm.rb
tawork-0.0.30 config/initializers/haml_gfm.rb