You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.3 KiB
Python

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('<!-- auto generated on the {} -->'.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 "<ul>{}</ul>".format(
"\n".join('<li><a href="#{}">{}</a></li>'.format(
review_id(review), review_title(review)
) for review in reviews)
)
def populate_template_str(templatestr, fields: dict[str, str]):
def fill(match):
return fields.get(match.group(1).lower(), 'Unknown field {}'.format(match.group(1)))
return re.sub(r'{([A-Z_]+)}', fill, templatestr)
if __name__ == '__main__':
generate_website('web/templates/index.html', 'reviews.json', 'web/index.html')
shutil.copy('reviews.json', 'web/reviews.json')