How to convert RedCloth.to_html back into editable vanilla text? - ruby-on-rails

I have with RedCloth saved plain text in a form and converted it to HTML. For example, writing this in my form, and saving it, would make it display the exact same way I wrote it :
This sentence
gets inserted into it
proper html syntax
to preserve line breakage.
With this code :
def parse_code
self.text = RedCloth.new(text).to_html
end
And then I can redisplay it with this :
= raw post.text
But when I want to edit it, it it returns to me as :
<p>This sentence</p>
<p>gets inserted into it</p>
<p>proper html syntax</p>
<p>to preserve line breakage</p>
How can I make it, so that when I edit it, it looks the same way it did before I went and saved it ?
Thanks!

I would leave the textile code stored in textile and do the conversion to HTML only in the view:
= raw RedCloth.new(#post.text).to_html
Converting between textile and HTML does not feel to be a good practice. Your parse_code method seem that it caused your text to be converted to HTML.. and than stored to the Db.
However if you want to convert HTML to textile, maybe clothred is for you or read this blog.

Edit: Shoot! I misunderstood the question!
You'd assign that text area's value back to textile using ClothRed:
ClothRed.new(html).to_textile
Sorry!

If I understood you right, you are storing the HTML output in the database. Instead of doing that, store the raw Textile contents and then convert them to HTML when showing it to the user.

Related

ruby on rails: text filed formatting

I tried to copy and past from Word document to text field using Ruby on Rails.
But all formatting( spaces, bold and other) are disappeared on text filed.
I've just got the simple lines of text without any formatting.
I've read that need to use Simple format tool... but I want that a user be able just to copy and past a text to text field without doing any adjustments.
I mean, I want make all adjustment in advance and the user could just copy and past the text and got all formatting, the same as in Word doc.
The link to file with text field as below.
https://gist.github.com/tatyana12/2f9d39c2f6e4f8fabea5e70e11eaf310
Also I have Application.html.erb file:
https://gist.github.com/tatyana12/15c27d542091b04f3c3adfdfd252b7f4
How to initialize editor if I don't have id = "edit" right now?
How to put some code extra style to this file?
Have a go at wrapping your field in your show.html.erb or wherever you want to display it with simple_format, for example:
<%= simple_format(#object.description) %>
See http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format for more info.
I've solved this problem by implementing SKEditor.
There is a lot of tutorial how to implement this editor.
I (as a lot of other users) have problem that my text wasn't formatted because this editor is not compatible with turbo links.
So, I disable turbo links in some files, and as result I have the text formatted.

Is it possible to render HTML from a ReactComponentB?

I've got an existing HTML page that I have transcribed into scalajs-react scalatags in a ReactComponentB object, but the output is slightly different from the original HTML. Is there a way I can render out the HTML from the ReactComponentB so that I can compare it with my original HTML?
Sounds like React.renderToStaticMarkup() is what you're looking for.

Rendering Pagedown Markdown in Rails 4

I have a properly working pagedown editor in a rails 4 application, but I believe I'm missing something simple to render the pagedown data at a later point in my application.
Here's the gem (and initialization) I'm using: https://github.com/hughevans/pagedown-bootstrap-rails
Any ideas on how to render that data with the already-used gems?
EDIT: I think the root of my problem is it's not storing the data as the HTML version, so it doesn't render it when I display the data again. It's missing the converter step when the form gets saved, but I don't see any specific instruction on how to do that, so I assumed it was default part of these gems.
EDIT2: I've since taken the approach to convert the Markdown on each page load, with the following code:
Unfortunately, it doesn't use all the Pagedown Markdown, but it's at least handling new lines properly:
Any ideas?
Thanks!
So the answer to this question is two fold. Either you can convert the MD to HTML and store it in the database, or leave it as MD in the DB and convert it to HTML every time you want to render it. Note that you'll need to convert it back to MD (I'm not sure if this is entirely easy or not) if you want that field to be editable in the original MD.
Since this app doesn't care about performance, I decided to store it as MD and render it.
The results I was getting above stemmed from HAML's whitespace rendering that it does, so I had to use a little HAML filters to work around that.
The HAML ended up looking like:
.wmd-output><
:preserve
#{#object.attribute}
The second challenge was actually pretty straightforward, just not explicitly stated anywhere in the Markdown documentation. So I just wrote some javascript that automatically converts any .wmd-output class into it's proper Markdown on page load:
$(function() {
$('.wmd-output').each(function(i) {
var converter = new Markdown.Converter();
var content = $(this).html();
$(this).html(converter.makeHtml(content));
});
});
I hope this helps other people in the future.
This line of haml referenced should be what you need to render it:
= f.input :description, :as => :pagedown, :input_html => { :preview => true }

Textareas and unsafe content

I've got wiki style content which is sanitized and stored in another field of the db for output as html. The original body field I'm not sure how to deal with as when I santize it characters are escaped and don't display well in the textarea.
What are the dangers of unsafe content in textareas? I'm sure I read previously that downloading such textarea content with ajax is preferable but I'd rather not go down that route if not necessary.
all HTML tag are no safe. by example if you close the textarea, you can add all nez HTML tag or what you want like JS. So it's exactly like inside a non textarea tag.

how to show contents which include html tag?

I am using FckEditor in Create.aspx page in asp.net mvc application.
Since I need to show rich text in web pages, I used ValidateInput(false) attribute top of action method in controller class.
And I used Html.Encode(Model.Message) in Details.aspx to protect user's attack.
But, I had result what I did not want as following :
<p> Hello </p>
I wanted following result not above :
Hello
How can I show the text what user input?
Thanks in advance
The short answer is that HTMLEncode is making your markup show like that. If you don't HTMLEncode, it will do what you want.
You need to think about whether or not you need full control of markup, who is entering the markup, and if an alternative like BBCode is an option.
If your users using the editor are all sure to be 'safe' users, then XSS isn't likely to be as much a concern. However, if you are using this on a comment field, then BBCode, or something like SO itself uses is more appropriate.
You wont be able to use a WYSIWYG editor and do HTMLEncode though... (without BBCode, or some other token system)
It seems the user entered "<p> Hello </p>" (due to pressing Enter?) into the edit control, and it is displaying correct in the HTML as you have done an Html.Encode. E.g. the paragrahs are not rendered, they are outputted as "<p>..</p>" as the string is HTML encoded into something like "<p> Hello <p>".
If you do not want tags, I would suggest searching the text string for tags (things with <...>) and removing them from the inputted text. Do this before HTML.Encode.
...or am I missing something?
You can use HttpServerUtility.HtmlEncode(String)

Resources