views:

172

answers:

3

I'm rebuilding my blog at http://www.elmalabarista.com/blog/. I have use in my previous version markdown and now I remember why I have almost zero code samples. Doing code samples in markdown is very fragile.

I try to put some python there I can't make markdown mark it as code!. The main culprit? The syntax is markdown for code is out spaces. Despite the fact I use wmd as the editor (how that work here in SO is a mistery for me), it never be able to move rigth the text so never get as code. this is the problem:

I put something simple:

:::python
def hello():
   pass

But the problem is that something I have:

:::python def hello(): pass

or

:::python def hello(): pass

(yes bot was formatted but you see? not work). Any mistake -using tabs, too much, too litle spaces) is punished heavily. And if the code is long, the chance of a problem increase too.

So, exist any way to simply do:

### My article

  1. My list entry one
  2. My list entry two

:::python def x (a, b): return a * b

and get converted rigth and never bother about this small thing again???

By the way, I'm using pygments & python markdown.

+3  A: 

Consider using reStructuredText -- it's the standard lightweight markup for Python, and is often used for docstrings and embedded documentation. It's quite easy, but also powerful -- if I remember correctly, the core Python libraries and Django both use it.

John Millikin
Yes, in combination with sphinx (http://sphinx.pocoo.org/) it's a great tool. If I remember correctly Doug Hellmann (http://www.doughellmann.com/PyMOTW/) mentioned he uses rest/sphinx for the Python module of the week blog.
monkut
reST seconded. The docutils library which implements it in Python is very friendly and simple to understand and extend
Eli Bendersky
But have the same problem. I follow http://www.codekoala.com/blog/2008/syntax-highlighting-rest-pygments-and-django/ and still is necesary to precisely indent each line of code to the formating works. So, no candy.
mamcx
I don't understand. The problem is that the syntax for code blocks uses indentation? If so, the best solution is to add a macro to your editor to indent by four spaces. Then you just paste the code listing, select it, indent, and it'll work.
John Millikin
Except when not work!I try to paste 6 times before have it *totally* rigth. Is easy in few lines but is not that reliable for long code...
mamcx
+1  A: 

I've been using google-code-prettify, which works pretty well.

Usage:

Put code snippets in <pre class="prettyprint">...</pre> or <code class="prettyprint">...</code> and it will automatically be pretty printed.

monkut
Ok, but I format the code with markdown. I if I use this kind f JS solutions I will need to manually out the html tags, defeating the use of a text-only content.
mamcx
you can put standard html tags in markdown and markdown won't mess with them... so why not <code> and </code>?
Mica
A: 

You need to indent code more than 4 spaces (btw have you noticed on SO if you add 4 spaces it gets recognized as code), this is 4 space indented:

:::python
def hello():
    pass
stefanB