Rails Syntax highlighter albino is not working - ruby-on-rails

I am using syntax highlighter "albino" i my rails project ,but it is 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">"Hello World!"</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.

Related

Custom Sublime Snippets - HTML Style Formatting

Forgive the vague title, I'm having a hard time figuring out the correct phrase for what I am trying to do.
I have a number of custom snippets I have written to support writing ERB for Ruby on Rails. For this example, I am trying to use an if tag that can be on a single line:
<% if something %>Content Here<% end %>
or multiple lines with indented content:
<% if something %>
Content Here
<% end %>
I am able to get the first (single line) format working fine with the following snippet:
<![CDATA[<% if $1 %>${2:$SELECTION}<% end %>$0]]>
The problem is that if I type in the trigger, hit tab, type in the conditional for $1, hit tab, then hit return, Sublime does exactly what I type and puts the following:
<% if something %>
[cursor]<% end %>
Is there a way in the snippet to instead treat the if and end tags like HTML tags:
<% if something %>
[cursor]
<% end %>
Thanks!
The easiest way is to define a new snippet with a new tabTrigger. It will look something like:
<snippet>
<content><![CDATA[<% if $1 %>
${2:$SELECTION}
<% end %>$0]]>
</content>

Ruby On Rails image_path

link_to(image_tag("icons/#{icon_name}.png"),url_or_object,options) I was trying use it like that but when I enter the project I'm seeing it like this http://prntscr.com/329yzz I can't see the image please help me,I am beginner in Ruby too
Please Help me
You are getting raw html output.
Use html_safe as shown below:
EDIT
As per OP's comments, html_safe was required in project_title_links method to convert the link returned from link_to_icon into an HTML safe string(DOM ready):
def link_to_icon(icon_name, url_or_object,options={})
link_to(image_tag("icons/#{icon_name}.png", ),url_or_object,options)
end
def project_title_links(project)
content_tag :h1 do [project.title, link_to_icon('show',project) ].join(' ').html_safe end
end
link_to takes an optional block, if you need anything more complicated than text:
link_to url_or_object, options do
image_tag("icons/#{icon_name}.png")
end
Since those method is being used within the views, link_to is using capture to evaluate the block. Hence it can be used like:
<%= link_to url_or_object, options do %>
<div class='wrapper'>
<label>Some text</label>
<%= image_tag("icons/#{icon_name}.png") %>
</div>
<% end %>

Include ERB delimiters inside of a string in an ERB block

I am working on a style guide which displays the code, as well as the output. It is currently structured so that the code only needs to be described once, and is displayed in both its raw and interpreted versions, like so:
<% code = <<PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR
<div>
#{ image_tag 'image.png' }
</div>
PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR
%>
<%= raw code %>
<%= content_tag :pre, code, class: "prettyprint linenums" %>
This is great, and fairly easy to maintain. The problem comes in with the rails helpers, like image_tag in the above example. The view example correctly displays an image in a div, and the code example displays the relevant HTML. In this case, the relevant HTML includes an anchor tag - the results of the image_tag method, not the call itself.
I would prefer the code examples to display the helper methods, rather that their results. I am able to make this work by specifying the example code in a file, and either rendering or reading the file. I would prefer to make this work by specifying the code in a variable, as above, but I can't seem to get an ERB delimiter to work inside of a string inside of an erb block. Even the simplest case of <% foo = '<%= bar %>' %> doesn't work at all. I've tried playing with the syntax (<%% %%> and % % for example), using details from the official documentation, without much success.
The only information I could find on the matter is here, using <%= "<" + "%=" %> link_to <%= image.css_tag.humanize %> <%= "%" + ">" %> %>, which does not work in this use case (if at all).
So, is there a way to specify a string that contains a ERB end-delimiter (%>) in an ERB string, or am I stuck using the slightly clunkier file-read method? Thanks!
Edit:
What I would like to end up with is a working version of this:
<%# Idealized code - does not work %>
<% code = <<PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR
<div>
<% image_tag 'image.png' %>
</div>
PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR
%>
So that <%= raw code %> would (continue to) output:
<div>
<img src="/images/image.png" alt="Image" />
</div>
And <%= content_tag :pre, code, class: "prettyprint linenums" %> would output:
<pre class="prettyprint linenums">
<div>
<% image_tag 'image.png' %>
</div>
</pre>
Instead of what it currently does when using a variable, which is:
<pre class="prettyprint linenums">
<div>
<img src="/images/image.png" alt="Image" />
</div>
</pre>
I want users to be able to copy the code example and paste it into a new view, without having to translate HTML back into the helpers that produce them. I think what I basically need is an alternative ERB delimiter, in the same way that ' and " (or even %q{}) vary for strings. It seems that even though the final ERB delimiter is occurring inside of a string, it is being actually processed as the end of the block. The simplest case of <% foo = '<%= bar %>' %> demonstrates somewhat what I want to accomplish. In a generator, you might use <% foo = '<%%= bar %>' %> (or something similar), to tell it not to process as ERB right then and there. This all works fine when reading from a file, or even in a pure rb file (like a helper), but it makes the most sense to put it in the view, in this case, as it is intended to be easily manipulated by our designers.
If I'm understanding you right, your real problem is that heredocs behave like double quotes as far as interpolation is concerned. So all you need is a quoting mechanism that behaves like single quotes. Ruby has lots of string quoting mechanisms, in particular we have %q{...}:
<% code = %q{
<div>
#{ image_tag 'image.png' }
</div>
} %>
You can use other delimiters if you'd like: %q|...|, %q(...), etc. There's still a change of course but at least you don't have to worry about interpolation problems.
If you really want to use a heredoc, you can specify the heredoc terminator with quotes and the corresponding quoting style will apply to the content:
<% code = <<'PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR'
<div>
#{ image_tag 'image.png' }
</div>
PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR
%>
The single quotes in <<'PLACE...' specify that single quoting rules (i.e. no interpolation) apply to the heredoc's content.
Of course none of that stuff will work with embedded ERB like this:
<% code = %q{
<div>
<% ... %>
</div>
} %>
because the ERB parser will see the first %> as the closing delimiter for the outer <% code... part. Fear not, I think I have a plan that will work without involving gross hacks or too much work.
Some preliminaries:
Rails uses Erubis for ERB processing.
Erubis allows you to change the delimiters with the :pattern option to its constructor.
Rails uses Tilt and Sprockets to handle the template processing pipeline, these allow you to make the right things happen to pancakes.js.coffee.erb in the right order.
Using the above you can add your own template format that is ERB with a different delimiter and you can have Rails use this new format to handle your "special" sections before the normal ERB processing can make a mess of things.
First you need to hook up Tilt. If you have a look at lib/tilt/erb.rb in your Tilt installation, you'll see the Erubis stuff in Tilt::ErubisTemplate at the bottom. You should be able to subclass Tilt::ErubisTemplate and provide a prepare override that adds, say, a :pattern => '<!--% %-->' option and punts to the superclass. Then register this with Tilt and Sprockets in a Rails initializer with something like this:
Tilt.register(Your::Template::Subclass, 'klerb') # "kl" for "kludge" :)
Rails.application.assets.register_engine('.klerb', Your::Template::Subclass)
Now your application should be able to handle .klerb files with <!--% ... %--> as the template delimiters. And you can also chain your klerb with erb using names like pancakes.html.erb.klerb and the file will go through klerb before the ERB; this means that templates like this (in a file called whatever.html.erb.klerb):
<!--% code = <<PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR
<div>
<% image_tag 'image.png' %>
</div>
PLACE_THE_EXAMPLE_CODE_BETWEEN_THESE_TWO_LINES_EXACTLY_AS_YOU_WANT_IT_TO_APPEAR
%-->
<!--%= "code = escape_the_erb_as_needed(%q{#{code}})" %-->
<% do_normal_erb_stuff %>
will do The Right Thing.
You'd need a helper to implement the escape_the_erb_as_needed functionality of course; a little experimentation should help you sort out what needs to be escape and in what way.
All that might look a bit complicated but it is really pretty straight forward. I've added custom template processing steps using Tilt and Sprockets and it turned out to be pretty simple in the end; figuring out which simple things to do took some work but I've already done that work for you:
Tilt::Template subclass, you get this by piggy backing on Tilt::ErubisTemplate.
Register with Tilt by calling Tilt.register.
Register with Sprockets by calling Rails.application.assets.register_engine.
...
Profit.

