Ruby-on-Rails: Mixing Sanitize and Truncate can be a dirty thing - ruby-on-rails

So stand alone I get what I need. But I want to truncate it, my dynamic text comes out with dirty text globbered with Microsoft Word garbage.
An Example :
≪! [If Gte Mso 9]>≪Xml> ≪Br /> ≪O:Office Document Settings> ≪Br /> ≪O:Allow Png/> ≪Br /> ≪/O:Off...
So how do I get the best of both worlds? Is there a shorthand ruby way to do this? For example a gsub statement that would clip off everything after the 125th char?

if you just want to slice, you can
>> long_ugly_string = "omg this is a long string"
=> "omg this is a long string"
>> long_ugly_string[10..-1]
=> "s a long string"
Reference: http://ruby-doc.org/core/classes/String.html#M000771
so, you are just specifying the starting character (10) and the ending character (-1 tells it to go to the end of the string).

Related

Single spacing in Jupyter math mode

I'm trying to write out a series of math equations inline in Jupyter. I've written the following in my markdown:
Solution:
$x_1 = -8$
$x_2 = 3$
The problem is that this displays much like it does in StackOverflow, with a space in between. Is there a way to display those as a single-spaced entity, rather than double-spaced? I'd rather them be left-aligned, so I don't want to double up the $$.
You can use a <br /> tag to insert a line break between the two equations (less space between lines):
Solution:
$x_1 = -8$ <br />
$x_2 = 3$

parsley.js telephone digits input validating with spaces

