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:
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.