diff --git a/examples/Examples.lektorproject b/examples/Examples.lektorproject index 9e6763f..b4a7af8 100644 --- a/examples/Examples.lektorproject +++ b/examples/Examples.lektorproject @@ -2,4 +2,4 @@ name = GroupBy Examples [packages] -lektor-groupby = 0.9.1 +lektor-groupby = 0.9.3 diff --git a/examples/configs/advanced.ini b/examples/configs/advanced.ini index 041a182..f2dd274 100644 --- a/examples/configs/advanced.ini +++ b/examples/configs/advanced.ini @@ -1 +1,15 @@ -match = {{([^}]{1,32})}} \ No newline at end of file +[testC] +root = / +slug = advanced/{key}/ +template = example-advanced.html + +[testC.pattern] +match = {{([^}]{1,32})}} + +[testC.fields] +desc = "Tag: " ~ this.group ~ ", Key: " ~ this.key + +[testC.key_map] +Blog = case-sensitive +Two = three +three = no-nested-replace diff --git a/examples/configs/groupby.ini b/examples/configs/groupby.ini index bb447ac..f7262d4 100644 --- a/examples/configs/groupby.ini +++ b/examples/configs/groupby.ini @@ -1,5 +1,12 @@ [testA] +enabled = True root = / -slug = config/{group}.html +slug = "config/{}.html".format(this.key) template = example-config.html split = ' ' + +[testA.fields] +title = "Tagged: " ~ this.group + +[testA.key_map] +Blog = News diff --git a/examples/packages/advanced-example/lektor_advanced.py b/examples/packages/advanced-example/lektor_advanced.py index 5bb2942..5ba2d21 100644 --- a/examples/packages/advanced-example/lektor_advanced.py +++ b/examples/packages/advanced-example/lektor_advanced.py @@ -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'{tag}' + return f'{tag}' args.field.source = regex.sub(_fn, obj) diff --git a/examples/packages/simple-example/lektor_simple.py b/examples/packages/simple-example/lektor_simple.py index 61490ae..3610da8 100644 --- a/examples/packages/simple-example/lektor_simple.py +++ b/examples/packages/simple-example/lektor_simple.py @@ -1,17 +1,26 @@ # -*- coding: utf-8 -*- from lektor.pluginsystem import Plugin -from lektor.utils import slugify +from typing import Iterator, Tuple +from datetime import datetime +from lektor_groupby import GroupBy, GroupByCallbackArgs class SimpleGroupByPlugin(Plugin): - def on_groupby_after_build_all(self, groupby, builder, **extra): - @groupby.watch('/blog', 'testB', slug='simple/{group}/index.html', - template='example-simple.html', flatten=True) - def convert_simple_example(args): + def on_groupby_before_build_all(self, groupby: GroupBy, builder, **extra): + watcher = groupby.add_watcher('testB', { + 'root': '/blog', + 'slug': 'simple/{key}/index.html', + 'template': 'example-simple.html', + }) + watcher.config.set_key_map({'Foo': 'bar'}) + watcher.config.set_fields({'date': datetime.now()}) + + @watcher.grouping(flatten=True) + def fn_simple(args: GroupByCallbackArgs) -> Iterator[Tuple[str, dict]]: # Yield groups - value = args.field # list type since model is 'strings' type + value = args.field # type: list # since model is 'strings' type for tag in value: - yield slugify(tag), {'val': tag, 'tags_in_page': len(value)} + yield tag, {'tags_in_page': value} # Everything below is just for documentation purposes page = args.record # extract additional info from source fieldKey, flowIndex, flowKey = args.key # or get field index @@ -19,6 +28,6 @@ class SimpleGroupByPlugin(Plugin): obj = page[fieldKey] else: obj = page[fieldKey].blocks[flowIndex].get(flowKey) - print('page:', page) - print(' obj:', obj) - print() + print('[simple] page:', page) + print('[simple] obj:', obj) + print('[simple] ') diff --git a/examples/templates/example-advanced.html b/examples/templates/example-advanced.html index 9804b09..6cdbb3f 100644 --- a/examples/templates/example-advanced.html +++ b/examples/templates/example-advanced.html @@ -1,3 +1,4 @@
This is: {{this}}
+Custom field, desc: "{{this.desc}}"
+Children: {{this.children}}
diff --git a/examples/templates/example-config.html b/examples/templates/example-config.html index 1deafc2..db9547e 100644 --- a/examples/templates/example-config.html +++ b/examples/templates/example-config.html @@ -1,7 +1,9 @@This is: {{this}}
+Group: "{{this.group}}", Key: "{{this.key}}"
+Custom field title: {{this.title}}
This is: {{this}}
+Custom field date: {{this.date}}