Sha256: afcc50d080f13fce94f8e08be2343d4a04a6e758305552fcb9f158bc98adf62f

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

require 'octopress-codeblock/version'
require 'octopress-code-highlighter'
require 'liquid'

module Octopress
  module Codeblock
    class Tag < Liquid::Block
      CaptionUrlTitle = /(\S[\S\s]*)\s+(https?:\/\/\S+|\/\S+)\s*(.+)?/i
      Caption = /(\S[\S\s]*)/

      def initialize(tag_name, markup, tokens)
        @markup = markup
        super
      end

      def render(context)
        code = super.strip.encode("UTF-8")

        begin
          highlight(code, get_options)
        rescue => e
          CodeHighlighter.highlight_failed(e, "{% codeblock [title] [url] [link text] [lang:language] [start:#] [mark:#,#-#] [linenos:false] %}\ncode\n{% endcodeblock %}", @markup, code)
        end
      end
        
      def get_options
        clean_markup = CodeHighlighter.clean_markup(@markup)
        defaults = {}

        if clean_markup =~ CaptionUrlTitle
          defaults = {
            title: $2,
            url: $2,
            link_text: $3,
          }
        elsif clean_markup =~ Caption
          defaults = {
            title: $1
          }
        end

        if defaults[:title] =~ /\S[\S\s]*\w+\.(\w+)/
          defaults[:lang] = $1
        end

        CodeHighlighter.parse_markup(@markup, defaults)
      end


      def highlight(code, options)
        options[:aliases] = @aliases || {}
        code = CodeHighlighter.highlight(code, options)
        code = "<notextile>#{code}</notextile>" if !@ext.nil? and @ext.match(/textile/)
        code
      end
    end
  end
end

Liquid::Template.register_tag('codeblock', Octopress::Codeblock::Tag)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
octopress-codeblock-1.0.4 lib/octopress-codeblock.rb
octopress-codeblock-1.0.3 lib/octopress-codeblock.rb