|
How to use PRE tags to preserve formatting, and improve readability of code snippets or tabular data in posts. category 'KB', language HTML, created 03-Sep-2009, version 32 (14-Mar-2011), by Luc Pattyn This article also appeared on the CodeProject. |
|
License: The author hereby grants you a worldwide, non-exclusive license to use and redistribute the files and the source code in the article in any way you see fit, provided you keep the copyright notice in place; when code modifications are applied, the notice must reflect that. The author retains copyright to the article, you may not republish or otherwise make available the article, in whole or in part, without the prior written consent of the author. Disclaimer: This work is provided |
When publishing code or some text that needs a strict syntax (in an article, a forum message, a tip, whatever) ALWAYS use the appropriate tags, which is either CODE tags (for a small snippet, no more than one line) or PRE tags (for all multi-line snippets).
The overall advantage is better readability, and as a result reaching a broader audience and getting more and better feedback. In detail:
As this is simple and very effective, I can tell you I simply no longer read code that is not tagged appropriately; if the author doesn't care, neither do I.
Here is an example (with the PRE tags shown explicitly):
<pre lang='cs'> / * the opening PRE tag (normally not visible!) */
private static int multiply(int arg1, int arg2) {
return arg1*arg2;
}
</pre> / * the closing PRE tag (normally not visible!) */
Warning: If you are using a multi-mode editor, you must be in "raw mode" (click the <> ) to enter and edit HTML tags such as PRE and CODE.
And here is a list of language codes, it may be incomplete and there might be small errors, it is a dynamic thing:
text plain text (= no syntax coloring)
cpp C++
cs C#
vb VB, VB.NET, VBscript
java Java, JavaScript
SQL SQL
midl Microsoft IDL
asm Assembly
msil MSIL
xml XML
html html
css CSS
It may come as a surprise however CodeProject does not take the content of PRE blocks literally, a number of HTML tags are accepted, others ignored and removed. Here is a non-exhaustive list of the ones that seem to work:
<b>text</b> generates bold text (which can not be discerned well at all, so I don't recommend it);
<i>text</i> italicizes the text
<u>text</u> underlines the text
Inner warning: it may work differently or not at all in preview mode! Well, actually it mostly does not work at all, only bold works a little bit.
A huge consequence is you have to HTML-escape the special characters < and > and & i.e. you must replace them like so:
< <
> >
& &
This escaping can be performed manually (when in "raw view") or by cheking the "Encode < symbol while pasting" checkbox in the forums. Pasting code in Q&A normally does it automatically, at the same time it adds PRE tags. When the < symbols aren't escaped correctly, parts of the snippet may vanish, and what remains may be shown in unintended colors. When done properly it all should look like < > & in "HTML view" or in preview.
Inner warning: Depending on the browser used it may seem sufficient to escape just <, however some browsers (and I suspect some CodeProject code too) don't work well when the others are left unescaped.
Here is an example using most of the above:
<pre linecount="on" lang="cs" style='background-color:#DDFFDD'>
private int <b>multiply</b>(int arg1, int arg2) {
return <span class='emphasis'>arg1*arg2</span>;
}</pre>
which yields:
private int multiply(int arg1, int arg2) {
return arg1*arg2;
}
Perceler |
Copyright © 2012, Luc Pattyn |
Last Modified 21-May-2025 |