Many people who find this post by searching will want to know how to format text as a block of code easily. To do that, select the text in the visual editor (i.e., the text box on Ask Ubuntu where you are writing or pasting it) and press Ctrl+K or click the button that looks like {}. This indents each line by four spaces, causing it to be rendered as a code block. With that said...
You probably still don't want to use <pre>
(or even <pre><code>
) for this.
I advise against using HTML tags to format code on Ask Ubuntu, outside of special situations where you really need them (which, for a complete Python program, is probably never). The reason is that, when you make a code block that way, HTML is still treated specially inside it. This includes the <
and >
characters, which often appear in source code.
This happens because Markdown code formatting does more than just adding tags. It also performs any necessary escaping in the text that is to be treated as code. As the Markdown help page says:
Indent four spaces to create an escaped <pre> <code>
block
(emphasis mine)
But if you really must do it, it seems like you can.
There may be bugs, though.
The first problem you're running into is that, although <pre>
</pre>
displays like code on Stack Exchange sites, and is in general usually rendered in monospace, but it does not actually mean code.
As the editing guide (see above) and muru say, the HTML markup for a preformatted code block is <pre><code>
</code></pre>
.
<!-- language: lang-python -->
<pre><code>#!/usr/bin/env python
import os;
for r,s,f in os.walk("."):
for i in f:
if "postgis-2.0.0" in i:
print(os.path.join(r,i))</code></pre>
This answer on Code Review Meta claims it works.
I recommend placing two newlines between the HTML comment and <pre>
, as shown above. This corresponds to the way a <!-- language: lang-something -->
comment is usually used with four-space indenting (i.e., the comment isn't indented, and a separate indented block follows it).
This works for me in the visual editor on the main site, sometimes, and it seem to recall that code like this is rendered properly after submission. If you're doing it like that, then I'm not sure what the problem is. When writing or editing posts, I have noticed that it often takes a few seconds--and sometimes much longer--for syntax highlighting to be applied. I imagine this is a browser rendering issue, but I have not noticed a problem in submitted posts, only in the visual editor's preview. I suspect a bug in the visual editor.
Another possibility is that perhaps syntax highlighting does not work the same (or at all?) here on meta. For example, I applied code highlighting to the HTML above (see the Markdown for this answer), which is formatted through the usual technique of indentation, but it's not rendering in HTML.
This is what the code shown above looks like (i.e, this is that, unindented). It seems to work on the main site, though I don't know if it's reliable. It does not seem to highlight here on meta.
#!/usr/bin/env python
import os;
for r,s,f in os.walk("."):
for i in f:
if "postgis-2.0.0" in i:
print(os.path.join(r,i))
Here it is with four-space indentation:
#!/usr/bin/env python
import os;
for r,s,f in os.walk("."):
for i in f:
if "postgis-2.0.0" in i:
print(os.path.join(r,i))
Unless something else in my post is getting in the way, it looks like meta doesn't support this. I have tested this from multiple browsers on Ubuntu and Windows, as well as the Stack Exchange app for Android, and syntax highlighting does not appear--not in this post, at least.
<pre>
tags, use:%s/^/ /
Shift+>
, but I'm just curious is it's possible with<pre>
tag.<pre><code>
blocks<
and>
characters into your programs (and others), since<pre>
and even<pre><code>
blocks can contain HTML.