Ruby syntax highlighting
In projects focused on the IT audience, from time to time the task arises of highlighting the syntax of the source files. Recently, I wanted to see how this problem is solved in Ruby.
For Ruby, there is a CodeRay solution that allows server-side syntax highlighting for the following languages:
The highlighting process takes place on the server side, so it does not load the client (if you use web technologies, such as Ruby On Rails), and it is also possible to develop separate utilities (for example, console) to create html files with highlighted code.
Consider a small example of working with CodeRay and write a small console application that will create highlighting for a given Ruby file.
First, install CodeRay with a simple command.
After that, we will write the program itself (it wasn’t me who invented it, so I’ll save it, so to speak, in its original, untouched form)
Let's take a look at this simple program in order: first we plug in the coderay module to have access to the classes of interest to us. After that, we check the number of command line arguments and extract the file name from the first argument. Next is the most important part of our program - the use of the CodeRay.encode function, to which we pass the following arguments:
And now we ask the program created by us to highlight itself. To do this, execute the following command.
After that, we will have the test.html file (which can be opened with any browser) containing the highlighted version of the codecolor.rb program.
Actually, that’s all you need to know when using the Ruby code highlighting generator. For a more detailed study of CodeRay, I advise you to visit the official website
For Ruby, there is a CodeRay solution that allows server-side syntax highlighting for the following languages:
- Ruby
- FROM
- Delphi
- HTML
- RHTML (Rails)
- Nitro-xhtml
- CSS
- Diff
- Java
- Javascript
- Json
- Yaml
The highlighting process takes place on the server side, so it does not load the client (if you use web technologies, such as Ruby On Rails), and it is also possible to develop separate utilities (for example, console) to create html files with highlighted code.
Consider a small example of working with CodeRay and write a small console application that will create highlighting for a given Ruby file.
First, install CodeRay with a simple command.
gem install coderay
After that, we will write the program itself (it wasn’t me who invented it, so I’ll save it, so to speak, in its original, untouched form)
#!/usr/bin/env ruby
# courtesy: Helder
# obvio171.wordpress.com/2007/06/03/colorful-ruby-code-for-your-blog
# modified to output to stdout so can be used as a filter
# 2008-09-03 23:22
require 'rubygems'
require 'coderay'
if ARGV.length != 1
puts "Wrong number of arguments. Use: codecolor.rb "
exit
end
rb_file = File.expand_path(ARGV[0])
print CodeRay.encode(
File.read(rb_file),
:ruby,
:html,
:line_numbers => :inline,
:hint => :info,
:css => :style,
:wrap => :div
)
Let's take a look at this simple program in order: first we plug in the coderay module to have access to the classes of interest to us. After that, we check the number of command line arguments and extract the file name from the first argument. Next is the most important part of our program - the use of the CodeRay.encode function, to which we pass the following arguments:
- highlighted text
- language whose syntax we highlight
- output format
- line number display style
- format of additional hints for highlighted code
- way to connect styles (can be: class or: style)
- way to wrap elements
And now we ask the program created by us to highlight itself. To do this, execute the following command.
ruby codecolor.rb > test.html
After that, we will have the test.html file (which can be opened with any browser) containing the highlighted version of the codecolor.rb program.
Actually, that’s all you need to know when using the Ruby code highlighting generator. For a more detailed study of CodeRay, I advise you to visit the official website