From 59cc2cce9697ceb7c60082922673d3bd3e607cd7 Mon Sep 17 00:00:00 2001 From: Omar Besbes <86571415+omar-besbes@users.noreply.github.com> Date: Mon, 7 Oct 2024 20:30:14 +0100 Subject: [PATCH] ci: automate README generation for France-IOI solutions --- .github/workflows/generate-docs.yaml | 2 +- scripts/France-IOI/README.template.md | 29 +++++++++++ scripts/France-IOI/generate.py | 70 +++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 scripts/France-IOI/README.template.md create mode 100644 scripts/France-IOI/generate.py diff --git a/.github/workflows/generate-docs.yaml b/.github/workflows/generate-docs.yaml index e850374..0e70b4e 100644 --- a/.github/workflows/generate-docs.yaml +++ b/.github/workflows/generate-docs.yaml @@ -11,7 +11,7 @@ jobs: strategy: matrix: - platform: [CSES] + platform: [CSES, France-IOI] steps: - name: Checkout repository diff --git a/scripts/France-IOI/README.template.md b/scripts/France-IOI/README.template.md new file mode 100644 index 0000000..528e3f7 --- /dev/null +++ b/scripts/France-IOI/README.template.md @@ -0,0 +1,29 @@ +# France-IOI Solutions + +Accepted solutions of [France-IOI problemset](https://www.france-ioi.org/algo/chapters.php). + +## Table of Contents + +{% for level, chapters in levels.items() %} +- [{{ level }}](#{{ level | lower | replace(' ', '-') }}) +{% for chapter, problems in chapters.items() %} + - [{{ chapter }}](#{{ chapter | lower | replace(' ', '-') }}) +{% endfor %} +{% endfor %} + +--- + +{% for level, chapters in levels.items() %} + +## {{ level }} + +{% for chapter, problems in chapters.items() %} + +### {{ chapter }} + +{% for problem in problems %} +1. 💻 {{ problem.title }} +{% endfor %} + +{% endfor %} +{% endfor %} diff --git a/scripts/France-IOI/generate.py b/scripts/France-IOI/generate.py new file mode 100644 index 0000000..c3dcec1 --- /dev/null +++ b/scripts/France-IOI/generate.py @@ -0,0 +1,70 @@ +import os +import logging + +from jinja2 import Template + +logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') + +FRANCE_IOI_SOLUTIONS_DIR = 'solutions/France-IOI' +TEMPLATE_FILE = 'scripts/France-IOI/README.template.md' + +def generate_data_from_folder(path): + data = {} + + # Verify solutions directory exists + if not os.path.exists(path): + logging.error(f'Solutions directory "{path}" does not exist.') + return + + # Walk through level directories + for level_dir in sorted(os.listdir(path)): + level_path = os.path.join(path, level_dir) + if not os.path.isdir(level_path): + continue + + level_data = {} + + # Walk through chapter directories within each level + for chapter_dir in sorted(os.listdir(level_path)): + chapter_path = os.path.join(level_path, chapter_dir) + if not os.path.isdir(chapter_path): + continue + + problems = [] + + # Iterate over files and assign unique problem IDs based on index + for idx, file in enumerate(sorted(os.listdir(chapter_path))): + if file.endswith('.cpp'): + problem_title = file.rsplit('.', 1)[0] # Extract problem title + problem_id = idx + 1 # Assign problem ID as index + problems.append({"id": problem_id, "title": problem_title}) + + if problems: + level_data[chapter_dir] = problems + + if level_data: + data[level_dir] = level_data + + return data + +if __name__ == '__main__': + # Load the template + logging.info('Loading the template') + with open(TEMPLATE_FILE, 'r') as file: + template = Template(file.read()) + + # Generate the data from the folder structure + logging.info('Starting content generation') + data = generate_data_from_folder(FRANCE_IOI_SOLUTIONS_DIR) + + # Render the README with data + logging.info('Rendering the README') + output = template.render(levels=data) + + # Save the generated README + output_file = f'{FRANCE_IOI_SOLUTIONS_DIR}/README.md' + logging.info(f'Writing the generated content to {output_file}') + with open(output_file, 'w') as file: + file.write(output) + + logging.info(f'Readme generation completed')