From d63f1f92a2f859b631d1d26ef87840fb56c60f33 Mon Sep 17 00:00:00 2001 From: jcon Date: Thu, 22 Jan 2009 14:38:04 -0500 Subject: [PATCH] Added line number capabilities to highlight blocks --- README.textile | 17 ++++++++++++++++- lib/jekyll/tags/highlight.rb | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.textile b/README.textile index 4fa4c406..b192b006 100644 --- a/README.textile +++ b/README.textile @@ -331,11 +331,26 @@ The argument to highlight is the language identifier. To find the appropriate identifier to use for your favorite language, look for the "short name" on the "Lexers":http://pygments.org/docs/lexers/ page. +There is a second argument to highlight called +linenos that is optional. Including the linenos +argument will force the highlighted code to include line numbers. For instance, +the following code block would include line numbers next to each line: + +
+{% highlight ruby linenos %}
+def foo
+  puts 'foo'
+end
+{% endhighlight %}
+
+ In order for the highlighting to show up, you'll need to include a highlighting stylesheet. For an example stylesheet you can look at "syntax.css":http://github.com/mojombo/tpw/tree/master/css/syntax.css. These are the same styles as used by GitHub and you are free to use them for your -own site. +own site. If you use linenos, you might want to include an additional CSS class +definition for lineno in syntax.css to distinguish the line numbers +from the highlighted code. h2. Categories diff --git a/lib/jekyll/tags/highlight.rb b/lib/jekyll/tags/highlight.rb index cd4121ae..597ceaed 100644 --- a/lib/jekyll/tags/highlight.rb +++ b/lib/jekyll/tags/highlight.rb @@ -2,10 +2,22 @@ module Jekyll class HighlightBlock < Liquid::Block include Liquid::StandardFilters + # we need a language, but the linenos argument is optional. + Syntax = /(\w+)\s?(:?linenos)?\s?/ - def initialize(tag_name, lang, tokens) + def initialize(tag_name, markup, tokens) super - @lang = lang.strip + if markup =~ Syntax + @lang = $1 + if defined? $2 + # additional options to pass to Albino. + @options = { 'O' => 'linenos=inline' } + else + @options = {} + end + else + raise SyntaxError.new("Syntax Error in 'highlight' - Valid syntax: highlight [lineno]") + end end def render(context) @@ -17,7 +29,7 @@ module Jekyll end def render_pygments(context, code) - "" + Albino.new(code, @lang).to_s + "" + "" + Albino.new(code, @lang).to_s(@options) + "" end def render_codehighlighter(context, code)