Wiki-like formatting in Rails - ruby-on-rails

I forgotten the name of this library. But it's sort of like Wiki how you type certain characters in front of your text, and then it'll make the text bold/italic/underline.
I'm not asking for the way Wiki is formatted but I'm aware there is something similar built into Rails. It's at the tip of my tongue. Thanks.

Are you looking for the textilize view helper? In your view, just say:
<%= textilize( post.body_text ) %>

Many of these are implemented in Ruby:
Comparison of lightweight markup languages
I like Markdown, through RDiscount.

RedCloth does this. It gives you textile markup (which is among the markup languages listed in Daniel's answer).
http://redcloth.org/

Related

How can I put CSS style in my I18n locale?

I have a following translation:
welcome: Hello there, %{general_kenobi}
Now I know, that I can put HTML tags in the translation, like <em> or so. But what if I want this general_kenobi to have my custom style? How do I implement it? I've tried to google it, but didn't find anythin useful (or maybe I'm just bad at googling). Thank in advance!
If you want to add something fancy like this to your locale you must add the _html extension to the key.
From the docs
Keys with a '_html' suffix and keys named 'html' are marked as HTML
safe. When you use them in views the HTML will not be escaped.
en:
welcome_html: "<b>Bolded text</b>"
then just the regular stuff in your views
<%= t('welcome_html')

How to use slim for mailer text template

I am using slim as the template engine for my rails app and would like to use slim for mailer templates as well.
There is no problem with html mailer templates (views/mailer/default_email.en.html.slim) but, I am not sure how to make the text templates work.
I have placed a text template in views/mailer/default_email.en.text.slim with this content:
Hello,
Your video is ready.
= #url
Thank you,
The A-Team
But the result is parsed as slim HTML, and looks like this:
<Hello>,</Hello><Your>video is ready.</Your><Click>the link below to watch it:</Click>http://watch.thy/video<Thank>you,</Thank><The>A-Team</The>
Other than prefixing every line with a pipe, isnt there a more natural way?
I even looked for an embedded plugin (like the markdown one) to say "plain text" but there is none.
Thanks in advance.
Slim was designed to generate HTML, not plain text, so you'll have to either use the pipe prefix for each line or go with .text.erb templates. I'd use the ERB templates, especially if you don't have a lot of interpolation going on.
For what it's worth, you can actually go the Slim route without having to prefix each line with a pipe, like so:
|
Hello,
<br><br>
Your video is ready.<br>
#{#url}
<br><br>
Thank you,<br>
The A-Team
I would definitely agree with eugen though, that the text.erb route is the best fit. Just providing another solution, in case somebody absolutely insisted on doing this in Slim. :-)

Rails 3.1 CKEditor gem raw text

I'm using the ckeditor gem and when I use the paste as plain text dialog it encapsulates the text in quotes as well as < p> tags and < br> tags. Is there any way I can tell ckeditor not to use any markup whatsoever when using that option.
What I am trying to accomplish is to have snippets of code within an article. Those snippets are processed using the markdown gem as well as pigments.rb. The following code is what I'm trying to accomplish
```ruby
puts "hello world"
class Hello
end
```
and this is what I'm getting
<p>
```ruby<br />
puts "hello world"
class hello<br />
end<br /></p>
This might be what you're looking for:
http://ckeditor.com/addon/codemirror
I really don't know if the ckeditor has that option.
If your problem is show the information like WYSIWYG in a web with RoR maybe you have to set in the view that the information is html safe (This is for security reasons and is set by default). If you don't do that you will see with the html tags.
You have many ways to do the html safe:
Here is a discussion about which one use:
raw vs. html_safe vs. h to unescape html
Hope that solve your problem.
Codemirror might be your best bet for this. Its like CKeditor but for code.
http://codemirror.net/
You could even write your own mode for it, which if I understand what your trying to do might end up being required.
Have not found anything better than going with Markdown. Just like it is done here on SO

Ruby, Textile: how to get plain text without tags and textile format

I have articles with bodies that are textile formatted text. I make full text search with sphinx and I want to highlight query in results. Sphinx give me something like this
A *simple* _<span class="highlight">example</span>_.
I want to remove all textile format but save highlight tags, like this
A simple <span class="highlight">example</span>.
Is it possible with Redcloth gem?
You could process the content with RedCloth and then strip the result of all tags. So something like:
strip_tags (RedCloth.new(simple_input).to_html)
strip_tags is available in the ActionView::Helpers::TextHelper module. http://apidock.com/rails/ActionView/Helpers/TextHelper/strip_tags for more info.
Have yet to work with Sphinx, but judging from your question, it appears that <span class="highlight">..</span> is added by Sphinx upon outputting the text as a search result.
I am currently unaware of any method in RedCloth, but the Vidibus-Textile Gem (based on RedCloth) adds a to_text method, which could be useful for your case.
Ofc you'd need to make sure it's invoked before Sphinx adds the span tags.

Sanitizing input for display in view when using simple_format

I'm trying to figure out the right way to display comments such that newlines and links are displayed. I know that usually, you should display user-inputs only when escaping html with h(). That of course won't display newlines or links, so I found the simple_format and auto_link methods.
What I am now doing is: simple_format(santize(auto_link(comment.text)))
Is this the right way to do this, and is it still safe from XSS attacks?
Thanks!
Eric
Have a look to the last ryanb screencast XSS Protection in Rails 3
Cheers

Resources