From 6c0c435fac13274c31ef37bbb82f8ce0eca677ab Mon Sep 17 00:00:00 2001 From: eliott <eliott.sammier@tetras-libre.fr> Date: Fri, 28 Jun 2024 18:16:34 +0200 Subject: [PATCH] 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 --- .../macao_12/script/src/export.py | 52 +++++-------------- 1 file changed, 14 insertions(+), 38 deletions(-) diff --git a/tetras_extraction/macao_12/script/src/export.py b/tetras_extraction/macao_12/script/src/export.py index 94f024fd..f876e40d 100644 --- a/tetras_extraction/macao_12/script/src/export.py +++ b/tetras_extraction/macao_12/script/src/export.py @@ -9,29 +9,17 @@ from common import * 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 path = path.relative_to(path.root) path = root / path - print(f"{path}: mkdir") - path.mkdir(parents=True, exist_ok=True) - # Create _index.md files for parent sections - section = path.parent - while section != root: - (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: + # Create parent dir if needed + path.parent.mkdir(parents=True, exist_ok=True) + # Create and write file + print(f"{path} ->\n{body}") + with open(path, "w") as f: 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: """Simple safeguard before `rm -rf`ing a directory, that checks if the path @@ -62,30 +50,18 @@ def main(): # ==> Apply STTL transformations template_output = export_corese.apply_templates() - # ==> Split the result - regex = re.compile(r"^####### *?([\/\w]+)\n", re.MULTILINE) + # ==> Split the result on the start-of-file markers + regex = re.compile(r"^####### *?(\S+)\n", re.MULTILINE) parts = regex.split(template_output) # 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 - # non-delimiter, and so on. + # empty), followed by each capturing group result of the delimiter, then + # the second non-delimiter, and so on. # With a pattern that has 1 group, its results are on odd-numbered indices. for i in range(1, len(parts), 2): - activity_path = parts[i] - activity_content = parts[i + 1] - # Split content again, to separate Markdown and optional JSON - content_parts = activity_content.split("%%%%%%%\n") - 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.") + file_path = parts[i] + file_content = parts[i + 1] + create_file(root, Path(file_path), file_content) + print(f"Found {len(parts)} parts => {(len(parts)-1)/2} files.") if __name__ == "__main__": -- GitLab