def collect_input(script, input):
# determine file extension
if input is not None:
if isinstance(input, (str, file_target)):
ext = os.path.splitext(input)[-1]
elif isinstance(input, Sequence) and len(input) > 0:
ext = os.path.splitext(input[0])[-1]
else:
raise ValueError('Unknown input file for action pandoc')
else:
ext = '.md'
input_file = tempfile.NamedTemporaryFile(mode='w+t', suffix=ext, delete=False).name
with open(input_file, 'w') as tmp:
if script is not None and script.strip():
tmp.write(script.rstrip() + '\n\n')
if isinstance(input, str):
try:
with open(input) as ifile:
tmp.write(ifile.read() + '\n\n')
except Exception as e:
raise ValueError(f'Failed to read input file {input}: {e}')
elif isinstance(input, Sequence):
for ifile in input:
try:
with open(ifile) as itmp:
tmp.write(itmp.read().rstrip() + '\n\n')
except Exception as e:
raise ValueError(f'Failed to read input file {ifile}: {e}')
return input_file
评论列表
文章目录