如何将 RedCloth.to_html 转换回可编辑的普通文本?

发布于 2024-10-06 16:21:45 字数 616 浏览 0 评论 0原文

我使用 RedCloth 将纯文本保存在表单中并将其转换为 HTML。例如,在我的表单中写入并保存它,将使其显示与我编写的方式完全相同:

This sentence
gets inserted into it
proper html syntax
to preserve line breakage.

使用此代码:

def parse_code
  self.text = RedCloth.new(text).to_html
end

然后我可以使用以下代码重新显示它:

= raw post.text

但是当我想编辑它时,它会返回到我:

<p>This sentence</p>
<p>gets inserted into it</p>
<p>proper html syntax</p>
<p>to preserve line breakage</p>

我怎样才能做到这一点,以便当我编辑它时,它看起来和我保存它之前一样?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彻夜缠绵 2024-10-13 16:21:45

我会将纺织代码保留在 Textile 中,并仅在视图中转换为 HTML:在

= raw RedCloth.new(@post.text).to_html

Textile 和 HTML 之间进行转换感觉不是一个好的做法。您的 parse_code 方法似乎导致您的文本转换为 HTML.. 并存储到数据库中。

但是,如果您想将 HTML 转换为纺织品,也许 clothred 适合您,或者阅读 此博客

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.

征﹌骨岁月お 2024-10-13 16:21:45

编辑:射击!我误解了这个问题!

您可以使用 ClothRed 将该文本区域的值分配回纺织品:

ClothRed.new(html).to_textile

抱歉!

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!

作妖 2024-10-13 16:21:45

如果我理解正确的话,您正在将 HTML 输出存储在数据库中。不要这样做,而是存储原始 Textile 内容,然后在向用户显示时将其转换为 HTML。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文