Skip to content

Commit 4f81bbf

Browse files
jneenjneen
andauthored
README improvements (#2219)
* add an example of formatting to terminal with a style * add a section about writing your own formatter * document Rouge::Lexer.enable_debug! * link to the plugin example repository --------- Co-authored-by: jneen <jneen@jneen.net>
1 parent 40f7898 commit 4f81bbf

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

README.md

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ use and whether to enable line numbers or not. More information is available in
6767
Rouge ships with a `rougify` command which allows you to easily highlight files
6868
in your terminal:
6969

70-
```bash
71-
rougify foo.rb
72-
rougify style monokai.sublime > syntax.css
70+
```console
71+
$ rougify foo.rb
72+
$ rougify foo.rb -t monokai.sublime
73+
$ rougify style monokai.sublime > syntax.css
7374
```
7475

7576
## Configuration
@@ -138,12 +139,35 @@ The built-in formatters are:
138139
highlighted text for use in the terminal. `theme` must be an instance of
139140
`Rouge::Theme`, or a `Hash` structure with `:theme` entry.
140141

142+
- `Rouge::Formatters::TerminalTruecolor.new(theme)` is similar to the previous,
143+
except it outputs ANSI truecolor codes, instead of approximating with a 256-color
144+
scheme.
145+
146+
- `Rouge::Formatters::Tex.new` is a formatter for TeX systems which wraps each
147+
token with an `\RG{toktype}{text}` tag. You can then use
148+
`rougify style mystyle --tex`
149+
to generate definitions for these tags and the surrounding environment.
150+
141151
#### Writing your own HTML formatter
142152

143-
If the above formatters are not sufficient, and you wish to customize the layout
144-
of the HTML document, we suggest writing your own HTML formatter. This can be
145-
accomplished by subclassing `Rouge::Formatters::HTML` and overriding specific
146-
methods:
153+
For the majority of applications, there are custom requirements for presenting
154+
highlighted text, as HTML or otherwise. In these cases, rather than patching or
155+
post-processing the output of Rouge, it is usually better to **write your own
156+
formatter**.
157+
158+
This may sound intimidating, but it is actually quite easy! All you have to do
159+
is subclass `Rouge::Formatter`, define a `tag`, and implement a method
160+
`#stream(tokens, &block)`, which receives an Enumerable of token/value pairs,
161+
and yields out chunks of strings which will be concatenated.
162+
163+
The `Formatter` base class contains the helper method `#token_lines(stream, &block)`,
164+
which separates tokens into distinct lines, and `Rouge::Formatters::HTML`
165+
contains the helper `#span(token, value)` to escape and render
166+
standard `<span>` tags for HTML.
167+
168+
Alternatively, if you want to override how individual spans are rendered,
169+
you can override `#safe_span(token, safe_value)`, which will be passed the
170+
token type and pre-escaped content for the token.
147171

148172
```ruby
149173
class MyFormatter < Rouge::Formatters::HTML
@@ -189,11 +213,11 @@ end
189213

190214
### Lexer Options
191215

192-
- `debug: false` will print a trace of the lex on stdout.
216+
- `debug: true` will print a trace of the lex on stdout. For safety, this only works if `Rouge::Lexer.enable_debug!` has been called.
193217

194218
- `parent: ''` allows you to specify which language the template is inside.
195219

196-
### CSS Options
220+
### Theme Options
197221

198222
- `scope: '.highlight'` sets the CSS selector to which styles are applied,
199223
e.g.:
@@ -270,6 +294,12 @@ make it clear in your submission that the lexer isn't finished yet. We'll do our
270294
best to answer any questions you have and sometimes the best way to do that is
271295
with actual code.
272296

297+
If your language is internal or obscure, or it is taking far too long to merge
298+
into baseline Rouge, you can very easily write a plugin for language support.
299+
Check out our [plugin example repository][plugin-example] for a good starting point.
300+
301+
[plugin-example]: https://github.com/rouge-ruby/rouge-plugin-example "Rouge Plugin Example"
302+
273303
### Testing Rouge
274304

275305
Once you've cloned the repository from GitHub, you can test the core of Rouge

0 commit comments

Comments
 (0)