Class: Gorgyrella::Formatters::Pygments

Inherits:
Object
  • Object
show all
Defined in:
lib/gorgyrella/formatters/pygments.rb

Class Method Summary collapse

Class Method Details

.highlight(text, lexer, format) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/gorgyrella/formatters/pygments.rb', line 6

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

.resolve_format(format, parent_filename = nil, first_line = 0) ⇒ Object



30
31
32
# File 'lib/gorgyrella/formatters/pygments.rb', line 30

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

.resolve_lexer(file, language = nil, sample_text = "") ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/gorgyrella/formatters/pygments.rb', line 20

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