Skip to content

Instantly share code, notes, and snippets.

@ptbrowne
Last active February 26, 2018 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptbrowne/cce1adfe238646cea5d61881f01f9c00 to your computer and use it in GitHub Desktop.
Save ptbrowne/cce1adfe238646cea5d61881f01f9c00 to your computer and use it in GitHub Desktop.
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