From 5f33e9f2785fcce0965603c07e39710cef01700d Mon Sep 17 00:00:00 2001 From: Caleb Maclennan <caleb@alerque.com> Date: Fri, 12 Apr 2019 13:19:57 +0300 Subject: [PATCH] Normalize output format handling (-t) to match input (-f) --- pancritic/filters.py | 12 ++++++------ pancritic/main.py | 39 ++++++++++++++++++--------------------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/pancritic/filters.py b/pancritic/filters.py index 48f0bb6..18ffc5a 100644 --- a/pancritic/filters.py +++ b/pancritic/filters.py @@ -147,16 +147,16 @@ def _markdown_filter(body): return body -def pandoc_filter(body, input_format, output_format, standalone, engine, outputfile=None): +def pandoc_filter(body, from_format, to_format, standalone, engine, outputfile=None): extra_args = ['-s'] if standalone else [] - def panflute_filter(body, input_format, output_format, extra_args, outputfile): + def panflute_filter(body, from_format, to_format, extra_args, outputfile): from panflute import convert_text - return convert_text(body, input_format=input_format, output_format=output_format, extra_args=extra_args) + return convert_text(body, input_format=from_format, output_format=to_format, extra_args=extra_args) - def pypandoc_filter(body, input_format, output_format, extra_args, outputfile): + def pypandoc_filter(body, from_format, to_format, extra_args, outputfile): from pypandoc import convert_text - return convert_text(body, output_format, input_format, extra_args=extra_args, outputfile=outputfile) + return convert_text(body, to_format, from_format, extra_args=extra_args, outputfile=outputfile) engines = ('panflute', 'pypandoc') if engine == 'panflute' else ('pypandoc', 'panflute') engine_function = { @@ -169,7 +169,7 @@ def pypandoc_filter(body, input_format, output_format, extra_args, outputfile): # i != 0 means failing last time if i != 0: print('Use {} instead.'.format(engine), file=sys.stderr) - return engine_function[engine](body, input_format, output_format, extra_args, outputfile) + return engine_function[engine](body, from_format, to_format, extra_args, outputfile) except: print('Cannot use {}.'.format(engine), file=sys.stderr) diff --git a/pancritic/main.py b/pancritic/main.py index 9dc59c7..5cefb45 100644 --- a/pancritic/main.py +++ b/pancritic/main.py @@ -18,11 +18,11 @@ def normalize_format(ext): return ext -def main(args, body, output_format, is_binary): +def main(args, body, is_binary): # parse CritiMarkup # diff/markup mode if args.critic_mode[0] in ('d', 'm'): - if args.to in ('latex', 'pdf'): + if args.to_format in ('latex', 'pdf'): body = criticmarkup_tex_diff_filter(body) # for any other format, use HTML (many formats support inline HTML) else: @@ -37,21 +37,21 @@ def main(args, body, output_format, is_binary): print('Unknown critic mode {}.'.format(args.critic_mode), file=sys.stderr) # convert between to and from format - # only convert markdown to html or tex if the output extension is really that format - if args.from_format == 'markdown' and output_format == 'html' and args.engine in ('markdown', 'markdown2'): + # only convert markdown to html or tex if the output format is really that format + if args.from_format == 'markdown' and args.to_format == 'html' and args.engine in ('markdown', 'markdown2'): body = markdown_filter(body, args.engine) body = html_filter(body, args.critic_template, args.critic_mode[0], args.standalone) - elif output_format != args.from_format: + elif args.to_format != args.from_format: # as long as the final format will be latex, don't add html_filter - if args.to not in ('latex', 'pdf'): + if args.to_format not in ('latex', 'pdf'): # defer standalone to pandoc body = html_filter(body, args.critic_template, args.critic_mode[0], False) if is_binary: # only pypandoc handles binary output - body = pandoc_filter(body, args.from_format, output_format, args.standalone, 'pypandoc', outputfile=args.output) + body = pandoc_filter(body, args.from_format, args.to_format, args.standalone, 'pypandoc', outputfile=args.output) else: - body = pandoc_filter(body, args.from_format, output_format, args.standalone, args.engine) - elif args.to != 'latex': + body = pandoc_filter(body, args.from_format, args.to_format, args.standalone, args.engine) + elif args.to_format != 'latex': body = html_filter(body, args.critic_template, args.critic_mode[0], args.standalone) # write (if is binary, already written above) @@ -110,19 +110,16 @@ def get_args(): args.output = args.input.name args.input.close() - - # output-format (remember args.output is a string) - try: - output_format = normalize_format(os.path.splitext(args.output)[1][1:]) - except TypeError: - print("No output file extension nor to-format specified. Default to HTML.") - output_format = 'html' - - if args.to is None: - args.to = output_format + # to-format (remember args.output is a string) + if not args.to_format: + if args.output.name != '<stdout>': + args.to_format = normalize_format(os.path.splitext(args.output)[1][1:]) + else: + print("No output file extension nor to-format specified. Default to HTML.") + args.to_format = 'html' - is_binary = output_format in ("odt", "docx", "epub", "epub3", "pdf") + is_binary = args.to_format in ("odt", "docx", "epub", "epub3", "pdf") if not is_binary: args.output = sys.stdout if args.output is None else open(args.output, 'w') @@ -130,7 +127,7 @@ def get_args(): print('Cannot output binary format to stdout', file=sys.stderr) exit(1) - return args, body, output_format, is_binary + return args, body, is_binary def cli():