rubychan / coderay

Fast and easy syntax highlighting for selected languages, written in Ruby.
http://coderay.rubychan.de/
Other
846 stars 115 forks source link

Could the API for CodeRay.scan(dataset, :python).term also allow for some variant that works on the file extension? (e. g. "rb" for ruby files, "py" or python files etc..) #218

Open ghost opened 7 years ago

ghost commented 7 years ago

Hello korny,

Sorry for so many reports :D - I am using coderay quite a lot.

I have a "cat" wrapper in ruby, that displays file content. I want to be able to just pass the file extension name into coderay and have it colourize the colourized content (before printing the result on the commandline). This is not, strictly speaking, what cat is doing, but I found for my use cases it's simpler if I just "cat foobar.py" and have coderay put out python-colourized content on the commandline. :D

The current API that I use is this one here:

dataset = CodeRay.scan(dataset, :python).term

In this context, the dataset variable was simply the content of the local file at hand, usualy obtained via:

dataset = File.read(path_here)

Or something like that.

Now I was trying to see if this API can be simplified. For example, I would like to avoid having to use symbols such as :python or :ruby, if that is possible. So I was thinking "coderay perhaps supports an API that works with the extension name".

So in this context, .py and .rb and so forth. Most of my files are standard and use such extension names.

Unfortunately, coderay does not support this.

I tried via '.py' which should indicate the file extension but coderay issued an error:

/Programs/Ruby/2.4.2/lib/ruby/gems/2.4.0/gems/coderay-1.1.2/lib/coderay/helpers/plugin_host.rb:212:in `validate_id': Invalid id given: .py (ArgumentError)

I also tried with 'py' instead, which may be the one that one gets via File.extname(path).delete('.') for instance but this also does not work.

Now finally I come to my suggestion:

Another API may be fine, but I think it may be better if the currently existing variant is extended. It does not have to be a String to the current API of the method, by the way, it could also be a hash. Then I could pass a hash with a key such as:

variable_holding_the_extension_name = File.extname(input_path)
extension_name: variable_holding_the_extension_name

Or something similar to the above.

If an unsupported extension is passed, an error could be raised, with a message such as:

"unsupported extension name: for the currently supported programming languages names, see: coderay --supported"

or something appropriate.

I am not sure if this would be useful or not or whether you would want coderay to support this, since it may be only me and nobody else who wants this. Please feel free to close this at any moment in time - coderay is great, I use it for commandline colourizing all the time. :)