import datetime import json import re import shutil with open('web/templates/review.html', 'r') as f: REVIEW_TEMPLATE = f.read() def review_id(review): return "_".join(re.sub('\s+', '-', x.lower() if x else '') for x in (review['company'], review['name'], review['variant'])) def review_title(review): return '{} "{}" {}'.format( review['company'], review['name'], '({})'.format(review['variant']) if review['variant'] else '' ) def generate_website(website_source: str, json_source: str, dest: str): with open(website_source, 'r') as f: website_content = f.read() with open(json_source, 'r') as f: data = json.load(f) website = populate_template_str(website_content, { 'index': generate_index(data['reviews']), 'pesto_ratings': '\n\n'.join(generate_review_html(review) for review in data['reviews']), 'current_year': str(datetime.date.today().year) }) with open(dest, 'w') as f: f.write(website) f.write(''.format(datetime.datetime.now())) def generate_review_html(review: dict) -> str: return populate_template_str(REVIEW_TEMPLATE, { 'review_id': review_id(review) , 'title': review_title(review) , 'date': review['date'] , 'notes': review['notes'] , 'ingredients': ', '.join(review['ingredients']) , 'rating_taste': review['rating_value']['taste'] , 'rating_consistency': review['rating_value']['consistency'] , 'rating_ingredients': review['rating_value']['ingredients'] , 'rating_price': review['rating_value']['price'] , 'rating_size': review['rating_value']['size'] , 'rating': review['final_verdict']['string'] }) def generate_index(reviews): return "