Skip to content

Commit

Permalink
fix: correctly escape internal/external links in README
Browse files Browse the repository at this point in the history
  • Loading branch information
omar-besbes committed Oct 12, 2024
1 parent e177609 commit ac9d701
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 16 deletions.
4 changes: 2 additions & 2 deletions scripts/CSES/README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Accepted solutions of [CSES problemset](https://cses.fi/problemset).

{% for category, problems in categories.items() %}

- [{{ category }}](#{{ category | lower | replace(' ', '-') }})
- [{{ category }}](#{{ category | escape_internal_link }})
{% endfor %}

---
Expand All @@ -29,7 +29,7 @@ Accepted solutions of [CSES problemset](https://cses.fi/problemset).
{{ problem.id }}
</td>
<td>
<a href="{{ category | replace(' ', '%20') }}/{{ problem.id }}%20-%20{{ problem.title | replace(' ', '%20') }}.cpp">
<a href="{{ category | escape_external_link }}/{{ problem.id }}%20-%20{{ problem.title | escape_external_link }}.cpp">
💻 {{ problem.title }}
</a>
</td>
Expand Down
31 changes: 26 additions & 5 deletions scripts/CSES/generate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
import logging
import os
import re
import urllib.parse

from jinja2 import Template
from jinja2 import Environment, FileSystemLoader

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')

CSES_SOLUTIONS_DIR = 'solutions/CSES'
TEMPLATE_FILE = 'scripts/CSES/README.template.md'
TEMPLATE_FILE = 'README.template.md'
TEMPLATE_DIR = 'scripts/CSES'

def generate_data_from_folder(path):
data = {}
Expand All @@ -33,11 +36,29 @@ def generate_data_from_folder(path):

return data

def escape_internal_link(text):
# Trim leading/trailing spaces
text = text.strip()
# Convert to lowercase
text = text.lower()
# Remove characters that are not word characters, hyphens, or spaces
text = re.sub(r'[^\w\- ]+', '', text)
# Replace spaces with hyphens
text = re.sub(r'\s', '-', text.strip())
# Remove trailing hyphens
text = re.sub(r'\-+$', '', text)
return text

def escape_external_link(text):
return urllib.parse.quote(text)

if __name__ == '__main__':
# Load the template
logging.info('Loading the template')
with open(TEMPLATE_FILE, 'r') as file:
template = Template(file.read())
env = Environment(loader=FileSystemLoader(TEMPLATE_DIR))
env.filters['escape_internal_link'] = escape_internal_link
env.filters['escape_external_link'] = escape_external_link
template = env.get_template(TEMPLATE_FILE)

# Generate the data from the folder structure
logging.info('Starting content generation')
Expand Down
8 changes: 4 additions & 4 deletions scripts/France-IOI/README.template.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Accepted solutions of [France-IOI problemset](https://www.france-ioi.org/algo/ch
## Table of Contents

{% for level, chapters in levels.items() %}
- [{{ level }}](#{{ level | lower | replace(' ', '-') }})
- [{{ level }}](#{{ level | escape_internal_link }})
{% for chapter, problems in chapters.items() %}
- [{{ chapter }}](#{{ chapter | lower | replace(' ', '-') }})
- [{{ chapter }}](#{{ chapter | escape_internal_link }})
{% endfor %}
{% endfor %}

Expand All @@ -22,15 +22,15 @@ Accepted solutions of [France-IOI problemset](https://www.france-ioi.org/algo/ch
### {{ chapter }}

{% for problem in content.problems %}
1. <a href="{{ level | replace(' ', '%20') }}/{{ chapter | replace(' ', '%20') }}/{{ problem.title | replace(' ', '%20') }}.{{ problem.extension }}"> 💻 {{ problem.title }} </a>
1. <a href="{{ level | escape_external_link }}/{{ chapter | escape_external_link }}/{{ problem.title | escape_external_link }}.{{ problem.extension }}"> 💻 {{ problem.title }} </a>
{% endfor %}

{% for part in content.parts %}

1. **{{ part.part }}**

{% for subproblem in part.problems %}
1. <a href="{{ level | replace(' ', '%20') }}/{{ chapter | replace(' ', '%20') }}/{{ part.part | replace(' ', '%20') }}/{{ subproblem.title | replace(' ', '%20') }}.{{ subproblem.extension }}"> 💻 {{ subproblem.title }} </a>
1. <a href="{{ level | escape_external_link }}/{{ chapter | escape_external_link }}/{{ part.part | escape_external_link }}/{{ subproblem.title | escape_external_link }}.{{ subproblem.extension }}"> 💻 {{ subproblem.title }} </a>
{% endfor %}

{% endfor %}
Expand Down
31 changes: 26 additions & 5 deletions scripts/France-IOI/generate.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import os
import logging
import os
import re
import urllib.parse

from jinja2 import Template
from jinja2 import Environment, FileSystemLoader

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'
TEMPLATE_FILE = 'README.template.md'
TEMPLATE_DIR = 'scripts/France-IOI'

def generate_data_from_folder(path):
data = {}
Expand Down Expand Up @@ -59,11 +62,29 @@ def generate_data_from_folder(path):

return data

def escape_internal_link(text):
# Trim leading/trailing spaces
text = text.strip()
# Convert to lowercase
text = text.lower()
# Remove characters that are not word characters, hyphens, or spaces
text = re.sub(r'[^\w\- ]+', '', text)
# Replace spaces with hyphens
text = re.sub(r'\s', '-', text.strip())
# Remove trailing hyphens
text = re.sub(r'\-+$', '', text)
return text

def escape_external_link(text):
return urllib.parse.quote(text)

if __name__ == '__main__':
# Load the template
logging.info('Loading the template')
with open(TEMPLATE_FILE, 'r') as file:
template = Template(file.read())
env = Environment(loader=FileSystemLoader(TEMPLATE_DIR))
env.filters['escape_internal_link'] = escape_internal_link
env.filters['escape_external_link'] = escape_external_link
template = env.get_template(TEMPLATE_FILE)

# Generate the data from the folder structure
logging.info('Starting content generation')
Expand Down

0 comments on commit ac9d701

Please sign in to comment.