Sha256: 12f1bd526773ebdcc56b9345f3f8b1a946304cc0113bf8f4c3ccc5e4d0b8c004

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require "rubygems"
begin
  require "rdiscount"
rescue LoadError
  require "kramdown"
end
require "cgi"
require "tempfile"

module Murdoc
  class Paragraph
    attr_accessor :source
    attr_accessor :annotation
    attr_accessor :source_type
    attr_accessor :starting_line
    attr_accessor :options

    def initialize(source, annotation, starting_line = 0, source_type = nil, options ={})
      self.source = source
      self.annotation = annotation
      self.starting_line = starting_line
      self.source_type = source_type
      self.options = options
    end

    def source_type
      @source_type
    end

    def source_type=(source_type)
      @source_type = source_type.to_s
    end


    def formatted_annotation
      if defined?(Markdown)
        Markdown.new(annotation, :smart).to_html
      else
        Kramdown::Document.new(annotation, :input => :markdown).to_html
      end
    end

    def formatted_source
      @formatted_source ||= if pygments_installed? && options[:highlight_source]
        IO.popen("pygmentize -l #{source_type} -O encoding=UTF8 -f html -O nowrap", "w+") do |pipe|
          pipe.puts source
          pipe.close_write
          pipe.read
        end
      else
        CGI.escapeHTML(source)
      end
    end

    protected
    def pygments_installed?
      @@pygments_installed ||= ENV['PATH'].split(':').any? { |dir| File.exist?("#{dir}/pygmentize") }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
murdoc-0.1.13 lib/murdoc/paragraph.rb
murdoc-0.1.12 lib/murdoc/paragraph.rb