typing
This commit is contained in:
@@ -1,59 +1,56 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from lektor.context import get_ctx
|
||||
from lektor.db import Record # typing
|
||||
from lektor.markdown import Markup
|
||||
from lektor.pluginsystem import Plugin # subclass
|
||||
from lektor.sourceobj import VirtualSourceObject as VObj
|
||||
from lektor.pluginsystem import Plugin, IniFile # subclass
|
||||
from lektor.sourceobj import VirtualSourceObject as VObj # typing
|
||||
|
||||
from typing import Set, Dict, Any, Iterator, Generator
|
||||
import re
|
||||
from typing import Set, Dict, Iterator, Generator
|
||||
from lektor_groupby.groupby import GroupBy # typing
|
||||
from lektor_groupby.util import report_config_error
|
||||
from lektor_groupby.watcher import GroupByCallbackArgs # typing
|
||||
|
||||
|
||||
class InlineTagsPlugin(Plugin):
|
||||
name = 'inlinetags'
|
||||
description = 'Auto-detect and reference tags inside written text.'
|
||||
|
||||
def on_setup_env(self, **extra) -> None:
|
||||
def _get_tags(record, *, recursive=False) -> Iterator[VObj]:
|
||||
def on_setup_env(self, **extra: Any) -> None:
|
||||
def _fn(record: Record, *, recursive: bool = False) -> Iterator[VObj]:
|
||||
fn = self.env.jinja_env.filters['vgroups']
|
||||
yield from fn(record, *self.config_keys, recursive=recursive)
|
||||
|
||||
self.env.jinja_env.filters.update(inlinetags=_get_tags)
|
||||
self.env.jinja_env.filters.update(inlinetags=_fn)
|
||||
|
||||
def on_process_template_context(self, context, **extra) -> None:
|
||||
# track dependency for observer replacement below
|
||||
def on_process_template_context(self, context: Dict, **extra: Any) -> None:
|
||||
if hasattr(context.get('this'), '_inlinetag_modified'):
|
||||
ctx = get_ctx()
|
||||
if ctx:
|
||||
ctx.record_dependency(self.config_filename)
|
||||
|
||||
def on_groupby_before_build_all(self, groupby, **extra) -> None:
|
||||
def on_groupby_before_build_all(self, groupby: GroupBy, **ex: Any) -> None:
|
||||
self.config_keys = set() # type: Set[str]
|
||||
# load config
|
||||
config = self.get_config()
|
||||
for sect in config.sections():
|
||||
if '.' in sect: # e.g., sect.fields and sect.key_map
|
||||
continue
|
||||
if self.add_observer(groupby, config, sect):
|
||||
if self._add(sect, config, groupby):
|
||||
self.config_keys.add(sect)
|
||||
|
||||
def add_observer(self, groupby, config, sect_key) -> bool:
|
||||
# get regex
|
||||
pattern = config.section_as_dict(sect_key + '.pattern')
|
||||
regex_str = pattern.get('match', r'{{([^}]{1,32})}}') # type: str
|
||||
tag_replace = pattern.get('replace', '{name}') # type: str
|
||||
|
||||
# validate input
|
||||
def _add(self, sect_key: str, config: IniFile, groupby: GroupBy) -> bool:
|
||||
_pattern = config.section_as_dict(sect_key + '.pattern')
|
||||
regex_str = _pattern.get('match', r'{{([^}]{1,32})}}') # type: str
|
||||
tag_replace = _pattern.get('replace', '{name}') # type: str
|
||||
try:
|
||||
regex = re.compile(regex_str)
|
||||
except Exception as e:
|
||||
report_config_error(sect_key, 'pattern.match', regex_str, e)
|
||||
return False
|
||||
|
||||
# add observer
|
||||
watcher = groupby.add_watcher(sect_key, config)
|
||||
|
||||
@watcher.grouping()
|
||||
def convert_inlinetags(args) -> Generator[str, str, None]:
|
||||
def _inlinetag(args: GroupByCallbackArgs) -> Generator[str, str, None]:
|
||||
arr = args.field if isinstance(args.field, list) else [args.field]
|
||||
_tags = {} # type: Dict[str, str]
|
||||
for obj in arr:
|
||||
|
||||
Reference in New Issue
Block a user