Skip to content

Commit

Permalink
Update generate_site_scaffolds.py
Browse files Browse the repository at this point in the history
  • Loading branch information
benrochford authored Jan 14, 2025
1 parent 1412110 commit 1b72e74
Showing 1 changed file with 96 additions and 5 deletions.
101 changes: 96 additions & 5 deletions .github/scripts/generate_site_scaffolds.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,104 @@ def validate_csv(df):
debug_print(f"Number of rows: {len(df)}")

def clean_path_name(name):
# ... (previous clean_path_name code) ...
debug_print(f"Cleaned path name: {name} -> {path_name}")
return path_name
accent_map = {
'ü': 'u', 'ä': 'a', 'ö': 'o',
'é': 'e', 'è': 'e', 'ê': 'e', 'ë': 'e',
'á': 'a', 'à': 'a', 'â': 'a', 'ã': 'a',
'í': 'i', 'ì': 'i', 'î': 'i', 'ï': 'i',
'ó': 'o', 'ò': 'o', 'ô': 'o', 'õ': 'o',
'ú': 'u', 'ù': 'u', 'û': 'u',
'ý': 'y', 'ÿ': 'y',
'ñ': 'n', 'ç': 'c'
}

path_name = str(name).lower()
for accent, replacement in accent_map.items():
path_name = path_name.replace(accent, replacement)
return re.sub(r'[^a-z0-9]', '-', path_name)

def find_most_recent_image(root_dir, current_year, location_name):
debug_print(f"Searching for previous image for {location_name} before year {current_year}")
# ... (previous find_most_recent_image code) ...
"""Find the most recent header image for a given location from previous years."""
current_year = int(current_year)

for year in range(current_year - 1, 2016, -1):
data_image = root_dir / '_data' / str(year) / location_name / 'location.yml'
site_image = root_dir / str(year) / location_name / 'index.md'

for check_path in [data_image, site_image]:
if check_path.exists():
try:
with open(check_path, 'r', encoding='utf-8') as f:
content = f.read()
image_match = re.search(r'^image:\s*(.+)$', content, re.MULTILINE)
if image_match and 'tbd.jpg' not in image_match.group(1):
return image_match.group(1).strip()
except Exception as e:
print(f"Warning: Error reading {check_path}: {e}")

return '/assets/images/tbd.jpg'

def replace_tags_in_file(file_path, replacements):
try:
root_dir = Path.cwd()

with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()

path_name = clean_path_name(replacements['NAME'])
previous_image = find_most_recent_image(root_dir, replacements['YEAR'], path_name)

is_yaml = file_path.suffix == '.yml'

if is_yaml:
if file_path.name == 'location.yml':
content = re.sub(r'^image:\s*.*$', f'image: {previous_image}',
content, flags=re.MULTILINE)

if file_path.name == 'hero.yml':
content = re.sub(r'^location:\s*.*$', f'location: SICSS-{replacements["NAME"]}',
content, flags=re.MULTILINE)

for tag, value in replacements.items():
if tag != 'NAME':
pattern = f'\\[\\[{tag}\\]\\]'
content = re.sub(pattern, str(value), content, flags=re.IGNORECASE)
else:
content = re.sub(r'^link:\s*.*$', f'link: {path_name}', content, flags=re.MULTILINE)

for tag, value in replacements.items():
pattern = f'\\[\\[{tag}\\]\\]'

if tag == 'NAME':
content = re.sub(r'^title:\s*.*$', f'title: {value}', content, flags=re.MULTILINE)
content = re.sub(pattern, path_name, content, flags=re.IGNORECASE)
else:
content = re.sub(pattern, str(value), content, flags=re.IGNORECASE)
else:
if file_path.name == 'index.md':
content = re.sub(r'^image:\s*.*$', f'image: {previous_image}',
content, flags=re.MULTILINE)

yaml_pattern = r'^---\n.*?---'
def yaml_replacer(match):
yaml_content = match.group(0)
yaml_content = re.sub(r'^title:\s*.*$', f'title: {replacements["NAME"]}',
yaml_content, flags=re.MULTILINE)
yaml_content = re.sub(r'^partner_site:\s*.*$', f'partner_site: {path_name}',
yaml_content, flags=re.MULTILINE)
yaml_content = re.sub(r'\[\[NAME\]\]', path_name, yaml_content, flags=re.IGNORECASE)
return yaml_content

content = re.sub(yaml_pattern, yaml_replacer, content, flags=re.DOTALL)

for tag, value in replacements.items():
pattern = f'\\[\\[{tag}\\]\\]'
content = re.sub(pattern, str(value), content, flags=re.IGNORECASE)

with open(file_path, 'w', encoding='utf-8') as f:
f.write(content)
except Exception as e:
print(f"Warning: Error processing {file_path}: {e}")

def generate_scaffolds(csv_path):
try:
Expand Down

0 comments on commit 1b72e74

Please sign in to comment.