Sha256: 943a3181e9991d2fffcdd1df517635f0ac91ec6300523b39031327d9a5903164

Contents?: true

Size: 619 Bytes

Versions: 2

Compression:

Stored size: 619 Bytes

Contents

require 'albino'

module Jackb
  
  class Highlight
    def self.render(content)
      self.new.render(content)
    end
  
    def render(content)
      content.gsub(highlight_regex) do |s|
        Albino.new(unescape($2), extract_lang($1))
      end
    end
  
    private
    def unescape(content)
      CGI::unescapeHTML content
    end
  
    def extract_lang(attributes)
      result = :ruby
      attributes.gsub(/(language|lang)=('|")([0-z]+)('|")/i) do |s|
        result = $3.to_sym
      end
      result
    end
    
    def highlight_regex
      /<pre><code([^<>]*)>(.*?)<\/code><\/pre>/im
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jackb-0.0.5 lib/jackb/highlight.rb
jackb-0.0.4 lib/jackb/highlight.rb