Sha256: 17e5e41082f99f36786e785d94ac2e921ce9cc708b5cd0bd99f1bed900bb0ed3

Contents?: true

Size: 1.85 KB

Versions: 3

Compression:

Stored size: 1.85 KB

Contents

#require 'cgi'
require 'albino'

module Eggplant
  class Markdown
    def initialize(markdown)
      @markdown = markdown
    end

    def to_html
      highlight!
      #CGI.unescapeHTML 
      doc.search('body > *').to_html
    end

    private

    def doc
      @doc ||= Nokogiri::HTML(markup)
    end

    def highlight!
      doc.search('div.code').each do |div|
        lexer = div['rel'] || :javascript

        lexted_text = Albino.new(div.text, lexer).to_s

        highlighted = Nokogiri::HTML(lexted_text).at('div')

        klasses = highlighted['class'].split(/\s+/)
        klasses << lexer
        klasses << 'code'
        klasses << 'highlight'
        highlighted['class'] = klasses.join(' ')

        div.replace(highlighted)
      end
    end

    # def markup
    #   @markup ||= begin
    #     t = RDiscount.new(@markdown.dup).to_html
    #     t.gsub!(/^(?:<p>)?@@@(?:<\/p>)?$/, '</div>')
    #     t.gsub!(/^(?:<p>)?@@@\s*([\w\+]+)(?:<\/p>)?$/, '<div class="code" rel="\1">')
    #     t
    #   end
    # end

    # def highlight!
    #   doc.search('code[data-mode]').each do |code|
    #     if ['javascript', 'coffeescript'].include?(code['data-mode'])
    #       code['data-exec'] = 'true'
    #     end
    #     if code['data-mode'] == 'html'
    #       code['data-mode'] = 'htmlmixed'
    #     end
    #   end
    # end

    def markup
      @markup ||= begin
        contents = []
        markdown = @markdown.dup.gsub(/^@@@$/, '~@@@').gsub(/^\s*@@@\s*([\w\+]+)?$([^~]*)\n~@{3}$/) do |content|
          i = contents.size
          contents << $2 + "\n"
          '<div class="code" rel="' + $1 + '">%@' + i.to_s + '@%</div>'
        end
        t = RDiscount.new(markdown).to_html
        contents.size.times do |i|
          t.gsub!("%@#{i}@%", contents[i])
        end
        t.gsub(/<p>\.(.*?)[\s\n]/, '<p class="\1">')
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eggplant-0.2.2 lib/eggplant/markdown.rb
eggplant-0.2.1 lib/eggplant/markdown.rb
eggplant-0.2.0 lib/eggplant/markdown.rb