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

@@ -2,4 +2,4 @@
name = GroupBy Examples name = GroupBy Examples
[packages] [packages]
lektor-groupby = 0.9.1 lektor-groupby = 0.9.3

View File

@@ -1 +1,15 @@
[testC]
root = /
slug = advanced/{key}/
template = example-advanced.html
[testC.pattern]
match = {{([^}]{1,32})}} match = {{([^}]{1,32})}}
[testC.fields]
desc = "Tag: " ~ this.group ~ ", Key: " ~ this.key
[testC.key_map]
Blog = case-sensitive
Two = three
three = no-nested-replace

View File

@@ -1,5 +1,12 @@
[testA] [testA]
enabled = True
root = / root = /
slug = config/{group}.html slug = "config/{}.html".format(this.key)
template = example-config.html template = example-config.html
split = ' ' split = ' '
[testA.fields]
title = "Tagged: " ~ this.group
[testA.key_map]
Blog = News

View File

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

View File

@@ -1,17 +1,26 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from lektor.pluginsystem import Plugin 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): class SimpleGroupByPlugin(Plugin):
def on_groupby_after_build_all(self, groupby, builder, **extra): def on_groupby_before_build_all(self, groupby: GroupBy, builder, **extra):
@groupby.watch('/blog', 'testB', slug='simple/{group}/index.html', watcher = groupby.add_watcher('testB', {
template='example-simple.html', flatten=True) 'root': '/blog',
def convert_simple_example(args): '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 # 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: 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 # Everything below is just for documentation purposes
page = args.record # extract additional info from source page = args.record # extract additional info from source
fieldKey, flowIndex, flowKey = args.key # or get field index fieldKey, flowIndex, flowKey = args.key # or get field index
@@ -19,6 +28,6 @@ class SimpleGroupByPlugin(Plugin):
obj = page[fieldKey] obj = page[fieldKey]
else: else:
obj = page[fieldKey].blocks[flowIndex].get(flowKey) obj = page[fieldKey].blocks[flowIndex].get(flowKey)
print('page:', page) print('[simple] page:', page)
print(' obj:', obj) print('[simple] obj:', obj)
print() print('[simple] ')

View File

@@ -1,3 +1,4 @@
<h2>Path: {{ this | url(absolute=True) }}</h2> <h2>Path: {{ this | url(absolute=True) }}</h2>
<div>This is: {{this}}</div> <p>This is: {{this}}</p>
<div>Children: {{this.children}}</div> <p>Custom field, desc: "{{this.desc}}"</p>
<p>Children: {{this.children}}</p>

View File

@@ -1,7 +1,9 @@
<h2>Path: {{ this | url(absolute=True) }}</h2> <h2>Path: {{ this | url(absolute=True) }}</h2>
<div>This is: {{this}}</div> <p>This is: {{this}}</p>
<p>Group: "{{this.group}}", Key: "{{this.key}}"</p>
<p>Custom field title: {{this.title}}</p>
<ul> <ul>
{%- for child, extras in this.children.items() %} {%- for child in this.children %}
<li>Page: {{ child.path }}, Tags: {{ extras }}</li> <li>Child: <a href="{{child|url}}">{{child.title}}</a> ({{child.path}})</li>
{%- endfor %} {%- endfor %}
</ul> </ul>

View File

@@ -1,12 +1,9 @@
<h2>Path: {{ this | url(absolute=True) }}</h2> <h2>Path: {{ this | url(absolute=True) }}</h2>
<div>This is: {{this}}</div> <p>This is: {{this}}</p>
<p>Custom field date: {{this.date}}</p>
<ul> <ul>
{%- for child, extras in this.children.items() %} {%- for child, extras in this.children.items() -%}
<li>Page: {{ child.path }}</li> {%- set etxra = (extras|first).tags_in_page %}
<ul> <li>{{etxra|length}} tags on page "{{child.path}}": {{etxra}}</li>
{%- for extra in extras %} {%- endfor %}
Name: {{ extra.val }}, Tag count: {{ extra.tags_in_page }}
{%- endfor %}
</ul>
{%- endfor %}
</ul> </ul>

View File

@@ -18,20 +18,12 @@ main { margin: 3em; }
{% block body %}{{ this.body }}{% endblock %} {% block body %}{{ this.body }}{% endblock %}
</main> </main>
<footer> <footer>
<div>Simple Tags: {%- for k, v in [('testA','Config'),('testB','Simple'),('testC','Advanced')] %}
{% for tag in ['blog','directory','blog-post','initial','samegroup'] %} <div>{{v}} Tags:
<a href="/blog/simple/{{tag}}/">({{tag}})</a> {%- for x in this|groupby(k, recursive=True)|list|unique|sort %}
{% endfor %} <a href="{{ x|url }}">({{x.key}})</a>
</div> {%- endfor %}
<div>Config Tags:
{% for tag in ['root','blog','directory','blog-post','initial','samegroup'] %}
<a href="/config/{{tag}}.html">({{tag}})</a>
{% endfor %}
</div>
<div>Advanced Tags:
{% for tag in ['tag','two','blog'] %}
<a href="/advanced/{{tag}}/">({{tag}})</a>
{% endfor %}
</div> </div>
{%- endfor %}
</footer> </footer>
</body> </body>