开发者

Rails Syntax highlighter albino is not working

I am using syntax highlighter "albino" i my rails project ,but it i开发者_StackOverflows not displaying any thing

below i have written the code

in helper model

def highlight(text)
  Albino.new(text, :ruby)
end 

In the view

<% @codes.each do |code| %>
  <%= highlight(code) %>
<% end %>

so can any one help me where i am going wrong or suggest any good highlighter gem for rails?


Which errors do you get?

You are missing a . after @codes:

<% @codes.each do |code| %>
  <%= highlight(code) %>
<% end %>


It looks to me like your helper is creating a new Albino instance but not using actually asking it to syntax highlight.

Change your helper as follows:

def highlight(text)
  Albino.colorize(text, :ruby)
end


Have you considered using Google Code Prettify? It's the syntax highlighter used on both Google Code and Stack Overflow, which is likely to mean it's reasonably robust.

I don't normally like putting too much functionality in JavaScript, but it seems to me that syntax highlighting is a reasonable feature to add in this way - after all the code will still be readable without the highlighting.


This is a bit of an old problem, but I just came across it myself.

The problem is that Albino is outputting HTML directly as it's being parsed (I think that's the right word, I'm quite new to this).

For example:

highlight(text)

And text is:

def hello_world
  puts "Hello World!".to_s
end

Will result in:

<div class="highlight"><pre><span class="k">def</span> <span class="nf">hello_world</span> <span class="nb">puts</span> <span class="s2">&quot;Hello World!&quot;</span><span class="o">.</span><span class="n">to_s</span> <span class="k">end</span> </pre> </div>

What needs to be done is add .html_safe into your highlight method.

Albino.colorize(text).html_safe

That should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