Skip to content
Snippets Groups Projects
Commit 6c0c435f authored by Eliott Sammier's avatar Eliott Sammier
Browse files

Simplify STTL output parser to just create files

One marker type only to indicate the file path, followed by the body, no special treatment for JSON files, no extra mkdir, one file per marker
parent a309a7b2
No related branches found
No related tags found
No related merge requests found
...@@ -9,29 +9,17 @@ from common import * ...@@ -9,29 +9,17 @@ from common import *
log = get_logger("export") log = get_logger("export")
def create_activity(root: Path, path: Path, body: str, quiz: str | None): def create_file(root: Path, path: Path, body: str):
# Remove root of activity path if present, we need a relative path # Remove root of activity path if present, we need a relative path
path = path.relative_to(path.root) path = path.relative_to(path.root)
path = root / path path = root / path
print(f"{path}: mkdir") # Create parent dir if needed
path.mkdir(parents=True, exist_ok=True) path.parent.mkdir(parents=True, exist_ok=True)
# Create _index.md files for parent sections # Create and write file
section = path.parent print(f"{path} ->\n{body}")
while section != root: with open(path, "w") as f:
(section / "_index.md").touch(exist_ok=True)
section = section.parent
body_file = path / "index.md"
print(f"{body_file}\n{body}")
with open(body_file, "w") as f:
f.write(body) f.write(body)
if quiz is not None:
quiz_file = path / "quiz.json"
print(f"{quiz_file}\n{quiz}")
with open(quiz_file, "w") as f:
f.write(quiz)
def safe_to_remove(path: Path) -> bool: def safe_to_remove(path: Path) -> bool:
"""Simple safeguard before `rm -rf`ing a directory, that checks if the path """Simple safeguard before `rm -rf`ing a directory, that checks if the path
...@@ -62,30 +50,18 @@ def main(): ...@@ -62,30 +50,18 @@ def main():
# ==> Apply STTL transformations # ==> Apply STTL transformations
template_output = export_corese.apply_templates() template_output = export_corese.apply_templates()
# ==> Split the result # ==> Split the result on the start-of-file markers
regex = re.compile(r"^####### *?([\/\w]+)\n", re.MULTILINE) regex = re.compile(r"^####### *?(\S+)\n", re.MULTILINE)
parts = regex.split(template_output) parts = regex.split(template_output)
# The parts list starts with the first non-delimiter string (which may be # The parts list starts with the first non-delimiter string (which may be
# empty), then each capturing group result of the delimiter, then the second # empty), followed by each capturing group result of the delimiter, then
# non-delimiter, and so on. # the second non-delimiter, and so on.
# With a pattern that has 1 group, its results are on odd-numbered indices. # With a pattern that has 1 group, its results are on odd-numbered indices.
for i in range(1, len(parts), 2): for i in range(1, len(parts), 2):
activity_path = parts[i] file_path = parts[i]
activity_content = parts[i + 1] file_content = parts[i + 1]
# Split content again, to separate Markdown and optional JSON create_file(root, Path(file_path), file_content)
content_parts = activity_content.split("%%%%%%%\n") print(f"Found {len(parts)} parts => {(len(parts)-1)/2} files.")
markdown = content_parts[0]
if len(content_parts) > 1 and content_parts[1] != "":
json = content_parts[1]
else:
json = None
print(
"{}\n\t-> index.md = '{}'\n\tquiz.json = '{}'".format(
activity_path, markdown, json
)
)
create_activity(root, Path(activity_path), markdown, json)
print(f"Found {len(parts)} parts => {(len(parts)-1)/2} activities.")
if __name__ == "__main__": if __name__ == "__main__":
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment