Fix encoding issues

This commit is contained in:
Knoch 2023-01-10 13:57:55 +01:00
parent e23d66716d
commit f0689888d4
5 changed files with 11 additions and 11 deletions

View File

@ -47,12 +47,12 @@ def not_cached_before(root, lang, file):
# Restore the cached chapter.
def get_cached_chapter(root, lang, chapter):
chapter_path = os.path.join(root, '.onpoint', lang, chapter + '.cache')
return open(chapter_path, 'r').read()
return open(chapter_path, 'r', encoding='utf8').read()
# Store a cached version of a chapter.
def store_cached_chapter(root, lang, chapter, content):
chapter_path = os.path.join(root, '.onpoint', lang, chapter + '.cache')
chapter_file = open(chapter_path, 'w')
chapter_file = open(chapter_path, 'w', encoding='utf-8')
chapter_file.write(content)
chapter_file.close()
@ -66,12 +66,12 @@ def __init_cache_directory(root, lang):
# Save the current time to the timestamp file.
def save_compile_timestamp(root, lang, file_name=timestamp_file_name):
timestamp_file_path = os.path.join(root, '.onpoint', lang, file_name)
timestamp_file = open(timestamp_file_path, 'w')
timestamp_file = open(timestamp_file_path, 'w', encoding='utf-8')
timestamp_file.write(str(int(time.time())))
timestamp_file.close()
# Retrieve the compile timestamp.
def __get_last_compile_time(root, lang):
timestamp_file_path = os.path.join(root, '.onpoint', lang, timestamp_file_name)
with open(timestamp_file_path) as f:
with open(timestamp_file_path, encoding='utf8') as f:
return int(f.readline())

View File

@ -21,7 +21,7 @@ def compile_chapters(root_directory, language, force_recompile=False):
chapters = ""
for chapter in structure:
chapters += '<section>\n' + compile_chapter(chapter, no_cache) + '\n</section>'
chapters += '<section>' + os.linesep + compile_chapter(chapter, no_cache) + os.linesep + '</section>'
cache.save_compile_timestamp(root, lang)
return chapters # string
@ -47,7 +47,7 @@ def compile_chapter(chapter, no_cache):
return cache.get_cached_chapter(root, lang, chapter)
print("Recompiling chapter " + chapter + " (" + lang + ").")
chapter_file = open(chapter_path, 'r')
chapter_file = open(chapter_path, 'r', encoding='utf-8')
delimiter = '\n@slide'
chapter_input = ('\n' + chapter_file.read()).split(delimiter)
slides = [delimiter + slide for slide in chapter_input][1:]

View File

@ -6,7 +6,7 @@ import re
# Calls a YAML parser for the given relative path inside the presentation root
# directory. Returns a dictionary with the YAML content.
def read_yaml(root, path):
with open(os.path.join(root, path), 'r') as stream:
with open(os.path.join(root, path), 'r', encoding='utf8') as stream:
try:
return yaml.safe_load(stream)
except yaml.YAMLError as exc:
@ -47,7 +47,7 @@ def add_lazyload(source, lazyload_images):
# per presentation.
def add_help_menu(source, root):
help_menu_code = open(os.path.join(
root, 'onpoint/help-menu.html'), "r").read()
root, 'onpoint/help-menu.html'), 'r', encoding='utf8').read()
help_menu_code = re.sub('@imprint', read_yaml(root, 'legal.yml')['imprint'], help_menu_code)
help_menu_code = re.sub('@privacy-policy', read_yaml(root, 'legal.yml')['privacy-policy'], help_menu_code)
return re.sub('@help', help_menu_code, source)

View File

@ -13,14 +13,14 @@ import helper
def compile(root, language='en', force_recompile=False, lazyload_images=False):
wrapper = open(os.path.join(root, 'layouts/root.html'), 'r').read()
wrapper = open(os.path.join(root, 'layouts/root.html'), 'r', encoding='utf8').read()
compiled_chapters = chapters.compile_chapters(root, language, force_recompile)
wrapper = wrapper.replace('@slides', compiled_chapters)
wrapper = helper.insert_metadata(wrapper, root, language)
wrapper = fragments.defragmentize(wrapper)
wrapper = helper.add_lazyload(wrapper, lazyload_images)
wrapper = helper.add_help_menu(wrapper, root)
with open(os.path.join(root, 'slides.' + language + '.html'), 'w+') as output:
with open(os.path.join(root, 'slides.' + language + '.html'), 'w+', encoding='utf8') as output:
output.write(wrapper)
print('done')

View File

@ -53,7 +53,7 @@ def get_slide_data(slide):
def get_slide_layout(layout_name):
global root
try:
return open(os.path.join(root, 'layouts/' + layout_name + '.html'), 'r').read()
return open(os.path.join(root, 'layouts/' + layout_name + '.html'), 'r', encoding='utf8').read()
except FileNotFoundError:
raise Exception('Layout ' + layout_name + ' not found!')