Class: Rouge::CLI::Highlight
- Inherits:
-
Rouge::CLI
- Object
- Rouge::CLI
- Rouge::CLI::Highlight
- Defined in:
- lib/rouge/cli.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#escape ⇒ Object
readonly
Returns the value of attribute escape.
-
#formatter ⇒ Object
readonly
Returns the value of attribute formatter.
-
#input_file ⇒ Object
readonly
Returns the value of attribute input_file.
-
#lexer_name ⇒ Object
readonly
Returns the value of attribute lexer_name.
-
#mimetype ⇒ Object
readonly
Returns the value of attribute mimetype.
Class Method Summary collapse
- .desc ⇒ Object
- .doc {|%[usage: rougify highlight <filename> [options...]]| ... } ⇒ Object
- .parse(argv) ⇒ Object
- .parse_opts(argv) ⇒ Object
-
.supports_truecolor? ⇒ Boolean
There is no consistent way to do this, but this is used elsewhere, and we provide explicit opt-in and opt-out with $COLORTERM.
Instance Method Summary collapse
- #escape_lexer ⇒ Object
-
#initialize(opts = {}) ⇒ Highlight
constructor
A new instance of Highlight.
- #input ⇒ Object
- #input_stream ⇒ Object
- #lexer ⇒ Object
- #lexer_class ⇒ Object
- #raw_lexer ⇒ Object
- #run ⇒ Object
Methods inherited from Rouge::CLI
class_from_arg, #error!, error!
Constructor Details
#initialize(opts = {}) ⇒ Highlight
Returns a new instance of Highlight.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
# File 'lib/rouge/cli.rb', line 306 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 |
Instance Attribute Details
#escape ⇒ Object (readonly)
Returns the value of attribute escape.
304 305 306 |
# File 'lib/rouge/cli.rb', line 304 def escape @escape end |
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
304 305 306 |
# File 'lib/rouge/cli.rb', line 304 def formatter @formatter end |
#input_file ⇒ Object (readonly)
Returns the value of attribute input_file.
304 305 306 |
# File 'lib/rouge/cli.rb', line 304 def input_file @input_file end |
#lexer_name ⇒ Object (readonly)
Returns the value of attribute lexer_name.
304 305 306 |
# File 'lib/rouge/cli.rb', line 304 def lexer_name @lexer_name end |
#mimetype ⇒ Object (readonly)
Returns the value of attribute mimetype.
304 305 306 |
# File 'lib/rouge/cli.rb', line 304 def mimetype @mimetype end |
Class Method Details
.desc ⇒ Object
167 168 169 |
# File 'lib/rouge/cli.rb', line 167 def self.desc "highlight code" end |
.doc {|%[usage: rougify highlight <filename> [options...]]| ... } ⇒ Object
171 172 173 174 175 176 177 178 179 180 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 |
# File 'lib/rouge/cli.rb', line 171 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-preset|-f <opts> specify the output formatter to use.] yield %[ If not provided, rougify will default to] yield %[ terminal256. options are: terminal256,] yield %[ terminal-truecolor, html, html-pygments,] yield %[ html-inline, html-line-table, html-table,] yield %[ null/raw/tokens, or tex.] 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 |
.parse(argv) ⇒ Object
268 269 270 |
# File 'lib/rouge/cli.rb', line 268 def self.parse(argv) new(parse_opts(argv)) end |
.parse_opts(argv) ⇒ Object
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 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 265 266 |
# File 'lib/rouge/cli.rb', line 224 def self.parse_opts(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 opts end |
.supports_truecolor? ⇒ Boolean
There is no consistent way to do this, but this is used elsewhere, and we provide explicit opt-in and opt-out with $COLORTERM
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/rouge/cli.rb', line 213 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 |
Instance Method Details
#escape_lexer ⇒ Object
292 293 294 295 296 297 298 |
# File 'lib/rouge/cli.rb', line 292 def escape_lexer Rouge::Lexers::Escape.new( start: @escape[0], end: @escape[1], lang: raw_lexer, ) end |
#input ⇒ Object
276 277 278 |
# File 'lib/rouge/cli.rb', line 276 def input @input ||= input_stream.read end |
#input_stream ⇒ Object
272 273 274 |
# File 'lib/rouge/cli.rb', line 272 def input_stream @input_stream ||= FileReader.new(@input_file) end |
#lexer ⇒ Object
300 301 302 |
# File 'lib/rouge/cli.rb', line 300 def lexer @lexer ||= @escape ? escape_lexer : raw_lexer end |
#lexer_class ⇒ Object
280 281 282 283 284 285 286 |
# File 'lib/rouge/cli.rb', line 280 def lexer_class @lexer_class ||= Lexer.guess( :filename => @input_file, :mimetype => @mimetype, :source => input_stream, ) end |
#raw_lexer ⇒ Object
288 289 290 |
# File 'lib/rouge/cli.rb', line 288 def raw_lexer lexer_class.new(@lexer_opts) end |
#run ⇒ Object
345 346 347 348 |
# File 'lib/rouge/cli.rb', line 345 def run Formatter.enable_escape! if @escape formatter.format(lexer.lex(input), &method(:print)) end |