Sha256: dfcad4871d51dbc3351f2a160fa540b186ff1b38b2c431675fbb293c06471583

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

require 'html/pipeline'
require 'filters/filters'
require 'nokogiri'
require 'jekyll-override' unless defined?(Jekyll).nil?

class ExtendedMarkdownFilter < HTML::Pipeline::MarkdownFilter
  include Filters::PreFilter
  include Filters::PostFilter

  EMF_CURLY_TAGS = %w(intro mac windows linux all tip note warning danger).join('|')

  def initialize(text, context = nil, result = nil)
    if context[:emf_use_blocks]
      text = self.class.convert_curly_to_bracket(text)
      @front_wrap = "\\[\\["
      @end_wrap = "\\]\\]"
      @wrap_symbol = "\\]"
    else
      @front_wrap = "\{\{"
      @end_wrap = "\}\}"
      @wrap_symbol = "}"
    end

    # do preprocessing, then call HTML::Pipeline::Markdown
    text = format_command_line    text
    text = format_helper          text

    super text, context, result
  end

  def self.convert_curly_to_bracket(text)
    return text if text.nil?
    text = text.gsub(/\{\{#(#{EMF_CURLY_TAGS})\}\}/, '[[#\1]]')
    text = text.gsub(/\{\{\/(#{EMF_CURLY_TAGS})\}\}/, '[[/\1]]')
    text = text.gsub(/\{\{ (octicon-\S+\s*[^\}]+) \}\}/,  '[[\1]]')

    text
  end

  def self.should_jekyll_replace?(site)
    html_pipeline_context = site.site_payload["site"]["html_pipeline"] && site.site_payload["site"]["html_pipeline"]["context"]
    pipeline_emf_context = site.site_payload["site"]["html_pipeline"]["context"][:emf_use_blocks] || site.site_payload["site"]["html_pipeline"]["context"]["emf_use_blocks"]
    site.site_payload["site"]["markdown"] == "HTMLPipeline" && html_pipeline_context && pipeline_emf_context
  end

  def call
    # initialize HTML::Pipeline::Markdown, then do post-processing
    html = super

    format_intro!           html
    format_os_blocks!       html
    format_admonitions!     html
    format_octicons!        html

    html
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
extended-markdown-filter-0.4.9 lib/extended-markdown-filter.rb
extended-markdown-filter-0.4.8 lib/extended-markdown-filter.rb
extended-markdown-filter-0.4.7 lib/extended-markdown-filter.rb
extended-markdown-filter-0.4.6 lib/extended-markdown-filter.rb