add examples
This commit is contained in:
31
examples/packages/advanced-example/lektor_advanced.py
Normal file
31
examples/packages/advanced-example/lektor_advanced.py
Normal file
@@ -0,0 +1,31 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from lektor.pluginsystem import Plugin
|
||||
from lektor.utils import slugify
|
||||
import re
|
||||
|
||||
|
||||
class AdvancedGroupByPlugin(Plugin):
|
||||
def on_groupby_before_build_all(self, groupby, builder, **extra):
|
||||
# load config
|
||||
regex = self.get_config().get('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):
|
||||
# args.field assumed to be Markdown
|
||||
obj = args.field.source
|
||||
for match in regex.finditer(obj):
|
||||
tag = match.group(1)
|
||||
yield slugify(tag), tag
|
||||
|
||||
def _fn(match: re.Match) -> str:
|
||||
tag = match.group(1)
|
||||
return f'<a href="/advanced/{slugify(tag)}/">{tag}</a>'
|
||||
args.field.source = regex.sub(_fn, obj)
|
||||
12
examples/packages/advanced-example/setup.py
Normal file
12
examples/packages/advanced-example/setup.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='lektor-advanced',
|
||||
py_modules=['lektor_advanced'],
|
||||
version='1.0',
|
||||
entry_points={
|
||||
'lektor.plugins': [
|
||||
'advanced = lektor_advanced:AdvancedGroupByPlugin',
|
||||
]
|
||||
}
|
||||
)
|
||||
24
examples/packages/simple-example/lektor_simple.py
Normal file
24
examples/packages/simple-example/lektor_simple.py
Normal file
@@ -0,0 +1,24 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from lektor.pluginsystem import Plugin
|
||||
from lektor.utils import slugify
|
||||
|
||||
|
||||
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):
|
||||
# Yield groups
|
||||
value = args.field # list type since model is 'strings' type
|
||||
for tag in value:
|
||||
yield slugify(tag), {'val': tag, 'tags_in_page': len(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
|
||||
if flowIndex is None:
|
||||
obj = page[fieldKey]
|
||||
else:
|
||||
obj = page[fieldKey].blocks[flowIndex].get(flowKey)
|
||||
print('page:', page)
|
||||
print(' obj:', obj)
|
||||
print()
|
||||
12
examples/packages/simple-example/setup.py
Normal file
12
examples/packages/simple-example/setup.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='lektor-simple',
|
||||
py_modules=['lektor_simple'],
|
||||
version='1.0',
|
||||
entry_points={
|
||||
'lektor.plugins': [
|
||||
'simple = lektor_simple:SimpleGroupByPlugin',
|
||||
]
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user