diff --git a/tetras_extraction/macao_12/script/src/export.py b/tetras_extraction/macao_12/script/src/export.py index 94f024fdbc87ed744192b253014b9d4e3601329a..f876e40dd0c2831fb69e39ef3a24d1fb51f461b7 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__":