class Rouge::CLI::Highlight

Attributes

escape[R]
formatter[R]
input_file[R]
lexer_name[R]
mimetype[R]

Public Class Methods

desc() click to toggle source
# File lib/rouge/cli.rb, line 163
def self.desc
  "highlight code"
end
doc() { |%[usage: rougify highlight <filename> [options...]]| ... } click to toggle source
# File lib/rouge/cli.rb, line 167
def self.doc
  return enum_for(:doc) unless block_given?

  yield %[usage: rougify highlight <filename> [options...]]
  yield %[       rougify highlight [options...]]
  yield %[]
  yield %[--input-file|-i <filename>  specify a file to read, or - to use stdin]
  yield %[]
  yield %[--lexer|-l <lexer>          specify the lexer to use.]
  yield %[                            If not provided, rougify will try to guess]
  yield %[                            based on --mimetype, the filename, and the]
  yield %[                            file contents.]
  yield %[]
  yield %[--formatter|-f <opts>       specify the output formatter to use.]
  yield %[                            If not provided, rougify will default to]
  yield %[                            terminal256.]
  yield %[]
  yield %[--theme|-t <theme>          specify the theme to use for highlighting]
  yield %[                            the file. (only applies to some formatters)]
  yield %[]
  yield %[--mimetype|-m <mimetype>    specify a mimetype for lexer guessing]
  yield %[]
  yield %[--lexer-opts|-L <opts>      specify lexer options in CGI format]
  yield %[                            (opt1=val1&opt2=val2)]
  yield %[]
  yield %[--formatter-opts|-F <opts>  specify formatter options in CGI format]
  yield %[                            (opt1=val1&opt2=val2)]
  yield %[]
  yield %[--require|-r <filename>     require a filename or library before]
  yield %[                            highlighting]
  yield %[]
  yield %[--escape                    allow the use of escapes between <! and !>]
  yield %[]
  yield %[--escape-with <l> <r>       allow the use of escapes between custom]
  yield %[                            delimiters. implies --escape]
end
new(opts={}) click to toggle source
# File lib/rouge/cli.rb, line 295
def initialize(opts={})
  Rouge::Lexer.enable_debug!

  opts[:requires].each do |r|
    require r
  end

  @input_file = opts[:input_file]

  if opts[:lexer]
    @lexer_class = Lexer.find(opts[:lexer]) \
      or error! "unknown lexer #{opts[:lexer].inspect}"
  else
    @lexer_name = opts[:lexer]
    @mimetype = opts[:mimetype]
  end

  @lexer_opts = opts[:lexer_opts]

  theme = Theme.find(opts[:theme]).new or error! "unknown theme #{opts[:theme]}"

  # TODO: document this in --help
  @formatter = case opts[:formatter]
  when 'terminal256' then Formatters::Terminal256.new(theme)
  when 'terminal-truecolor' then Formatters::TerminalTruecolor.new(theme)
  when 'html' then Formatters::HTML.new
  when 'html-pygments' then Formatters::HTMLPygments.new(Formatters::HTML.new, opts[:css_class])
  when 'html-inline' then Formatters::HTMLInline.new(theme)
  when 'html-line-table' then Formatters::HTMLLineTable.new(Formatters::HTML.new)
  when 'html-table' then Formatters::HTMLTable.new(Formatters::HTML.new)
  when 'null', 'raw', 'tokens' then Formatters::Null.new
  when 'tex' then Formatters::Tex.new
  else
    error! "unknown formatter preset #{opts[:formatter]}"
  end

  @escape = opts[:escape]
end
parse(argv) click to toggle source
# File lib/rouge/cli.rb, line 217
def self.parse(argv)
  opts = {
    :formatter => supports_truecolor? ? 'terminal-truecolor' : 'terminal256',
    :theme => 'thankful_eyes',
    :css_class => 'codehilite',
    :input_file => '-',
    :lexer_opts => {},
    :formatter_opts => {},
    :requires => [],
  }

  until argv.empty?
    arg = argv.shift
    case arg
    when '-r', '--require'
      opts[:requires] << argv.shift
    when '--input-file', '-i'
      opts[:input_file] = argv.shift
    when '--mimetype', '-m'
      opts[:mimetype] = argv.shift
    when '--lexer', '-l'
      opts[:lexer] = argv.shift
    when '--formatter-preset', '-f'
      opts[:formatter] = argv.shift
    when '--theme', '-t'
      opts[:theme] = argv.shift
    when '--css-class', '-c'
      opts[:css_class] = argv.shift
    when '--lexer-opts', '-L'
      opts[:lexer_opts] = parse_cgi(argv.shift)
    when '--escape'
      opts[:escape] = ['<!', '!>']
    when '--escape-with'
      opts[:escape] = [argv.shift, argv.shift]
    when /^--/
      error! "unknown option #{arg.inspect}"
    else
      opts[:input_file] = arg
    end
  end

  new(opts)
end
supports_truecolor?() click to toggle source

There is no consistent way to do this, but this is used elsewhere, and we provide explicit opt-in and opt-out with $COLORTERM

# File lib/rouge/cli.rb, line 206
def self.supports_truecolor?
  return true if %w(24bit truecolor).include?(ENV['COLORTERM'])
  return false if ENV['COLORTERM'] && ENV['COLORTERM'] =~ /256/

  if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
    ENV['ConEmuANSI'] == 'ON' && !ENV['ANSICON']
  else
    ENV['TERM'] !~ /(^rxvt)|(-color$)/
  end
end

Private Class Methods

parse_cgi(str) click to toggle source
# File lib/rouge/cli.rb, line 340
def self.parse_cgi(str)
  pairs = CGI.parse(str).map { |k, v| [k.to_sym, v.first] }
  Hash[pairs]
end

Public Instance Methods

escape_lexer() click to toggle source
# File lib/rouge/cli.rb, line 281
def escape_lexer
  Rouge::Lexers::Escape.new(
    start: @escape[0],
    end: @escape[1],
    lang: raw_lexer,
  )
end
input() click to toggle source
# File lib/rouge/cli.rb, line 265
def input
  @input ||= input_stream.read
end
input_stream() click to toggle source
# File lib/rouge/cli.rb, line 261
def input_stream
  @input_stream ||= FileReader.new(@input_file)
end
lexer() click to toggle source
# File lib/rouge/cli.rb, line 289
def lexer
  @lexer ||= @escape ? escape_lexer : raw_lexer
end
lexer_class() click to toggle source
# File lib/rouge/cli.rb, line 269
def lexer_class
  @lexer_class ||= Lexer.guess(
    :filename => @input_file,
    :mimetype => @mimetype,
    :source => input_stream,
  )
end
raw_lexer() click to toggle source
# File lib/rouge/cli.rb, line 277
def raw_lexer
  lexer_class.new(@lexer_opts)
end
run() click to toggle source
# File lib/rouge/cli.rb, line 334
def run
  Formatter.enable_escape! if @escape
  formatter.format(lexer.lex(input), &method(:print))
end