Skip to content

Instantly share code, notes, and snippets.

@Ceasar Ceasar/md2.py
Created Jul 18, 2013

Embed
What would you like to do?
"""
Convert markdown files containing Python code into working Python files.
Usage: python md2.py [markdownfile]
Example:
Flask is Fun
============
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
Interested?
===========
- [Download latest release](http://pypi.python.org/packages/source/F/Flask/Flask-0.10.tar.gz) (0.10)
- [Fork it on Github](https://github.com/mitsuhiko/flask)
Becomes:
# Flask is Fun
# ============
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
# Interested?
# ===========
# - [Download latest release](http://pypi.python.org/packages/source/F/Flask/Flask-0.10.tar.gz) (0.10)
# - [Fork it on Github](https://github.com/mitsuhiko/flask)
"""
import sys
TAB_WIDTH = 4
INDENT = ' ' * TAB_WIDTH
if __name__ == "__main__":
with open(sys.argv[1]) as f:
for line in f:
if not line.strip():
print ""
elif line.startswith(INDENT):
print line[TAB_WIDTH:].rstrip()
else:
print "# %s" % line.rstrip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.