Sha256: e154193b8fb294a3fac2ad89d1afee2dcf05b03b1b3955a2b29b20ab5546cf3a

Contents?: true

Size: 1.99 KB

Versions: 2

Compression:

Stored size: 1.99 KB

Contents

# encoding: utf-8
require 'redcarpet'

module HTML
  class Pipeline

    # LinuxFr Flavored Markdown
    class LFMarkdown < Redcarpet::Render::HTML
      attr_accessor :image_class

      PARSER_OPTIONS = {
        :no_intra_emphasis  => true,
        :tables             => true,
        :fenced_code_blocks => true,
        :autolink           => true,
        :strikethrough      => true,
        :superscript        => true
      }

      HTML_OPTIONS = {
        :filter_html        => true,
        :no_styles          => true,
        :hard_wrap          => true,
        :xhtml              => true
      }

      def initialize(extensions={})
        super extensions.merge(HTML_OPTIONS)
      end

      def header(text, header_level)
        l = header_level + 1
        "<h#{l}>#{text}</h#{l}>\n"
      end

      def strikethrough(text)
        "<s>#{text}</s>"
      end

      def image(link, title, alt_text)
        return "" if link.blank?
        ::Image.new(link, title, alt_text).to_html  # FIXME
      end

      def normal_text(text)
        text = CGI.escapeHTML(text)
        text.gsub!('« ', '«&nbsp;')
        text.gsub!(/ ([:;»!?])/, '&nbsp;\1')
        text.gsub!(' -- ', '—')
        text.gsub!('...', '…')
        text
      end

    end


    # HTML Filter that converts Markdown text into HTML and converts into a
    # DocumentFragment. This is different from most filters in that it can take a
    # non-HTML as input. It must be used as the first filter in a pipeline.
    #
    # This filter does not write any additional information to the context hash.
    class MarkdownFilter < TextFilter
      def initialize(text, context = nil, result = nil)
        super text, context, result
        @text = @text.gsub "\r", ''
      end

      # Convert Markdown to HTML using the best available implementation
      # and convert into a DocumentFragment.
      def call
        lfm = Redcarpet::Markdown.new LFMarkdown, LFMarkdown::PARSER_OPTIONS
        lfm.render @text
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
html-pipeline-linuxfr-0.14.1 lib/html/pipeline/markdown_filter.rb
html-pipeline-linuxfr-0.14.0 lib/html/pipeline/markdown_filter.rb