I have an input for telephone number.
I would like to write this format: 0175 6565 6262 (with spaces). But if write with " " spaces so get error and I write without spaces so get not error.
Here my HTML Input:
<input type="text" data-parsley-minlength="6" data-parsley-minlength-message="minlength six number" data-parsley-type="digits" data-parsley-type-message="only numbers" class="input_text" value="">
Hope someone can help me?
That's a great answer, but it's a bit too narrow for my needs. Input field should be tolerant of all potential inputs – periods, hyphens, parentheses, spaces in unexpected places, plus signs for international folk – and using this document from Microsoft detailing what numbers IE11 should accept, I've come up with this:
data-parsley-pattern="^[\d\+\-\.\(\)\/\s]*$"
Every number in that list passes the test with flying colours. Enjoy!
If you want your input to accept a string like "nnnn nnnn nnnn" you should use a regular expression.
For example, you can use the following HTML:
<input type="text" name="phone" value="" data-parsley-pattern="^\d{4} \d{4} \d{4}$" />
With this pattern the input will only be valid when you have fourdigits«space»fourdigits«space»fourdigits
You can test or tweak the regular expression and test it here: http://regexpal.com/
If you will use this pattern multiple times in your project I suggest you create a custom validator (see http://parsleyjs.org/doc/index.html#psly-validators-craft)

Primefaces inputTextArea maxlength not working

I am using JSF 2.0 with Primefaces 3.2. I have a text area with a maxlength set to 4000.
But the text area allows me to type 4001 chars. (Always one extra character).
I have been setting my maxlength to 3999 to avoid this problem
Is this a known issue? I dont see this problem on showcase, any ideas?
<p:inputTextarea id="text" value="#{controller.text}" maxlength="4000"
rows="6" cols="150" autoResize="true" required="true" requiredMessage="Text is required" rendered="#{controller.condition}"/>
Sometimes enter counts as 2 characters: \r\n instead of just \n.
In such cases try to replace all of these characters to \n in your setter method. You can do that for example by using StringUtils.replace() method:
your_string = StringUtils.replace(your_string, "\r\n", "\n");
Using UTF-8 characters that take up more than one byte to store still counts as one character, but you can run into trouble with this in your database (getting an error message that it's more than 4000 characters).
You say it's always +1 character, check out using only ASCII characters (numbers+english letters).
ps: (if you are using nobleCount to display the remaing characters, it also has some issues with UTF-8 characters/some special characters).

How to escape whitepace in Rails?

I know that rails automatically escapes characters like '<' or '&', but this does nothing for multiple spaces next to each other. I would like to escape everything, including spaces.
I understand that normally you don't want to use and that you should use css instead. However, I'm trying to take user input and display it, so css isn't feasible.
For example, I have the user input: test . When I display it with <%=#user_input%> in the view, the extra whitespace is displayed as a single space (though it appears correctly in the source).
Is there an easy way to escape the whitespace? Should I just use h #user_input and then replace all the spaces?
The whitespace isn't removed. Browsers simply interpret multiple whitespace characters as a single space.
You could convert each space to if you want:
<%= raw #user_input.gsub(/\s/, " ") %>
You could alternatively replace each space with an empty <span class="whitespace"></span> tag, and then use CSS to style the whitespace 'characters' however you like.
Finally, you can do this with only CSS too using the white-space: pre style (example below).
http://jsfiddle.net/G3VnY/
Edit (to answer the follow-up in your comment)
<%= raw h("this is a sample & with ampersand.").gsub(/\s/, " ") %>
This escapes the & as & in the source (and will do similar for other HTML entities), and then does the " " to conversion.

Interpret newlines as <br>s in markdown (Github Markdown-style) in Ruby

I'm using markdown for comments on my site and I want users to be able to create line breaks by pressing enter instead of space space enter (see this meta question for more details on this idea)
How can I do this in Ruby? You'd think Github Flavored Markdown would be exactly what I need, but (surprisingly), it's quite buggy.
Here's their implementation:
# in very clear cases, let newlines become <br /> tags
text.gsub!(/^[\w\<][^\n]*\n+/) do |x|
x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
end
This logic requires that the line start with a \w for a linebreak at the end to create a <br>. The reason for this requirement is that you don't to mess with lists: (But see the edit below; I'm not even sure this makes sense)
* we don't want a <br>
* between these two list items
However, the logic breaks in these cases:
[some](http://google.com)
[links](http://google.com)
*this line is in italics*
another line
> the start of a blockquote!
another line
I.e., in all of these cases there should be a <br> at the end of the first line, and yet GFM doesn't add one
Oddly, this works correctly in the javascript version of GFM.
Does anyone have a working implementation of "new lines to <br>s" in Ruby?
Edit: It gets even more confusing!
If you check out Github's official Github Flavored Markdown repository, you'll find yet another newline to <br> regex!:
# in very clear cases, let newlines become <br /> tags
text.gsub!(/(\A|^$\n)(^\w[^\n]*\n)(^\w[^\n]*$)+/m) do |x|
x.gsub(/^(.+)$/, "\\1 ")
end
I have no clue what this regex means, but it doesn't do any better on the above test cases.
Also, it doesn't look like the "don't mess with lists" justification for requiring that lines start with word characters is valid to begin with. I.e., standard markdown list semantics don't change regardless of whether you add 2 trailing spaces. Here:
item 1
item 2
item 3
In the source of this question there are 2 trailing spaces after "item 1", and yet if you look at the HTML, there is no superfluous <br>
This leads me to think the best regex for converting newlines to <br>s is just:
text.gsub!(/^[^\n]+\n+/) do |x|
x =~ /\n{2}/ ? x : (x.strip!; x << " \n")
end
Thoughts?
I'm not sure if this will help, but I just use simple_format()
from ActionView::Helpers::TextHelper
ActionView simple_format
my_text = "Here is some basic text...\n...with a line break."
simple_format(my_text)
output => "<p>Here is some basic text...\n<br />...with a line break.</p>"
Even if it doesn't meet your specs, looking at the simple_format() source code .gsub! methods might help you out writing your own version of required markdown.
A little too late, but perhaps useful for other people. I've gotten it to work (but not thoroughly tested) by preprocessing the text using regular expressions, like so. It's hideous as a result of the lack of zero-width lookbehinds, but oh well.
# Append two spaces to a simple line, if it ends in newline, to render the
# markdown properly. Note: do not do this for lists, instead insert two newlines. Also, leave double newlines
# alone.
text.gsub! /^ ([\*\+\-]\s+|\d+\s+)? (.+?) (\ \ )? \r?\n (\r?\n|[\*\+\-]\s+|\d+\s+)? /xi do
full, pre, line, spaces, post = $~.to_a
if post != "\n" && pre.blank? && post.blank? && spaces.blank?
"#{pre}#{line} \n#{post}"
elsif pre.present? || post.present?
"#{pre}#{line}\n\n#{post}"
else
full
end
end

Resources