Guide to embedding code in Markdown

When I first read the wikipedia article on markdown, I figured that code highlighting would be easy as pie and that nothing would stop me from embedding all kinds of cool code in my blogposts. I would soon be wiser...

The first problem I had was that my output looked like this:

# Let's initialize foo 
foo=10
if foo==10:
    # oh my god, foo is 10, let's print something!
    print 'Woop woop, foo is %d' % (foo)

See that ugly red squares above? I had a bit of troube finding out how to fix that. It turns out that it's fairly easy. :-)

This is the markdown producing the ugly code fragment above:

    # Let's initialize foo  
    foo=10
    if foo==10:
        # oh my god, foo is 10, let's print something!
        print 'Woop woop, foo is %d' % (foo)

To make it pretty, we need to specify what kind of code we are dealing with. You simply ident the code as you normaly would, but preceedes the code with a :::programming-language-name tag. For instance, to produce a pretty python code fragment, I would do this:

    :::python
    # Let's initialize foo  
    foo=10
    if foo==10:
        # oh my god, foo is 10, let's print something!
        print 'Woop woop, foo is %d' % (foo)

and it would look like this:

# Let's initialize foo  
foo=10
if foo==10:
    # oh my god, foo is 10, let's print something!
    print 'Woop woop, foo is %d' % (foo)

Easy, huh!? Pelican uses pygments to parse the code, to see a list of all the supported programming languages, go here.


Written by Jannich Brendle ons 14 august 2013 In Blogging

tags: programmingblogginghowto

Related posts

social