Skip to content
Browse files

couple of tweaks to line numbering option

  • Loading branch information...
2 parents 4b2fa43 + d63f1f9 commit 9b0205905420036c8b4a4ed30959b54095a939e5 @mojombo mojombo committed Jan 23, 2009
Showing with 37 additions and 8 deletions.
  1. +1 −0 History.txt
  2. +20 −4 README.textile
  3. +16 −4 lib/jekyll/tags/highlight.rb
View
1 History.txt
@@ -7,6 +7,7 @@
* Add a working Mephisto / MySQL converter [github.com/ivey]
* Allowing .htaccess files to be copied over into the generated site [github.com/briandoll]
* Add option to not put file date in permalink URL [github.com/mreid]
+ * Add line number capabilities to highlight blocks [github.com/jcon]
* Bug Fixes
* Fix permalink behavior [github.com/cavalle]
* Fixed an issue with pygments, markdown, and newlines [github.com/zpinter]
View
24 README.textile
@@ -342,17 +342,33 @@ The argument to <code>highlight</code> 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 <code>highlight</code> called
+<code>linenos</code> that is optional. Including the <code>linenos</code>
+argument will force the highlighted code to include line numbers. For
+instance, the following code block would include line numbers next to each
+line:
+
+<pre>
+{% highlight ruby linenos %}
+def foo
+ puts 'foo'
+end
+{% endhighlight %}
+</pre>
+
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 <code>lineno</code> in syntax.css to distinguish the line
+numbers from the highlighted code.
h2. Categories
-Posts are placed into categories based on the directory structure they are found
-within (see above for an example). The categories can be accessed from within
-a Liquid template as follows:
+Posts are placed into categories based on the directory structure they are
+found within (see above for an example). The categories can be accessed from
+within a Liquid template as follows:
<pre>
{% for post in site.categories.foo %}
View
20 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 <lang> [linenos]")
+ end
end
def render(context)
@@ -18,9 +30,9 @@ def render(context)
def render_pygments(context, code)
if Jekyll.content_type == :markdown
- return "\n" + Albino.new(code, @lang).to_s + "\n"
+ return "\n" + Albino.new(code, @lang).to_s(@options) + "\n"
else
- "<notextile>" + Albino.new(code, @lang).to_s + "</notextile>"
+ "<notextile>" + Albino.new(code, @lang).to_s(@options) + "</notextile>"
end
end

0 comments on commit 9b02059

Please sign in to comment.
Something went wrong with that request. Please try again.