Makefile, AppCache and numerous other changes

This commit is contained in:
relikd
2019-11-25 18:09:40 +01:00
parent 57351a5e12
commit 40d483bc38
98 changed files with 1310 additions and 306 deletions

View File

@@ -1,9 +1,9 @@
# -*- coding: utf-8 -*-
from lektor.pluginsystem import Plugin
from lektor.pluginsystem import Plugin, get_plugin
from lektor.databags import Databags
from markupsafe import Markup
from datetime import datetime
import unicodedata
import os
import shutil
# -------
# Sorting
@@ -39,20 +39,17 @@ def noUmlaut(text):
return str(text)
def pluralize(n, single, multi):
if n == 0:
return ''
return u'{} {}'.format(n, single if n == 1 else multi)
def replaceFractions(txt):
res = ''
for x in txt.split():
try:
i = ['1/2', '1/3', '2/3', '1/4', '3/4', '1/8', '-'].index(x)
res += [u'½', u'', u'', u'¼', u'¾', u'', u' - '][i]
i = ['1/2', '1/3', '2/3', '1/4', '3/4', '1/8'].index(x)
res += [u'½', u'', u'', u'¼', u'¾', u''][i]
except ValueError:
res += ' ' + x
if x in u'-–—':
res += u' - '
else:
res += ' ' + x
return res.lstrip()
@@ -77,24 +74,6 @@ def updateSet_if(dic, parent, parentkey, value):
dic[key] = set()
dic[key].add(value)
def updateSet_addMultiple(dic, key, others):
try:
dic[key]
except KeyError:
dic[key] = set()
dic[key].update(others)
def findCluster(key, clusterList=[30, 60, 120]):
key = int(key) if key else 0
if key > 0:
for cluster in clusterList:
if key < cluster:
key = cluster
break
return key
# --------------------
# Ingredient splitting
@@ -105,13 +84,14 @@ def splitIngredientLine(line):
indices = [0, len(line)]
for i, char in enumerate(line):
if char.isspace():
capture = False
indices[state] = i
state += 1
if capture:
capture = False
indices[state] = i
state += 1
continue
elif capture:
continue
elif state == 1 and char in '0123456789-.,':
elif state == 1 and char in u'0123456789-–—.,':
state -= 1
elif state > 1:
break
@@ -127,6 +107,9 @@ def parseIngredientLine(line, measureList=[], rep_frac=False):
measure = line[idx[0]:idx[1]].lstrip()
if measure.lower() in measureList:
name = line[idx[1]:].lstrip()
# if name.startswith('of '):
# measure += ' of'
# name = name[3:]
else:
measure = ''
name = line[idx[0]:].lstrip()
@@ -136,19 +119,18 @@ def parseIngredientLine(line, measureList=[], rep_frac=False):
name, note = [x.strip() for x in name_note]
return {'value': val, 'measure': measure, 'name': name, 'note': note}
# --------------------
# Other Helper methods
def groupByMergeCluster(dic, arr=[30, 60, 120], reverse=False):
arr = sorted([int(x) for x in arr])
groups = dict()
for key, recipes in dic:
key = findCluster(key, arr)
if key == 0 and not reverse:
key = ''
updateSet_addMultiple(groups, key, recipes)
return sorted(groups.items(), reverse=bool(reverse))
def replace_atref_urls(text, label=None):
if '@' not in text:
return text
result = list()
for x in text.split():
if x[0] == '@':
x = x[1:]
result.append(u'<a href="{}">{}</a>'.format(x, label or x))
else:
result.append(x)
return Markup(' '.join(result))
# ----------------
# Main entry point
@@ -157,68 +139,56 @@ def groupByMergeCluster(dic, arr=[30, 60, 120], reverse=False):
class HelperPlugin(Plugin):
name = u'Helper'
description = u'Some helper methods, filters, and templates.'
alt = None
availableTags = set()
buildTime = None
settings = dict()
translations = dict()
# -----------
# Event hooks
# -----------
def on_before_build_all(self, builder, **extra):
# display only tags that contain at least one recipe
def processCLI(self, extra_flags):
useCache = bool(extra_flags.get('ENABLE_APPCACHE'))
plugin = get_plugin('force-update', self.env)
if plugin.enabled and not useCache:
plugin.enabled = False
print('AppCache: ' + ('ENABLED' if useCache else 'DISABLED'))
self.env.jinja_env.globals['ENABLE_APPCACHE'] = useCache
def processSettings(self):
bag = Databags(self.env)
pad = self.env.new_pad()
for r in pad.query('recipes'):
self.availableTags.update(r['tags'])
for alt in self.env.load_config().iter_alternatives():
set = pad.get('settings', alt=alt)
self.translations[alt] = bag.lookup('i18n+' + alt)
self.settings[alt] = {
'measures': set['measures'].lower().split(),
'replFrac': set['replace_frac']
}
def on_after_prune(self, builder, **extra):
# redirect to /en/
for file in ['index.html']:
src_f = os.path.join(self.env.root_path, 'root', file)
if os.path.exists(src_f):
dst_f = os.path.join(builder.destination_path, file)
with open(dst_f, 'wb') as df:
with open(src_f, 'rb') as sf:
shutil.copyfileobj(sf, df)
def on_before_build_all(self, builder, **extra):
build_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print('Build time: ' + build_time)
self.env.jinja_env.globals['DATE_NOW'] = build_time
# update project settings once per build
self.processCLI(getattr(builder, 'extra_flags'))
self.processSettings()
def on_process_template_context(self, context, **extra):
self.alt = context['alt']
# def on_process_template_context(self, context, **extra):
# pass
def on_setup_env(self, **extra):
# self.env.load_config().iter_alternatives()
# pad = self.env.new_pad()
# pad.query('groupby', alt=alt)
def localizeDic(alt, partA, partB=None):
if alt not in self.translations:
raise RuntimeError(
'localize() expects first parameter to be an alternate')
if partB is None:
partA, partB = partA.split('.', 1)
return self.translations[alt][partA][partB]
def localizeDic(key, subkey=None):
bag = Databags(self.env).lookup('i18n+{}.{}'.format(self.alt, key))
return bag[subkey] if subkey else bag
def to_duration(time, cluster=None):
time = int(time) if time else 0
if (time <= 0):
return ''
# Calls itself without cluster argument
if cluster:
cluster = [int(x) for x in cluster]
idx = cluster.index(time)
if idx == 0:
return '<' + to_duration(time)
timeA = to_duration(cluster[idx - 1])
if idx + 1 >= len(cluster):
return '>' + timeA
else:
return u'{} {}'.format(timeA, to_duration(time))
days = time // (60 * 24)
time -= days * (60 * 24)
L = localizeDic('duration')
return ' '.join([
pluralize(days, L['day'], L['days']),
pluralize(time // 60, L['hour'], L['hours']),
pluralize(time % 60, L['min'], L['mins'])]).strip()
def ingredientsForRecipe(recipe):
set = self.env.new_pad().get('settings', alt=self.alt)
meaList = [x.strip() for x in set['measures'].lower().split(',')]
repFrac = set['replace_frac']
def ingredientsForRecipe(recipe, alt='en'):
meaList = self.settings[alt]['measures']
repFrac = self.settings[alt]['replFrac']
for line in recipe['ingredients']:
line = line.strip()
@@ -229,23 +199,21 @@ class HelperPlugin(Plugin):
else:
yield parseIngredientLine(line, meaList, repFrac)
def groupByAttribute(recipeList, attribute):
def groupByAttribute(recipeList, attribute, alt='en'):
groups = dict()
for recipe in recipeList:
if attribute == 'ingredients':
for ing in ingredientsForRecipe(recipe):
for ing in ingredientsForRecipe(recipe, alt):
updateSet_if(groups, ing, 'name', recipe)
else:
updateSet_if(groups, recipe, attribute, recipe)
# groups[undefinedKey].update(groups.pop('_undefined'))
return groups.items()
self.env.jinja_env.filters['duration'] = to_duration
self.env.jinja_env.filters['rating'] = numFillWithText
self.env.jinja_env.filters['replaceFractions'] = replaceFractions
self.env.jinja_env.filters['enumIngredients'] = ingredientsForRecipe
self.env.jinja_env.filters['replaceAtRefURLs'] = replace_atref_urls
self.env.jinja_env.filters['groupByAttribute'] = groupByAttribute
self.env.jinja_env.filters['groupSort'] = groupByDictSort
self.env.jinja_env.filters['groupMergeCluster'] = groupByMergeCluster
self.env.jinja_env.globals['localize'] = localizeDic
self.env.jinja_env.globals['availableTags'] = self.availableTags