Sha256: a49eff1caa17fbe94a82238203e29e7ee028043339cec7eaee4902af45b7315d

Contents?: true

Size: 1.16 KB

Versions: 47

Compression:

Stored size: 1.16 KB

Contents

require 'wunderbar'
require 'coderay'
require 'nokogiri'

# workaround for https://github.com/rubychan/coderay/pull/159
module CodeRay::PluginHost
  alias_method :old_plugin_path, :plugin_path
  def plugin_path *args
    args.first.untaint if args.first == CodeRay::CODERAY_PATH
    old_plugin_path *args
  end
end

module Wunderbar
  class HtmlMarkup
    def _coderay(*args)

      # allow arguments in any order, disambiguate based on type
      lang, string, attrs = :ruby, '', {}
      args.each do |arg|
        case arg
          when Symbol; lang = arg
          when String; string = arg
          when Hash;   attrs = arg
        end
      end

      base = _{ CodeRay.scan(CDATANode.normalize(string), lang).div }

      # remove wrapping divs
      while base.length == 1 and base.first.name == 'div'
        div = base.first.parent.children.pop
        div.children.each {|child| child.parent = base.first.parent}
        base.first.parent.children += base.first.children
        base = div.children
      end

      # add attrs provided to pre element
      if base.length == 1 and base.first.name == 'pre'
        base.first.attrs.merge! attrs
      end
    end
  end
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
wunderbar-1.0.21 lib/wunderbar/coderay.rb
wunderbar-1.0.20 lib/wunderbar/coderay.rb
wunderbar-1.0.19 lib/wunderbar/coderay.rb
wunderbar-1.0.18 lib/wunderbar/coderay.rb
wunderbar-1.0.17 lib/wunderbar/coderay.rb
wunderbar-1.0.16 lib/wunderbar/coderay.rb
wunderbar-1.0.15 lib/wunderbar/coderay.rb
wunderbar-1.0.14 lib/wunderbar/coderay.rb
wunderbar-1.0.13 lib/wunderbar/coderay.rb
wunderbar-1.0.12 lib/wunderbar/coderay.rb
wunderbar-1.0.11 lib/wunderbar/coderay.rb
wunderbar-1.0.10 lib/wunderbar/coderay.rb
wunderbar-1.0.9 lib/wunderbar/coderay.rb
wunderbar-1.0.8 lib/wunderbar/coderay.rb
wunderbar-1.0.7 lib/wunderbar/coderay.rb
wunderbar-1.0.6 lib/wunderbar/coderay.rb
wunderbar-1.0.5 lib/wunderbar/coderay.rb
wunderbar-1.0.4 lib/wunderbar/coderay.rb
wunderbar-1.0.3 lib/wunderbar/coderay.rb
wunderbar-1.0.2 lib/wunderbar/coderay.rb