require "html/pipeline" class AdditionalMarkdownFilter < HTML::Pipeline::MarkdownFilter def initialize(text, context = nil, result = nil) # do preprocessing, then call HTML::Pipeline::Markdown format_command_line! text format_helper! text super text, context, result end def call # initialize HTML::Pipeline::Markdown, then do post-processing html = super format_intro! html format_os_blocks! html format_admonition! html html end def format_command_line!(text) text.gsub! /\n?``` command-line(.+?)```/m do |block| block.gsub! /^``` command-line/, '
'
      block.gsub! /^```$/, "
\n" block.gsub!(/^\$ (.+)$/) { %Q|#{$1.rstrip}| } block.gsub!(/^(\# .+)$/) { %Q|#{$1.rstrip}| } block.gsub!(/^> (.+)$/) { %Q|# #{$1.rstrip}| } block end end def format_helper!(text) text.gsub! /\n?``` helper(.+?)```/m do |block| block.gsub! /^``` helper\s*/, '' block.gsub! /^```$/, '' header = '' block.gsub! /^#### (.+?)$/ do header = $1.strip '' end content = block.strip content = "

#{content}

" unless content =~ /^

{{#intro}}<\/p>/, '

') html.gsub!(/

{{\/intro}}<\/p>/, '

') end def format_os_blocks!(html) html.gsub!(/

{{#mac}}<\/p>/, '

') html.gsub!(/

{{#windows}}<\/p>/, '

') html.gsub!(/

{{#linux}}<\/p>/, '

') html.gsub!(/

{{#all}}<\/p>/, '

') html.gsub!(/

{{\/(mac|windows|linux|all)}}<\/p>/, '

') end def format_admonition!(html) html.gsub!(/

{{#(tip|warning|error)}}<\/p>/, '

') html.gsub!(/

{{\/(tip|warning|error)}}<\/p>/, '

') end end