def cli(): """Execute for command line usage.""" description = "Create an IPython notebook from markdown." example_use = "Example: notedown some_markdown.md > new_notebook.ipynb" parser = argparse.ArgumentParser(description=description, epilog=example_use) parser.add_argument('input_file', help="markdown input file (default STDIN)", nargs="?", type=argparse.FileType('r'), default=sys.stdin) parser.add_argument('--output', help="output file, (default STDOUT)", type=argparse.FileType('w'), default=sys.stdout) parser.add_argument('--code_block', help=("choose to match only 'fenced' or 'indented' " "code blocks or give a regular expression to " "match code blocks. Will be compiled with " "re.MULTILINE | re.VERBOSE." "Default is to match both " "fenced and indented code blocks."), default=None) args = parser.parse_args() with args.input_file as ip, args.output as op: # if no stdin and no input file if args.input_file.isatty(): parser.print_help() exit() reader = MarkdownReader(code_regex=args.code_block) writer = JSONWriter() notebook = reader.read(ip) writer.write(notebook, op)
def cli(): """Execute for command line usage.""" description = "Create an IPython notebook from markdown." example_use = "Example: notedown some_markdown.md > new_notebook.ipynb" parser = argparse.ArgumentParser(description=description, epilog=example_use) parser.add_argument('input_file', help="markdown input file (default STDIN)", nargs="?", type=argparse.FileType('r'), default=sys.stdin) parser.add_argument('--output', nargs='?', help="output file, (default STDOUT)", type=argparse.FileType('w'), default=sys.stdout) parser.add_argument('--code_block', help=("choose to match only 'fenced' or 'indented' " "code blocks or give a regular expression to " "match code blocks. Will be compiled with " "re.MULTILINE | re.VERBOSE." "Default is to match both " "fenced and indented code blocks."), default=None) parser.add_argument('--pre', nargs='+', default=[], help=("additional code to place at the start of the " "notebook, e.g. --pre '%%matplotlib inline' " "'import numpy as np'")) parser.add_argument('--knit', nargs='?', help=("pre-process the markdown with knitr. " "Default chunk options are 'eval=FALSE' " "but you can change this by passing a string. " "Requires R in your path and knitr installed."), const='eval=FALSE') parser.add_argument('--rmagic', action='store_true', help=("autoload the rmagic extension. Synonym for " "--pre '%%load_ext rmagic'")) parser.add_argument('--nomagic', action='store_false', dest='magic', help=("disable code magic.")) args = parser.parse_args() # if no stdin and no input file if args.input_file.isatty(): parser.print_help() exit() # temporary output file because knitr works best with files tmp_out = ".knitr.tmp.output" # maybe think about having a Knitr class that uses a tempfile, # with a knit method that returns a string if args.knit and args.output is sys.stdout: output = tmp_out elif args.knit and not args.output: markdown = os.path.splitext(args.input_file.name)[0] + '.md' notebook = os.path.splitext(args.input_file.name)[0] + '.ipynb' output = markdown args.output = open(notebook, 'w') elif args.knit and args.output: output = args.output.name if args.knit: knit(args.input_file.name, output, opts_chunk=args.knit) # make the .md become the input file args.input_file = open(output, 'r') precode = args.pre if args.rmagic: precode.append(r"%load_ext rmagic") with args.input_file as ip, args.output as op: kwargs = {'code_regex': args.code_block, 'precode': precode, 'magic': args.magic} reader = MarkdownReader(**kwargs) writer = JSONWriter() notebook = reader.read(ip) writer.write(notebook, op) if os.path.exists(tmp_out) and args.knit and args.output is sys.stdout: os.remove(tmp_out)