ruby-on-rails - 将简单文本转换为 html 以尊重换行符空格和制表符
问题描述
编写一个将简单文本文件转换为html的方法,以便可以将其集成到ckeditor中。但是,我们无法使用任何 RoR gem 转换空格、制表符和换行符
我试过了
output_html = ActionController::Base.helpers.simple_format(contents)
output_html = output_html.gsub(/ (?= )/, ' ')
text_content = "firstline\nsecond line \n\n\nthird line\t\taftertab"
output_html = "firstline<br/>second line<br/><br/><br/>third<br/>line aftertab"
解决方案
您可以String.gsub
为此使用:
text_content = "firstline\nsecond line \n\n\nthird line\t\taftertab"
text_content.gsub(/\s/, "\n" => "<br/>", "\t" => " ", " " => " ")
# => "firstline<br/>second line <br/><br/><br/>third line aftertab"
推荐阅读
- sql-server - 按度量排序,但将相关行放在一起
- c# - 无法在同一页面 MVC 中显示 Modal 弹出窗口
- python - 使用 ImageDraw 不会在图像上绘制线条
- node.js - 如何在 mongodb 中插入和更新重复项?
- javascript - 将日期字符串转换为 Postgres 时间戳时区格式
- c++ - catkin_make run_tests 时 Gtest 失败。未定义参考
- ruby-on-rails - 渲染模型字段的集合
- typescript - 在调用我的函数之前,我的 ionic 4 应用程序上的 html 正在显示
- ios - 添加到 UITableViewCell 内容视图时如何调用 UITableview 数据源(cellForRowAt)?
- node.js - 为什么 bin/www 文件在 express-generator 中没有扩展名?