require "redcarpet"
require "coderay"

module Highlighter
  class << self
    def registered(app)
      app.helpers Helpers
    end
    alias :included :registered
  end

  module Helpers
    def _highlight(string, language, class_name=nil)
      result = %Q{<div class="highlight #{language} #{class_name}">}
      result += '<div class="ribbon"></div>'
      result += '<div class="scroller">'
      code = CodeRay.scan(string, language)
      result += code.div css: :class,
                      line_numbers: :table,
                      line_number_anchors: false
      result += '</div>'
      result += %Q{</div>}
      result
    end

    def highlight(language, class_name, &block)
      concat(_highlight(capture(&block), language, class_name))
    end
  end

  class HighlightedHTML < Redcarpet::Render::HTML
    include Helpers

    def header(text, level)
      "<h#{level} class='anchorable-toc' id='toc_#{TOC::TableOfContents.anchorify(text)}'>#{text}</h#{level}>"
    end

    def block_code(code, language)
      _highlight(code, language)
    end
  end
end

::Middleman::Extensions.register(:highlighter, Highlighter)

require "redcarpet"
require "coderay"

module Highlighter
  class << self
    def registered(app)
      app.helpers Helpers
    end
    alias :included :registered
  end

  module Helpers
    def _highlight(string, language, class_name=nil)
      result = %Q{<div class="highlight #{language} #{class_name}">}
      result += '<div class="ribbon"></div>'
      result += '<div class="scroller">'
      code = CodeRay.scan(string, language)
      result += code.div css: :class,
                      line_numbers: :table,
                      line_number_anchors: false
      result += '</div>'
      result += %Q{</div>}
      result
    end

    def highlight(language, class_name, &block)
      concat(_highlight(capture(&block), language, class_name))
    end
  end

  class HighlightedHTML < Redcarpet::Render::HTML
    include Helpers

    def preprocess(raw)
      # Twitter-linkify @usernames
      nontwitters = %w{ @each }
      raw.gsub(/@\w+/) do |username|
        if nontwitters.include?(username)
          username
        else
          username[0] = ''
          "[@#{username}](https://twitter.com/#{username})"
        end
      end
    end

    def header(text, level)
      "<h#{level} class='anchorable-toc' id='toc_#{TOC::TableOfContents.anchorify(text)}'>#{text}</h#{level}>"
    end

    def block_code(code, language)
      _highlight(code, language)
    end
  end
end

::Middleman::Extensions.register(:highlighter, Highlighter)

Convert twitter names to links in .md files
Convert twitter names to links in .md files

Except for @each, of course
