Class: Rouge::CLI::Highlight

Inherits:
Rouge::CLI show all
Defined in:
lib/rouge/cli.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Rouge::CLI

class_from_arg, #error!, error!

Constructor Details

#initialize(opts = {}) ⇒ Highlight

Returns a new instance of Highlight.


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/rouge/cli.rb', line 240

def initialize(opts={})
  @input_file = opts[:input_file]

  if opts[:lexer]
    @lexer_class = Lexer.find(opts[:lexer]) \
      or error! "unkown 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]}"

  @formatter = case opts[:formatter]
  when 'terminal256' then Formatters::Terminal256.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-table' then Formatters::HTMLTable.new(Formatters::HTML.new)
  else
    error! "unknown formatter preset #{opts[:formatter]}"
  end
end

Instance Attribute Details

#formatterObject (readonly)

Returns the value of attribute formatter


238
239
240
# File 'lib/rouge/cli.rb', line 238

def formatter
  @formatter
end

#input_fileObject (readonly)

Returns the value of attribute input_file


238
239
240
# File 'lib/rouge/cli.rb', line 238

def input_file
  @input_file
end

#lexer_nameObject (readonly)

Returns the value of attribute lexer_name


238
239
240
# File 'lib/rouge/cli.rb', line 238

def lexer_name
  @lexer_name
end

#mimetypeObject (readonly)

Returns the value of attribute mimetype


238
239
240
# File 'lib/rouge/cli.rb', line 238

def mimetype
  @mimetype
end

Class Method Details

.descObject


151
152
153
# File 'lib/rouge/cli.rb', line 151

def self.desc
  "highlight code"
end

.doc {|%[usage: rougify highlight <filename> [options...]]| ... } ⇒ Object

Yields:

  • (%[usage: rougify highlight <filename> [options...]])

155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rouge/cli.rb', line 155

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 %[--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)]
end

.parse(argv) ⇒ Object


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/rouge/cli.rb', line 181

def self.parse(argv)
  opts = {
    :formatter => 'terminal256',
    :theme => 'thankful_eyes',
    :css_class => 'codehilite',
    :input_file => '-',
    :lexer_opts => {},
    :formatter_opts => {},
  }

  until argv.empty?
    arg = argv.shift
    case arg
    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 /^--/
      error! "unknown option #{arg.inspect}"
    else
      opts[:input_file] = arg
    end
  end

  new(opts)
end

Instance Method Details

#inputObject


222
223
224
# File 'lib/rouge/cli.rb', line 222

def input
  @input ||= input_stream.read
end

#input_streamObject


218
219
220
# File 'lib/rouge/cli.rb', line 218

def input_stream
  @input_stream ||= FileReader.new(@input_file)
end

#lexerObject


234
235
236
# File 'lib/rouge/cli.rb', line 234

def lexer
  @lexer ||= lexer_class.new(@lexer_opts)
end

#lexer_classObject


226
227
228
229
230
231
232
# File 'lib/rouge/cli.rb', line 226

def lexer_class
  @lexer_class ||= Lexer.guess(
    :filename => @input_file,
    :mimetype => @mimetype,
    :source => input_stream,
  )
end

#runObject


266
267
268
# File 'lib/rouge/cli.rb', line 266

def run
  formatter.format(lexer.lex(input), &method(:print))
end