Class: Rouge::CLI::Highlight
- Inherits:
-
Rouge::CLI
- Object
- Rouge::CLI
- Rouge::CLI::Highlight
- Defined in:
- lib/rouge/cli.rb
Instance Attribute Summary collapse
-
#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
Instance Method Summary collapse
-
#initialize(opts = {}) ⇒ Highlight
constructor
A new instance of Highlight.
- #input ⇒ Object
- #input_stream ⇒ Object
- #lexer ⇒ Object
- #lexer_class ⇒ Object
- #run ⇒ Object
Methods inherited from Rouge::CLI
Constructor Details
#initialize(opts = {}) ⇒ Highlight
Returns a new instance of Highlight.
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/rouge/cli.rb', line 226 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] formatter_class = Formatter.find(opts[:formatter]) \ or error! "unknown formatter #{opts[:formatter]}" @formatter = formatter_class.new(opts[:formatter_opts]) end |
Instance Attribute Details
#formatter ⇒ Object (readonly)
Returns the value of attribute formatter.
224 225 226 |
# File 'lib/rouge/cli.rb', line 224 def formatter @formatter end |
#input_file ⇒ Object (readonly)
Returns the value of attribute input_file.
224 225 226 |
# File 'lib/rouge/cli.rb', line 224 def input_file @input_file end |
#lexer_name ⇒ Object (readonly)
Returns the value of attribute lexer_name.
224 225 226 |
# File 'lib/rouge/cli.rb', line 224 def lexer_name @lexer_name end |
#mimetype ⇒ Object (readonly)
Returns the value of attribute mimetype.
224 225 226 |
# File 'lib/rouge/cli.rb', line 224 def mimetype @mimetype end |
Class Method Details
.desc ⇒ Object
145 146 147 |
# File 'lib/rouge/cli.rb', line 145 def self.desc "highlight code" end |
.doc {|%[usage: rougify highlight <filename> [options...]]| ... } ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rouge/cli.rb', line 149 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 %[--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
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 |
# File 'lib/rouge/cli.rb', line 171 def self.parse(argv) opts = { :formatter => 'terminal256', :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', '-f' opts[:formatter] = argv.shift when '--lexer-opts', '-L' opts[:lexer_opts] = parse_cgi(argv.shift) when '--formatter-opts', '-F' opts[:formatter_opts] = parse_cgi(argv.shift) when /^--/ error! "unknown option #{arg.inspect}" else opts[:input_file] = arg end end new(opts) end |
Instance Method Details
#input ⇒ Object
208 209 210 |
# File 'lib/rouge/cli.rb', line 208 def input @input ||= input_stream.read end |
#input_stream ⇒ Object
204 205 206 |
# File 'lib/rouge/cli.rb', line 204 def input_stream @input_stream ||= FileReader.new(@input_file) end |
#lexer ⇒ Object
220 221 222 |
# File 'lib/rouge/cli.rb', line 220 def lexer @lexer ||= lexer_class.new(@lexer_opts) end |
#lexer_class ⇒ Object
212 213 214 215 216 217 218 |
# File 'lib/rouge/cli.rb', line 212 def lexer_class @lexer_class ||= Lexer.guess( :filename => @input_file, :mimetype => @mimetype, :source => input_stream, ) end |
#run ⇒ Object
245 246 247 248 249 |
# File 'lib/rouge/cli.rb', line 245 def run formatter.format(lexer.lex(input)) do |chunk| print chunk end end |