27 lines
1.0 KiB
Python
27 lines
1.0 KiB
Python
import yaml
|
|
import os
|
|
|
|
# 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:
|
|
try:
|
|
return yaml.safe_load(stream)
|
|
except yaml.YAMLError as exc:
|
|
print(exc)
|
|
|
|
# Retrieve the available languages as stated in the meta.yml (key: language).
|
|
def get_available_languages(root):
|
|
return read_yaml(root, 'meta.yml')['language']
|
|
|
|
# Replaces the placeholders in the given content with corresponding values in
|
|
# the given language from the meta.yml and returns the filled content.
|
|
def insert_metadata(content, root, lang):
|
|
metadata = read_yaml(root, 'meta.yml')
|
|
for key, value in metadata.items():
|
|
placeholder = '@' + key
|
|
filler = value[lang]
|
|
# print('replace', placeholder, 'with', filler)
|
|
if '@' + key in content:
|
|
content = content.replace(placeholder, filler)
|
|
return content |