How to insert special HTML-symbols with HAML - ruby-on-rails-3

When I'm saying:
%p= item.price + " dollars"
I'm getting
50&nbsp ;dollars
instead of having non-breakable space symbol.
How to insert this and another special symbols using HAML ?

How about
%p= item.price + " dollars".html_safe

Use != instead of =
See "Unescaping HTML" in the haml reference: http://haml.info/docs/yardoc/file.REFERENCE.html#unescaping_html

The interpolation option:
%p= "#{item.price} dollars".html_safe

I tried using html_safe in different ways, but none worked. Using \xa0 as suggested by ngn didn't work, either, but it got me to try the Unicode escape of the non-breaking space, which did work:
"FOO\u00a0BAR"
and .html_safe isn't even needed (unless something else in the string needs that, of course).
The Ruby Programming Language, first edition, says: "In Ruby 1.9, double-quoted strings can include arbitrary Unicode escape characters with \u escapes. In its simplest form, \u is followed by exactly four hexadecimal digits ..."

This answer is for a slightly different question but I found this question searching for it...
If you have a submit tag %input{ :type => "submit", :value => " dollars", :name => "very_contrived" } even if you throw an html_safe on the :value it will not evaluate the html.
The solution is to use the rails helper... duh
= submit_tag " dollars".html_safe
this is pretty obvious but it tripped me up. Legacy code + rails upgrade = this kind of stuff :P

You could use \xa0 in the string instead of . 0xa0 is the ASCII code of the non-breaking space.

I prefer using the character itself with the escaped HTML method with most symbols other than the whitespace characters. That way i don't have to remember all the html codes. As for the whitespace characters i prefer to use CSS, this is a much cleaner way.
%p&= "#{item.price} $%&#*#"

Related

How to write an XPath to select text with a quote & comma character?

How can I select a XPath that contains a text with both quote & comma character?
I have to find an element if the text contains =Yes, It's OK
My XPath does not save in ranorex tool even though if i put the text inside the double quotes like below
//span[text()="Yes, It's OK"]
So how can I save this xpath that uses "".in Ranorex
your use of double-quotes is correct. if
//span[text()="Yes, It's OK"]
doesn't match, it might be because the xpath engine has a lowercase bug (i have encountered PHP+DOMXPath+libxml2 systems where it would only match lowercase text even for text with uppercase characters, never quite figured out the problem there), or it might be because the text has hidden whitespace you're not aware of, maybe it actually has a hard-to-spot whitespace at the start or the end? anyway maybe contains() will work:
//span[contains(text(),"Yes, It's OK"]
or.. if it has the lowercase-issue i have encountered in the wild once before, maybe this will work:
//span[contains(text(),"yes, it's ok"]
(that really shouldn't be required, but i have been in that situation once before, and that was years ago, and that was... probably php5)

Spaces between elements in HAML

How would you represent this in HAML?:
<a>Link</a> | <a>Link</a>
Note that I want to retain the spaces on either side of the bar.
I would write exactly what you've written, which is perfectly valid HAML. You may embed regular HTML into HAML:
%h1
<a>Link</a> | <a>Link</a>
Sometimes whitespace bites you when you're marking things up with HAML, and there is no pretty way of making your tags come out correctly. That is why HAML gives you the option of falling back to HTML.
Note that, if you're ok with one or more spaces between your links and the |, you can just write regular old HAML:
%h1
%a link
|
%a link
The new lines will be preserved, and render as a space in the browser, where any amount of any kind of whitespace will always be treated like a single space.
Put '|' on next line, new line will be preserved, and render as a white space.
%a link
|
%a link
meagar's answer is how I would do it, you could also use haml filters to write exactly the HTML you need.
This might sound dirty, but filters use is encouraged, see this article : http://chriseppstein.github.io/blog/2010/02/08/haml-sucks-for-content/

RegexKitLite Not Matching NSString Correctly

Alright, I'm trying to write some code that removes words that contain an apostrophe from an NSString. To do this, I've decided to use regular expressions, and I wrote one, that I tested using this website: http://rubular.com/r/YTV90BcgoQ
Here, the expression is: \S*'+\S
As shown on the website, the words containing an apostrophe are matched. But for some reason, in the application I'm writing, using this code:
sourceString = [sourceString stringByReplacingOccurrencesOfRegex:#"\S*'+\S" withString:#""];
Doesn't return any positive result. By NSLogging the 'sourceString', I notice that words like 'Don't' and 'Doesn't' are still present in the output.
It doesn't seem like my expression is the problem, but maybe RegexKitLite doesn't accept certain types of expressions? If someone knows what's going on here, please enlighten me !
Literal NSStrings use \ as an escape character so that you can put things like newlines \n into them. Regexes also use backslashes as an escape character for character classes like \S. When your literal string gets run through the compiler, the backslashes are treated as escape characters, and don't make it to the regex pattern.
Therefore, you need to escape the backslashes themselves in your literal NSString, in order to end up with backslashes in the string that is used as the pattern: #"\\S*'+\\S".
You should have seen a compiler warning about "Unknown escape sequence" -- don't ignore those warnings!

How to write this in haml?

What is that i should write to convert this to haml
When I tried like this
%p
We prefer questions that can be
%b answered
. not just discussed.
I got Illegal element: classes and ids must have values error.
IS there any way i can get the dot not bolded.
This should work:
%p
We prefer questions that can be
<b>answered</b>. not just discussed.
Edit
As #mark pointed out below the reason you were receiving the error was because if a line starts with a . haml is expecting a class name in order to render a div with that class.
\.
escapes the .
Mark's answer is still going to be the best, for readability.
You could do this in pure HAML by using the > operator on the b tag to remove surrounding spaces. However, you'd need to insert a non-breaking space before the bold word to keep it separate.
%p
We prefer questions that can beĀ 
%b> answered
\. not just discussed.
You need to escape the leading period as it's evaluating it as a div with a class.
%div.my_class
==
.my_class
==
<div class='my_class'></div>
%p
We prefer questions that can be
%b answered
\. not just discussed.

Ruby 1.9 strip not removing whitespace

Im doing some screen scraping and im getting back a string that appears to end with whitespace but neither string.strip or strip.gsub(/\s/u, '') removes the character.
Im guessing it's a character encoding issue. Any suggestions?
I think, there are a lot of "space characters".
You can use something like this:
my_string.gsub("\302\240", ' ').strip
You can try this: my_string.gsub(/\A[[:space:]]+|[[:space:]]+\z/, '')
This should remove all space characters from the beginning and the end of string, including all possible unicode space variations.
Figure out the character code of the last character (str[-1].ord) and explicitly search and destroy it. Rinse/repeat if there exist more unwanted characters after that. After doing this, report back here what the invisible character was. (Perhaps it's only invisible because the font you are using does not have that glyph?)