Skip to content

Instantly share code, notes, and snippets.

@ptbrowne ptbrowne/make-doc.py
Last active Feb 26, 2018

Embed
What would you like to do?
make-doc and executes samples
"""
Will render markdown to markdown.
Will execute execbash code blocks and add their output to the code block.
"""
import mistune
import sh
from textwrap import wrap
def wrapped(txt):
return '\n'.join(wrap('%s' % txt, 78))
class ExecBashMardownRenderer(mistune.Renderer):
def newline(self):
return ''
def paragraph(self, content):
return '''\n%s\n''' % wrapped(content)
def hrule(self):
return '-----------'
def header(self, text, level, raw):
return '#' * level + ' ' + text + '\n'
def codespan(self, txt):
return '`%s`' % txt
def list_bullet(self, txt):
return '\n* ' + wrap(txt)
def list_item(self, txt):
return '\n-' + (' ' if txt.startswith('`') else '') + wrapped(txt) + '\n'
def list(self, body, ordered=True):
return body
def block_code(self, code, lang):
if lang == 'execbash':
return '\n```bash\n$ %s\n%s\n```\n' % (code, sh.bash('-c', code))
return '\n\n```%s\n%s\n```\n' % (lang or '', code)
def image(self, src, title, text):
return '![%s](%s)' % (text, src)
renderer = ExecBashMardownRenderer()
markdown = mistune.Markdown(renderer=renderer)
if __name__ == '__main__':
with open('.README.template') as f:
content = f.read()
with open('README.md', 'w+') as w:
w.write(markdown(content))
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.