Skip to content

Instantly share code, notes, and snippets.

@1stvamp
Last active January 1, 2016 22:49
Show Gist options
  • Save 1stvamp/8212989 to your computer and use it in GitHub Desktop.
Save 1stvamp/8212989 to your computer and use it in GitHub Desktop.
Python module to allow the simple programmatic generation of Markdown mark-upped text.
def header(content, depth=1, underline=False):
if underline:
char = ('=', '-', '*')[(depth if depth < 4 else 3)]
return "{0}\n{1}".format(content, (char * len(content)))
return "{0} {1}".format(('#' * depth), content)
def quote(content, prefix='> '):
return "\n".join("> {0}".format(c) for c in content.split("\n"))
def paragraph(content):
return "{0}\n\n".format(content)
def emphasis(content, strength=1, char='*'):
return "{0}{1}{0}".format((char * strength), content)
def ulist(content, marker='*'):
return "\n".join("* {0}".format(c) for c in content)
def olist(content):
return "\n".join("{0}. {1}".format((i + 1), c) for i, c in enumerate(content))
def link(content, address, title=None):
title = ' "{0}"'.format(title) if title is not None else ''
return "[{0}]({1}{2})".format(content, address, title)
def image(content, address, title=None):
return "!{0}".format(link(content, address, title))
def pre(content, inline=False):
if inline:
return "`{0}`".format(content)
return quote(content, ' ')
if __name__ == '__main__':
content = ''
content += paragraph('Hello world!')
content += paragraph(link('go here', 'http://example.com/') + ' or have a ' + image('kitten', 'http://www.catster.com/files/post_images/35653cc7aab32e647da2cd81e37ed1c9.gif'))
content += ulist(['foobar', 'well hello there', emphasis('yo momma')])
print content

Hello world!

go here or have a kitten

  • foobar
  • well hello there
  • yo momma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment