Skip to content

Commit

Permalink
ci: automate README generation for France-IOI solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-besbes committed Oct 7, 2024
1 parent f799c7b commit 59cc2cc
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/generate-docs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

strategy:
matrix:
platform: [CSES]
platform: [CSES, France-IOI]

steps:
- name: Checkout repository
Expand Down
29 changes: 29 additions & 0 deletions scripts/France-IOI/README.template.md
Original file line number Diff line number Diff line change
@@ -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. <a href="{{ level | replace(' ', '%20') }}/{{ chapter | replace(' ', '%20') }}/{{ problem.title | replace(' ', '%20') }}.cpp"> 💻 {{ problem.title }} </a>
{% endfor %}

{% endfor %}
{% endfor %}
70 changes: 70 additions & 0 deletions scripts/France-IOI/generate.py
Original file line number Diff line number Diff line change
@@ -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')

0 comments on commit 59cc2cc

Please sign in to comment.