Class: Rouge::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/rouge/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.start(argv = ARGV, *a) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rouge/cli.rb', line 14

def self.start(argv=ARGV, *a)
  unless %w(highlight style).include?(argv.first)
    argv.unshift 'highlight'
  end

  super(argv, *a)
end

Instance Method Details

#highlight(file = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rouge/cli.rb', line 35

def highlight(file=nil)
  filename = options[:file] || file
  source = filename ? File.read(filename) : $stdin.read

  if options[:lexer].nil?
    lexer_class = Lexer.guess(
      :filename => filename,
      :mimetype => options[:mimetype],
      :source   => source,
    )
  else
    lexer_class = Lexer.find(options[:lexer])
    raise "unknown lexer: #{options[:lexer]}" unless lexer_class
  end

  # only HTML is supported for now
  formatter = Formatters::HTML.new(normalize_hash_keys(options[:formatter_opts]))
  lexer = lexer_class.new(normalize_hash_keys(options[:lexer_opts]))

  puts Rouge.highlight(source, lexer, formatter)
end

#style(theme_name = 'thankful_eyes') ⇒ Object



58
59
60
61
62
63
# File 'lib/rouge/cli.rb', line 58

def style(theme_name='thankful_eyes')
  theme = Theme.find(theme_name)
  raise "unknown theme: #{theme_name}" unless theme

  puts theme.new(options).render
end