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.

93 lines
3.2 KiB
Python

2 years ago
import datetime
import json
import re
import shutil
import hashlib
2 years ago
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 get_json():
with open("reviews.json", 'r') as f:
return json.load(f)
def generate_website(template_dir: str, dest: str):
with open(template_dir + 'index.html', 'r') as f:
2 years ago
website_content = f.read()
with open(template_dir + 'table.html', 'r') as f:
table_template = f.read()
data = get_json()
2 years ago
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),
'css_hash': stylesheet_hash(),
2 years ago
})
with open(dest + 'index.html', 'w') as f:
2 years ago
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_servings': review['rating_value']['servings']
, 'rating': review['final_verdict']['string']
, 'image_items': generate_image_items(review)
, 'categories': " ".join('<span class="category" x-category="{0}">{0}</span>'.format(cat[0].upper() + cat[1:]) for cat in review['category'])
2 years ago
})
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)
def generate_image_items(review: dict) -> str:
return '\n'.join(
'<div class="image-item"><img src="img/{}" /></div>'.format(img)
for img in review.get('images', [])
)
def stylesheet_hash():
hash = hashlib.sha256()
with open("web/style.css", 'rb') as f:
while True:
chunk = f.read(hash.block_size)
if not chunk:
break
hash.update(chunk)
return hash.hexdigest()
2 years ago
if __name__ == '__main__':
generate_website('web/templates/', 'web/')
2 years ago
shutil.copy('reviews.json', 'web/reviews.json')