Last active
February 26, 2018 15:51
-
-
Save ptbrowne/cce1adfe238646cea5d61881f01f9c00 to your computer and use it in GitHub Desktop.
make-doc and executes samples
This file contains hidden or 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
""" | |
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 '' % (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