Glyph - Source Code

If you’re a programmer, chances are that you’re going to include some source code in your articles and books. Glyph offers two ways to format code blocks effortlessly: the codeblock macro, which simply wraps text into <pre> and <code> tags, or the highlight macro. The last one requires either Coderay or Ultraviolet, but it provides syntax highlighting for the most common programming languages.

Cosider the following piece of ruby code:

1def find_child(&block)
2  children.each do |c|
3    c.descend do |node, level|
4      return node if block.call(node)
5    end
6  end
7  nil
8end

It can be wrapped in a highlight macro, like so:

 1highlight[=ruby|
 2  def find_child(&block)
 3    children.each do |c|
 4      c.descend do |node, level|
 5        return node if block.call(node)
 6      end
 7    end
 8    nil
 9  end
10=]

...to produce the following, using the coderay highlighter:

1def find_child(&block)
2  children.each do |c|
3    c.descend do |node, level|
4      return node if block.call(node)
5    end
6  end
7  nil
8end