Things I Remember to Write Down

Rouge, the pygments-compatible pure-ruby syntax highlighter

EDIT 11 Dec 2012: Rouge now supports many more languages. See them all here.

After a full month of rage coding, I’m happy to announce that Rouge is ready for folks to use.

Rouge is a syntax highlighter written purely in Ruby, with the goals of being the best quality lexing out there, easy to deploy (i.e. on Heroku Cedar, which still doesn’t support pygments.rb), and 100% compatible with pygments stylesheets. It’s also easy to extend, and contributions are welcome! Rouge can always use more themes, lexers, and formatters.

It currently supports the following languages, with many more to come:

Ruby

class (ruby("is"))::Hella
  def crazy!
    no?
  end
end

C++

#include<iostream>

using namespace std;

int main()
{
    cout << "Hello World" << endl;
}

C

#include "ruby/ruby.h"

static int
clone_method_i(st_data_t key, st_data_t value, st_data_t data)
{
    clone_method((VALUE)data, (ID)key, (const rb_method_entry_t *)value);
    return ST_CONTINUE;
}

XML

<?xml version="1.0" encoding="utf-8"?>
<xsl:template match="/"></xsl:template>

Haskell

-- sum
infixr 7 \/
(\/) :: Series a -> Series a -> Series a
s1 \/ s2 = \d -> s1 d ++ s2 d

ERB

(try using the :parent option!)

<title><%= @title %></title>

TeX

To write \LaTeX\ you would type \verb:\LaTeX:.

Scheme

(define (nested-set! root names val)
  (let loop ((cur root)
             (elts names))
    (if (null? (cdr elts))
        (module-set! cur (car elts) val)
        (loop (module-ref cur (car elts)) (cdr elts)))))

Common Lisp

(def-compound-type BIT-VECTOR (&optional (size '*)) (x)
  (ensure-dim BIT-VECTOR size)
  (and (bit-vector-p x)
       (or (eq size '*) (eql size (array-dimension x 0)))
  )
  (c-typep-vector 'BIT-VECTOR-P size x)
)

Java

public class java {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Make

.PHONY: all
all: $(OBJ)

$(OBJ): $(SOURCE)
	@echo "compiling..."
	$(GCC) $(CFLAGS) $< > $@

Perl

#!/usr/bin/env perl
use warnings;
print "a: ";
my $a = "foo";
print $a;

PHP

<?php
  print("Hello {$world}");
?>

Python

def lex(code, lexer):
    """
    Lex ``code`` with ``lexer`` and return an iterable of tokens.
    """
    try:
        return lexer.get_tokens(code)
    except TypeError, err:
        if isinstance(err.args[0], str) and \
           'unbound method get_tokens' in err.args[0]:
            raise TypeError('lex() argument must be a lexer instance, '
                            'not a class')
        raise

Shell

ry::exec() {
  local names="$1"; shift
  if [[ "$names" == "all" ]]; then
    names="$(ry ls)"
  else
    names="$(tr , "\n" <<<"$names")"
  fi

  for name in $names; do
    PATH="$(ry fullpath "$name")" "$@"
  done
}

TCL

set lines [split [read $fh] "\n"]

HTML

<html>
  <head><title>Title!</title></head>
  <body>
    <p id="foo">Hello, World!</p>
    <script type="text/javascript">var a = 1;</script>
    <style type="text/css">#foo { font-weight: bold; }</style>
  </body>
</html>

CSS

body {
    font-size: 12pt;
    background: #fff url(temp.png) top left no-repeat;
}

Javascript

$(document).ready(function() { alert('ready!'); });

JSON

{ "foo": ["bar", { "baz": 1.0 } ] }
blog comments powered by Disqus