以文本编辑器的形式在HTML中显示原始文本 [英] Displaying raw text in HTML like a text editor

查看:478
本文介绍了以文本编辑器的形式在HTML中显示原始文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据以下规则,我想在网页中显示一个UTF8文本字符串(或整个< body> >)




  • 所有字符都应该像在unicode-aware文本编辑器中那样字面显示。


  • 空白不应该被忽略。 TAB应缩进一些空格(例如4或8)。 LF应该会导致换行符。

  • 文本应该自动换行(正如< p> code>段落,或者在文本编辑器打开文字包装的情况下打开文本文件),因为文本文件中包含div的宽度发生了变化。




显然,我需要在文本字符串上执行一些预处理步骤,然后将其插入到网页中(也许我需要用命名实体替换某些字符& foo; 。也许我需要用一个特定的标记<< pre> 来包围文本文件,例如,等等)。



正是我需要执行哪些预处理步骤才能实现上述目标。如果有多组步骤达到上述目的,请用最短/最简单的答案回答。 解决方案

<1> (3)实际上很简单,如果你以UTF-8的形式提供你的文档,并且支持IE> = 8就没问题了。然后是:

 < body style =white-space:pre-wrap> 
(Your String here,'&'分别编码为'& lt;'和'& amp;')
< / body>



对于(2),您将需要预处理步骤(a < s / \ t / / g )或等到所有浏览器都支持标签大小


I have a UTF8 text string that I would like displayed in a div (or the entire <body>) in a web page according to the following rules:

  • All characters should be shown literally as they would in a unicode-aware text editor.

  • Whitespace should not be ignored. TABs should indent to some number of spaces (4 or 8 for example). LFs should cause a line break.

  • Text should be automatically word wrapped (as it would for a normal <p> paragraph, or as it would in a text editor with word wrap turned on) as the width of the containing div of the text file changes.

Clearly I need to perform some preprocessing steps on the text string before I insert it into the web page (perhaps I need to replace certain characters with named entities &foo;. Perhaps I need to surround the text file with a certain tag, <pre> for example, etc).

Precisely what preprocessing steps do I need to perform to achieve the above. If there are multiple sets of steps that achieve the above, than please answer with the shortest/simplest.

解决方案

(1) and (3) are actually quite simple, if you serve your document as UTF-8 and you're fine with supporting IE >= 8. Then it's:

<body style="white-space: pre-wrap">
  (Your String here, '<' and '&' encoded to '&lt;' and '&amp;' respectively)
</body>

The white-space CSS property is the key here.

For (2) you will either need a pre-processing step (a la s/\t/ /g) or wait until all browsers support tab-size.

这篇关于以文本编辑器的形式在HTML中显示原始文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