Rails HAML helper to display a list item

I am having difficulty getting my helper to display a list item. The markup looks like the following:
- #bars.each do |bar|
<% display_bar(bar) %>
The actual helper looks like the following:
module MyHelper
def display_bar(bar)
type = bar.type
concat(%li.type)
concat(%b some text)
concat(%i some more text)
end
end
What am I doing wrong here?
Such things has to be implemented via partials. Or see 5.
<% won't show you anyting. You're in Haml. It's ERb stuff (but even there it wouldn't have shown anything: you'd forgotten the = sign, it should have been <%=).
About concat(%li.type): you cant put your markup inside your Ruby code. Ruby knows nothing about your %li "code".
Amokrane Chentir already mentioned.
You're trying to reinvent the wheel. Rails already provides magnificent helper for such simple cases.
Take a look:
= content_tag_for(:li, #bars) do |bar|
%b= bar.title
%i= bar.id
UPD: content_tag_for sets styles/ids for each li tag based on the current model instance that makes it easy to implement styling/scripting in the future.
The name of your helper is display_bar not display_event.
You should use = instead of <% %>
- #bars.each do |bar|
= display_event(bar)
EDIT
Oops didn't read carefully the content of display_bar method, as #jdoe mentioned you can't use Haml markup syntax in your Ruby code.

How to comment code in Rails views?

As I'm playing with Rails and developing views I often want to comment out code. Simple enough with classes & models but views are a bit more tricky.
What's best way to comment code in a view so it's not interpreted by, well, anything... HTML gives us <!-- commented Rails code here --> though code enclosed here seems to get interpreted anyway?!? Or is there a more Railsy way?
<% code code # comment %> USED to work but I think that was accidental.
You were always supposed to put comments in separate comment tags <%# comment %>
Note NO SPACE before the pound.
Now the old loophole is closed (I forget whether 'now' means Ruby 1.8 or Rails 3 or what) so that:
<% code code # this runs too %>
<% # also runs %>
<%# the only way to comment out %>
I use this all the time
<%# This is a comment %>
The reason Ruby code would be executed inside <!-- --> HTML comments is because all of the server side code (ie. Ruby) is interpreted first, and then the output is sent to the client, at which point the browser interprets <!-- --> as a comment. As the other answers said, use <% #comment %> to comment within a Rails view.
Although (and i'm hoping to be corrected here) you have to be careful because i've had some really strange behavior when doing something like this:
<% if (my_boolean) # Commenting on this if-block %>
where it will affect the HTML that directly follows that (even if it's on another line).
Anyone?
And would this qualify as an answer, or a comment?
<% #comment here %>
:D

Resources