Created
February 28, 2013 18:11
-
-
Save elmiko/5058823 to your computer and use it in GitHub Desktop.
I liked the ideas put forward by the 1.5.0 version of CoffeeScript for using Markdown documents as both source file and documentation. Here is a quick and dirty Python file to do the same type of preprocessing for any language.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
import markdown | |
import os.path | |
from bs4 import BeautifulSoup | |
def read_code_from_file(mdfile): | |
"""returns a string of the code sections in the Markdown file""" | |
origfilepos = mdfile.tell() | |
mdfile.seek(0) | |
html = markdown.markdown(mdfile.read()) | |
soup = BeautifulSoup(html) | |
codestring = '' | |
for codeblock in soup.find_all('code'): | |
if len(codestring) > 0: | |
codestring += '\n' | |
codestring += codeblock.text | |
mdfile.seek(origfilepos) | |
return codestring | |
def write_file_from_code(originalfilename, extension, code): | |
"""write a new file of source code""" | |
basename = os.path.basename(originalfilename) | |
dirname = os.path.dirname(originalfilename) | |
newname = basename.split('.')[0] + '.' + extension | |
newfile = open(os.path.join(dirname, newname), 'w') | |
newfile.write(code) | |
newfile.close() | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser( | |
description='Strip the code segments from a Markdown file.') | |
parser.add_argument( | |
'files', metavar='F', type=file, nargs='+', | |
help='a markdown file to strip') | |
parser.add_argument( | |
'--ext', dest='extension', required=True, | |
help='the extension to use for stripped files') | |
args = parser.parse_args() | |
for mdfile in args.files: | |
code = read_code_from_file(mdfile) | |
write_file_from_code(mdfile.name, args.extension, code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment