Syntax Highlighting in nanoc3

133 views
Skip to first unread message

Robin Wilson

unread,
Dec 26, 2009, 7:02:25 AM12/26/09
to nanoc
Hi,

According to the nanoc wiki, Syntax Highlighting is included by
default in nanoc3. However, I can't find anything about this in the
manual, and when I try using the example given on the syntax
highlighting page on the wiki (code which I copied shown below), I get
an error saying undefined method 'code'.

<% code(:ruby) do %>
#!/usr/bin/env ruby

def some_function
x = blah.foo
x.bar "xyzzy
end
<% end %>

How should I go about using this new syntax highlighting component?
I'm very keen to use it, as there will be lots of code examples on my
website.

Cheers,

Robin

Denis Defreyne

unread,
Dec 26, 2009, 7:22:59 AM12/26/09
to na...@googlegroups.com

Hi,

nanoc does not strictly include a syntax highighting helper, but it comes with a “filtering” helper, which allows filtering parts of a page using a given filter (see http://nanoc.stoneship.org/doc/3.0.0/Nanoc3/Helpers/Filtering.html for details). This way, you can run the “coderay” filter like this:

<% filter :coderay, :language => :ruby do %>
def foo
puts "hello"
end
<% end %>

For the nanoc site, though, I use a custom “SyntaxColorize” helper which works around some issues with the filters (for example, it adds <pre><code>…</code></pre> if necessary, strips whitespace so I can indent my code, allows both CodeRay and Ultraviolet, and transforms XHTML into HTML). You can find it the code for this helper at <http://pastie.org/756913>; drop this code somewhere in a file in lib/, call “include Nanoc3::Helpers::SyntaxColorize” to enable the helper and you’ll be able to use it. You can use it like this:

<% syntax_colorize :ruby do %>
def foo
puts "hello"
end
<% end %>

You can also find a simplified version at <http://pastie.org/756911>.

Including this syntax colorization helper would likely be a welcome enhancement, and I’ll definitely consider adding this filter to the next nanoc release, v3.1.

Hope this helps,

Denis

--
Denis Defreyne
denis.d...@stoneship.org

Robin Wilson

unread,
Dec 26, 2009, 7:57:59 AM12/26/09
to nanoc
Thanks for the very prompt reply. I can't quite get this to work, but
I'm sure I'm doing something really silly. Basically, it's giving me
an error saying "NameError: uninitialised constant
Nanoc3::Helpers::SyntaxColorize". I think that means that it can't
find the module called SyntaxColorize, but I've definitely put the
file you gave me in the /lib directory (see below)...it just doesn't
seem to be loading it.

I've put the file you put on pastie into a file called syntax.rb in
the /lib directory, and the helpers.rb file in the lib directory is
shown below:

include Nanoc3::Helpers::SyntaxColorize
include Nanoc3::Helpers::LinkTo

I'm sure I'm doing something silly again....

Robin

On Dec 26, 12:22 pm, Denis Defreyne <denis.defre...@stoneship.org>
wrote:


> On 26 Dec 2009, at 13:02, Robin Wilson wrote:
>
>
>
> > Hi,
>
> > According to the nanoc wiki, Syntax Highlighting is included by
> > default in nanoc3. However, I can't find anything about this in the
> > manual, and when I try using the example given on the syntax
> > highlighting page on the wiki (code which I copied shown below), I get
> > an error saying undefined method 'code'.
>
> > <% code(:ruby) do %>
> >  #!/usr/bin/env ruby
>
> >  def some_function
> >    x = blah.foo
> >    x.bar "xyzzy
> >  end
> > <% end %>
>
> > How should I go about using this new syntax highlighting component?
> > I'm very keen to use it, as there will be lots of code examples on my
> > website.
>
> Hi,
>

> nanoc does not strictly include a syntax highighting helper, but it comes with a “filtering” helper, which allows filtering parts of a page using a given filter (seehttp://nanoc.stoneship.org/doc/3.0.0/Nanoc3/Helpers/Filtering.htmlfor details). This way, you can run the “coderay” filter like this:


>
>         <% filter :coderay, :language => :ruby do %>
>         def foo
>           puts "hello"
>         end
>         <% end %>
>
> For the nanoc site, though, I use a custom “SyntaxColorize” helper which works around some issues with the filters (for example, it adds <pre><code>…</code></pre> if necessary, strips whitespace so I can indent my code, allows both CodeRay and Ultraviolet, and transforms XHTML into HTML). You can find it the code for this helper at <http://pastie.org/756913>; drop this code somewhere in a file in lib/, call “include Nanoc3::Helpers::SyntaxColorize” to enable the helper and you’ll be able to use it. You can use it like this:
>
>         <% syntax_colorize :ruby do %>
>             def foo
>               puts "hello"
>             end
>         <% end %>
>
> You can also find a simplified version at <http://pastie.org/756911>.
>
> Including this syntax colorization helper would likely be a welcome enhancement, and I’ll definitely consider adding this filter to the next nanoc release, v3.1.
>
> Hope this helps,
>
> Denis
>
> --
> Denis Defreyne

> denis.defre...@stoneship.org

Denis Defreyne

unread,
Dec 26, 2009, 8:00:36 AM12/26/09
to na...@googlegroups.com
On 26 Dec 2009, at 13:57, Robin Wilson wrote:

> Thanks for the very prompt reply. I can't quite get this to work, but
> I'm sure I'm doing something really silly. Basically, it's giving me
> an error saying "NameError: uninitialised constant
> Nanoc3::Helpers::SyntaxColorize". I think that means that it can't
> find the module called SyntaxColorize, but I've definitely put the
> file you gave me in the /lib directory (see below)...it just doesn't
> seem to be loading it.
>
> I've put the file you put on pastie into a file called syntax.rb in
> the /lib directory, and the helpers.rb file in the lib directory is
> shown below:
>
> include Nanoc3::Helpers::SyntaxColorize
> include Nanoc3::Helpers::LinkTo
>
> I'm sure I'm doing something silly again....

Hi,

nanoc loads files in lib/* in alphabetical order, so perhaps lib/helpers.rb is loaded before the actual code for Nanoc3::Helpers::SyntaxColorize is loaded. Because of this, I often have lib/helpers_.rb instead of lib/helpers.rb, which makes sure the files in lib/helpers/ are loaded before lib/helpers_.rb.

Hope this helps,

Denis

--
Denis Defreyne
denis.d...@stoneship.org

Robin Wilson

unread,
Dec 26, 2009, 8:16:12 AM12/26/09
to nanoc
Hi again,

I'm sure I'm being a bit of a pain here, but I've got that bit working
(it was the alphabetical order problem), and now I'm not getting any
errors, but I'm not getting any syntax highlighting either!

I've put the following code in my index.html file, and I've got all
the other files listed as above.

<% syntax_colorize :ruby, type=:coderay do %>


def foo
puts "hello"
end
<% end %>

The example you gave me didn't have the type argument, but I've added
that because it looks like the helper code needs that. I've tried
various ways to specify the type (type => :coderay,
type= :coderay, :type => :coderay) but none of them seem to work. Some
give errors, some just don't give me any syntax highlighting.

Sorry about all these problems - once we've got this sorted I'll
document it all on the Wiki.

Cheers,

Robin

On Dec 26, 1:00 pm, Denis Defreyne <denis.defre...@stoneship.org>
wrote:

> denis.defre...@stoneship.org

Denis Defreyne

unread,
Dec 26, 2009, 8:26:14 AM12/26/09
to na...@googlegroups.com
On 26 Dec 2009, at 14:16, Robin Wilson wrote:

> Hi again,
>
> I'm sure I'm being a bit of a pain here, but I've got that bit working
> (it was the alphabetical order problem), and now I'm not getting any
> errors, but I'm not getting any syntax highlighting either!
>
> I've put the following code in my index.html file, and I've got all
> the other files listed as above.
>
> <% syntax_colorize :ruby, type=:coderay do %>
> def foo
> puts "hello"
> end
> <% end %>
>
> The example you gave me didn't have the type argument, but I've added
> that because it looks like the helper code needs that. I've tried
> various ways to specify the type (type => :coderay,
> type= :coderay, :type => :coderay) but none of them seem to work. Some
> give errors, some just don't give me any syntax highlighting.

Hi,

You can leave the type argument out if you want; it defaults to :coderay. If you want to specify the type (either :coderay or :ultraviolet), you can do so this way:

<% syntax_colorize :ruby, :coderay do %>


def foo
puts "hello"
end
<% end %>

Can you show me the errors that you’re getting? If you don’t get any syntax highlighting, make sure that you have CSS rules for coloring the syntax (e.g. variables, comments, functions, …). Does the outputted HTML have special classes for different parts of the source? If you can’t solve the issue, you can always send me the source for your web site (preferably in private) so I can take a look at what’s going wrong.

Hope this helps,

Denis

--
Denis Defreyne
denis.d...@stoneship.org

Robin Wilson

unread,
Dec 26, 2009, 9:05:53 AM12/26/09
to nanoc
Hiya,

I haven't got any CSS rules to display them in different colours - I
realise I need to add those. However, at the moment, the different
bits of code aren't being enclosed in any special classes to get the
colours. I'll send you a copy of my website for you to test via
private email.

Cheers,

Robin

On Dec 26, 1:26 pm, Denis Defreyne <denis.defre...@stoneship.org>
wrote:

> denis.defre...@stoneship.org

Denis Defreyne

unread,
Dec 26, 2009, 9:41:17 AM12/26/09
to na...@googlegroups.com
On 26 Dec 2009, at 15:05, Robin Wilson wrote:

> Hiya,
>
> I haven't got any CSS rules to display them in different colours - I
> realise I need to add those. However, at the moment, the different
> bits of code aren't being enclosed in any special classes to get the
> colours. I'll send you a copy of my website for you to test via
> private email.

Hi,

Your site compiles fine and produces correct output when using CodeRay (couldn’t try it with ultraviolet because ultraviolet is still incompatible with Ruby 1.9.x). When using ultraviolet, are you getting the warning “WARNING: Couldn't load uv; please install the ultraviolet gem. This message will not appear again.”?

Regards,

Reply all
Reply to author
Forward
0 new messages