markdown-textwrap

text wrap markdown documents
git clone git://git.ricketyspace.net/markdown-textwrap.git
Log | Files | Refs

commit 937ff835b5808f184162d68d5eb93e12a7c5de86
parent e357ce168c8f6a78df349fbb262d71bf46fe11b5
Author: rsiddharth <s@ricketyspace.net>
Date:   Sun, 25 Mar 2018 19:44:00 +0000

md_tw.py: Update main.

* md_tw.py (main): Update function.

Diffstat:
md_tw.py | 33++++++++++++++++++++++++++++++++-
1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/md_tw.py b/md_tw.py @@ -18,6 +18,7 @@ # along with markdown-textwrap (see COPYING). If not, see # <http://www.gnu.org/licenses/>. +import argparse import re import textwrap @@ -516,4 +517,34 @@ class TWMarkdown(mistune.Markdown): def main(): - print('USAGE: md_tw 72 file.md file2.md [...]') + def parse_args(): + parser = argparse.ArgumentParser() + + # Options for args. + w_opts = { + 'type': int, + 'dest':'width', + 'default':72, + 'help': 'Max. line width. Default is 72.' + } + f_opts = { + 'type': argparse.FileType('r'), + 'help': 'File path of Markdown document.' + } + + # Define expected args. + parser.add_argument('-w', '--width', **w_opts) + parser.add_argument('md_file', **f_opts) + + # Parse 'em. + a = parser.parse_args() + + return {'width': a.width, 'text': a.md_file.read()} + + def wrap(text, width): + return TWMarkdown(tw_width=width)(text) + + def out(text): + print('{}'.format(text)) + + return out(wrap(**parse_args()))