Skip to content

Commit 11c13ce

Browse files
committed
use a tempfile for intermediate html file for pandoc in importer
1 parent 83a8059 commit 11c13ce

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

pelican/tools/pelican_import.py

+34-36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import re
88
import subprocess
99
import sys
10+
import tempfile
1011
import time
1112
from collections import defaultdict
1213
from html import unescape
@@ -785,9 +786,8 @@ def fields2pelican(
785786
print(out_filename)
786787

787788
if in_markup in ('html', 'wp-html'):
788-
html_filename = os.path.join(output_path, filename + '.html')
789-
790-
with open(html_filename, 'w', encoding='utf-8') as fp:
789+
with tempfile.TemporaryDirectory() as tmpdir:
790+
html_filename = os.path.join(tmpdir, 'pandoc-input.html')
791791
# Replace newlines with paragraphs wrapped with <p> so
792792
# HTML is valid before conversion
793793
if in_markup == 'wp-html':
@@ -796,40 +796,38 @@ def fields2pelican(
796796
paragraphs = content.splitlines()
797797
paragraphs = ['<p>{}</p>'.format(p) for p in paragraphs]
798798
new_content = ''.join(paragraphs)
799-
800-
fp.write(new_content)
801-
802-
if pandoc_version < (2,):
803-
parse_raw = '--parse-raw' if not strip_raw else ''
804-
wrap_none = '--wrap=none' \
805-
if pandoc_version >= (1, 16) else '--no-wrap'
806-
cmd = ('pandoc --normalize {0} --from=html'
807-
' --to={1} {2} -o "{3}" "{4}"')
808-
cmd = cmd.format(parse_raw,
809-
out_markup if out_markup != 'markdown' else "gfm",
810-
wrap_none,
811-
out_filename, html_filename)
812-
else:
813-
from_arg = '-f html+raw_html' if not strip_raw else '-f html'
814-
cmd = ('pandoc {0} --to={1}-smart --wrap=none -o "{2}" "{3}"')
815-
cmd = cmd.format(from_arg,
816-
out_markup if out_markup != 'markdown' else "gfm",
817-
out_filename, html_filename)
818-
819-
try:
820-
rc = subprocess.call(cmd, shell=True)
821-
if rc < 0:
822-
error = 'Child was terminated by signal %d' % -rc
823-
exit(error)
824-
825-
elif rc > 0:
826-
error = 'Please, check your Pandoc installation.'
799+
with open(html_filename, 'w', encoding='utf-8') as fp:
800+
fp.write(new_content)
801+
802+
if pandoc_version < (2,):
803+
parse_raw = '--parse-raw' if not strip_raw else ''
804+
wrap_none = '--wrap=none' \
805+
if pandoc_version >= (1, 16) else '--no-wrap'
806+
cmd = ('pandoc --normalize {0} --from=html'
807+
' --to={1} {2} -o "{3}" "{4}"')
808+
cmd = cmd.format(parse_raw,
809+
out_markup if out_markup != 'markdown' else "gfm",
810+
wrap_none,
811+
out_filename, html_filename)
812+
else:
813+
from_arg = '-f html+raw_html' if not strip_raw else '-f html'
814+
cmd = ('pandoc {0} --to={1}-smart --wrap=none -o "{2}" "{3}"')
815+
cmd = cmd.format(from_arg,
816+
out_markup if out_markup != 'markdown' else "gfm",
817+
out_filename, html_filename)
818+
819+
try:
820+
rc = subprocess.call(cmd, shell=True)
821+
if rc < 0:
822+
error = 'Child was terminated by signal %d' % -rc
823+
exit(error)
824+
825+
elif rc > 0:
826+
error = 'Please, check your Pandoc installation.'
827+
exit(error)
828+
except OSError as e:
829+
error = 'Pandoc execution failed: %s' % e
827830
exit(error)
828-
except OSError as e:
829-
error = 'Pandoc execution failed: %s' % e
830-
exit(error)
831-
832-
os.remove(html_filename)
833831

834832
with open(out_filename, encoding='utf-8') as fs:
835833
content = fs.read()

0 commit comments

Comments
 (0)