Sha256: 2e87fad027e942cb86ce7cea551e6ec7495f3e17edb531e3dd7e83b8b6d1a21a

Contents?: true

Size: 1.11 KB

Versions: 206

Compression:

Stored size: 1.11 KB

Contents

require 'nokogiri'
require "coderay"

module CodeRay
  class << self
    alias :orig_scan :scan
    # Override the coderay scan method to remove escaped underscores prior
    # to passing over
    def scan(*args)
      args.first.gsub!("\\_", "_")
      orig_scan(*args)
    end
  end
end

class CodeblocksFilter < Nanoc::Filter
  identifier :codeblocks

  def run(content, params={})
    doc = Nokogiri::HTML.fragment(content)
    # Remove any escaped underscores from code blocks that are not marked
    # up by coderay
    doc.search('code').each do |n|
      n.content = n.content.gsub("\\_", "_")
    end
    # Same for coderay markup, not sure if this will apply always,
    # but seems to deal with highlighted Ruby
    # Keeping for future reference, but now replaced by remove underscores
    # prior to passing to coderay above
    #doc.search('.CodeRay .code').each do |n|
    #  n.xpath("//text()[. = '\\_']").each do |n|
    #    n.content = n.content.gsub("\\_", "_")
    #  end
    #  n.xpath("//text()[. = '\\']").each do |n|
    #    n.content = n.content.gsub("\\", "")
    #  end
    #end
    doc.to_html
  end

end

Version data entries

206 entries across 206 versions & 1 rubygems

Version Path
origen-0.42.1 templates/nanoc/lib/codeblocks_filter.rb
origen-0.42.0 templates/nanoc/lib/codeblocks_filter.rb
origen-0.41.2 templates/nanoc/lib/codeblocks_filter.rb
origen-0.41.1 templates/nanoc/lib/codeblocks_filter.rb
origen-0.41.0 templates/nanoc/lib/codeblocks_filter.rb
origen-0.40.2 templates/nanoc/lib/codeblocks_filter.rb
origen-0.40.1 templates/nanoc/lib/codeblocks_filter.rb
origen-0.40.0 templates/nanoc/lib/codeblocks_filter.rb
origen-0.38.0 templates/nanoc/lib/codeblocks_filter.rb
origen-0.37.0 templates/nanoc/lib/codeblocks_filter.rb
origen-0.36.1 templates/nanoc/lib/codeblocks_filter.rb
origen-0.36.0 templates/nanoc/lib/codeblocks_filter.rb
origen-0.35.1 templates/nanoc/lib/codeblocks_filter.rb
origen-0.35.0 templates/nanoc/lib/codeblocks_filter.rb
origen-0.34.3 templates/nanoc/lib/codeblocks_filter.rb
origen-0.34.2 templates/nanoc/lib/codeblocks_filter.rb
origen-0.34.1 templates/nanoc/lib/codeblocks_filter.rb
origen-0.34.0 templates/nanoc/lib/codeblocks_filter.rb
origen-0.33.3 templates/nanoc/lib/codeblocks_filter.rb
origen-0.33.2 templates/nanoc/lib/codeblocks_filter.rb