Note that there are some explanatory texts on larger screens.

plurals
  1. PORendering Plaintext as HTML maintaining whitespace – without <pre>
    primarykey
    data
    text
    <p>Given any arbitrary text file full of printable characters, how can this be converted to HTML that would be rendered exactly the same (with the following requirements)?</p> <ul> <li>Does not rely on any but the default HTML whitespace rules <ul> <li>No <code>&lt;pre&gt;</code> tag</li> <li>No CSS <code>white-space</code> rules</li> </ul></li> <li><code>&lt;p&gt;</code> tags are fine, but not required (<code>&lt;br /&gt;</code>s and/or <code>&lt;div&gt;</code>s are fine)</li> <li><p>Whitespace is maintained exactly.</p> <p>Given the following lines of input (ignore erroneous auto syntax highlighting):</p> <pre><code>Line one Line two, indented four spaces </code></pre> <p>A browser should render the output exactly the same, maintaining the indentation of the second line and the gap between "indented" and "spaces". Of course, I am not actually looking for monospaced output, and the font is orthogonal to the algorithm/markup.</p> <p>Given the two lines as a complete input file, example correct output would be:</p> <pre><code>Line one&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Line two, indented&amp;nbsp;&amp;nbsp;&amp;nbsp; four spaces </code></pre></li> <li><p>Soft wrapping in the browser is desirable. That is, the resulting HTML should not force the user to scroll, even when input lines are wider than their viewport (assuming individual words are still narrowing than said viewport).</p></li> </ul> <p>I’m looking for fully defined <strong>algorithm.</strong> Bonus points for implementation in <strong>python</strong> or <strong>javascript</strong>.</p> <p><em>(Please do not just answer that I should be using <code>&lt;pre&gt;</code> tags or a CSS <code>white-space</code> rule, as my requirements render those options untenable. Please also don’t post untested and/or naïve suggestions such as “replace all spaces with <code>&amp;nbsp;</code>.” After all, I’m positive a solution is technically possible — it’s an interesting problem, don’t you think?)</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload