use a tempfile for intermediate html file for pandoc in importer

This commit is contained in:
Deniz Turgut 2023-10-28 16:31:05 +03:00
commit 11c13ceae1
No known key found for this signature in database
GPG key ID: 87B7168D7AB3ED2F

View file

@ -7,6 +7,7 @@ import os
import re
import subprocess
import sys
import tempfile
import time
from collections import defaultdict
from html import unescape
@ -785,9 +786,8 @@ def fields2pelican(
print(out_filename)
if in_markup in ('html', 'wp-html'):
html_filename = os.path.join(output_path, filename + '.html')
with open(html_filename, 'w', encoding='utf-8') as fp:
with tempfile.TemporaryDirectory() as tmpdir:
html_filename = os.path.join(tmpdir, 'pandoc-input.html')
# Replace newlines with paragraphs wrapped with <p> so
# HTML is valid before conversion
if in_markup == 'wp-html':
@ -796,7 +796,7 @@ def fields2pelican(
paragraphs = content.splitlines()
paragraphs = ['<p>{}</p>'.format(p) for p in paragraphs]
new_content = ''.join(paragraphs)
with open(html_filename, 'w', encoding='utf-8') as fp:
fp.write(new_content)
if pandoc_version < (2,):
@ -829,8 +829,6 @@ def fields2pelican(
error = 'Pandoc execution failed: %s' % e
exit(error)
os.remove(html_filename)
with open(out_filename, encoding='utf-8') as fs:
content = fs.read()
if out_markup == 'markdown':