update example to v0.9.3

This commit is contained in:
relikd
2022-04-06 13:16:44 +02:00
parent a6d9f715f9
commit 637524a615
9 changed files with 78 additions and 52 deletions

View File

@@ -1,31 +1,35 @@
# -*- coding: utf-8 -*-
from lektor.pluginsystem import Plugin
from lektor.utils import slugify
from typing import Generator
import re
from lektor_groupby import GroupBy, GroupByCallbackArgs
class AdvancedGroupByPlugin(Plugin):
def on_groupby_before_build_all(self, groupby, builder, **extra):
def on_groupby_before_build_all(self, groupby: GroupBy, builder, **extra):
# load config
regex = self.get_config().get('match')
config = self.get_config()
regex = config.get('testC.pattern.match')
try:
regex = re.compile(regex)
except Exception as e:
print('inlinetags.regex not valid: ' + str(e))
return
# since we load and use a config file, we need to track the dependency
@groupby.depends_on(self.config_filename)
@groupby.watch('/', 'testC', slug='advanced/{group}/',
template='example-advanced.html')
def convert_replace_example(args):
watcher = groupby.add_watcher('testC', config) # tracks dependency
@watcher.grouping()
def _replace(args: GroupByCallbackArgs) -> Generator[str, str, None]:
# args.field assumed to be Markdown
obj = args.field.source
slugify_map = {} # type Dict[str, str]
for match in regex.finditer(obj):
tag = match.group(1)
yield slugify(tag), tag
key = yield tag
print('[advanced] slugify:', tag, '->', key)
slugify_map[tag] = key
def _fn(match: re.Match) -> str:
tag = match.group(1)
return f'<a href="/advanced/{slugify(tag)}/">{tag}</a>'
return f'<a href="/advanced/{slugify_map[tag]}/">{tag}</a>'
args.field.source = regex.sub(_fn, obj)