Skip to content

Commit

Permalink
Normalize output format handling (-t) to match input (-f)
Browse files Browse the repository at this point in the history
  • Loading branch information
alerque committed May 22, 2019
1 parent a2a70c4 commit 0efd83a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
12 changes: 6 additions & 6 deletions pancritic/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)

Expand Down
41 changes: 19 additions & 22 deletions pancritic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -67,7 +67,7 @@ def get_args():
parser.add_argument('-o', '--output',
help='Output file. Default: stdout.')

parser.add_argument('-t', '--to',
parser.add_argument('-t', '--to', dest='to_format',
help='Output format. Default: inferred from --output.')
parser.add_argument('-f', '--from', dest='from_format',
help='Input format. Default: inferred from --input.')
Expand Down Expand Up @@ -110,27 +110,24 @@ 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.", file=sys.stderr)
output_format = 'html'
# to-format (remember args.output is a string)
if not args.to_format:
try:
args.to_format = normalize_format(os.path.splitext(args.output)[1][1:])
except TypeError:
print("No output file extension nor to-format specified. Default to HTML.", file=sys.stderr)
args.to_format = 'html'

if args.to is None:
args.to = output_format

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')
elif args.output is None:
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():
Expand Down

0 comments on commit 0efd83a

Please sign in to comment.