Sha256: 298978136e06d0c65dae93d92a4b3bb019ac88c07f7d82cdac8a82eae056eab8

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

require "tempfile"

module Gorgyrella
  module Formatters
    class Pygments
      def self.highlight(text, lexer, format)
        tempfile = Tempfile.new("pygments")
        tempfile.write(text)
        tempfile.flush
        
        raise "lexer is none!" if lexer =~ /none/
        raise "lexer is empty!" if lexer.empty?
        raise "lexer has spaces! '#{lexer}' #{text}" if lexer =~ /\s/
        raise "format has spaces! '#{format}' #{text}" if format =~ /\s/

        cmd = "pygmentize -l #{lexer} -f #{format} #{tempfile.path}"
        `#{cmd}`
      end
      
      def self.resolve_lexer(file, language = nil, sample_text = "")
        if language && !language.empty?
          language
        else
          pygments_guess_lexer_file = File.join(File.dirname(__FILE__), "pygments_guess_lexer.py")
          pygments_lexer = `python #{pygments_guess_lexer_file} #{file}`.chomp
          pygments_lexer.empty? ? 'text' : pygments_lexer
        end
      end

      def self.resolve_format(format, parent_filename = nil, first_line = 0)
        format || 'html'
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gorgyrella-0.0.1 lib/gorgyrella/formatters/pygments.rb
gorgyrella-0.0.2 lib/gorgyrella/formatters/pygments.rb