Skip to content

Commit 128d29c

Browse files
committed
Initial commit
0 parents  commit 128d29c

File tree

7 files changed

+196
-0
lines changed

7 files changed

+196
-0
lines changed

.flake8

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[flake8]
2+
ignore = E203, E266, E501, W503, F403, F401
3+
max-line-length = 89
4+
max-complexity = 18
5+
# select = B,C,E,F,W,T4,B9

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
dist
2+
build
3+
.coverage
4+
.tox
5+
flymd*
6+
*.pyc
7+
*.pyo
8+
*.egg-info
9+
*~
10+
*#
11+
.#*
12+
*.org

.pre-commit-config.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
repos:
2+
- repo: https://github.com/ambv/black
3+
rev: "22.10.0"
4+
hooks:
5+
- id: black
6+
- repo: https://github.com/asottile/reorder_python_imports
7+
rev: "v3.9.0"
8+
hooks:
9+
- id: reorder-python-imports
10+
- repo: https://github.com/PyCQA/flake8
11+
rev: "5.0.4"
12+
hooks:
13+
- id: flake8
14+
language_version: python3
15+
additional_dependencies: [
16+
flake8-bugbear,
17+
]

LICENSE

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2022, Terminal Labs Inc.
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
1. Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
3. Neither the name of the copyright holder nor the names of its
16+
contributors may be used to endorse or promote products derived from
17+
this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Lektor Debug
2+
3+
This Plugin provides a `{% debug %}` template tag that will render helpful debugging information in a template. To use, simply add this tag in a template, and run lektor server in development mode with `LEKTOR_DEV=1 lektor server`.
4+
5+
This plugin replaces Lektor's built-in, optional `{% debug %}` tag from Jinja2's Debug Extension. That tag is completely superceded by this plugins, so we just replace it.
6+
7+
This plugins tag can also be left in with no ill-effects. Unless the environment variable `LEKTOR_DEV=1` is set, `{% debug %}` will be a noop and return an empty string, unlike Lektor's current behavior which, since Jinja2's Debug Extension would not be added, would raise a `TemplateSyntaxError`.
8+
9+
### `{% debug %}` contents
10+
11+
This tag automatically renders as a collapsed `<detail>` html element, so it is minimally invasive unless you need it. When expanded, it will show a Python-ish dict with keys for:
12+
13+
- All fields available on `this`
14+
- The complete `dir(this)`, which shows it's native attributes and methods (different from the above)
15+
- All flowblocks and their fields
16+
- All current template context, exactly like Jinja2's Debug Extension
17+
- All available template filters, exactly like Jinja2's Debug Extension
18+
- All available template tests, exactly like Jinja2's Debug Extension
19+
20+
The top-level keys are meant to quickly tell you what that piece of data is, but do not necessarily correspond to an available key in a real Python dict. For example, `this.fields` is not a key in any Lektor Python dict, but it's hopefully clear what this represents.
21+
22+
Flowblocks are listed as `this.<flowblock name>.blocks`, which is how you would access them in a template. Each flow field will present a list of dicts of all blocks' fields and their values.
23+
24+
This debug info is presented as a code block, ran through Lektor's markdown processor. Since that is the case, if you have [Lektor Markdown Highlighter](https://www.getlektor.com/plugins/lektor-markdown-highlighter/) installed and configured, this will also use the syntax highlighting for Python.
25+
26+
### Additional Template Context
27+
28+
This plugin also adds the following template variables globally:
29+
30+
- `{{ dir }}` Python's `dir` builtin
31+
- `{{ str }}` Python's `str` builtin
32+
- `{{ type }}` Python's `type` builtin

lektor_debug.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
import pprint
4+
5+
from jinja2.ext import DebugExtension
6+
from markupsafe import Markup
7+
8+
from lektor.pluginsystem import Plugin
9+
from lektor.markdown import Markdown
10+
from lektor.types.flow import FlowDescriptor
11+
12+
class LektorDebugExtension(DebugExtension):
13+
"""Jinja extension for Lektor to replace Jinja's native Debug extension"""
14+
def _render(self, context) -> str:
15+
if not os.environ.get("LEKTOR_DEV") == "1":
16+
return ""
17+
18+
raw = {}
19+
20+
this = context.get('this') # not always present
21+
if this:
22+
data = getattr(this, '_data', None)
23+
if data:
24+
raw['this.fields'] = data
25+
for k, v in data.items():
26+
if isinstance(v, FlowDescriptor):
27+
raw[f"flow: this.{k}.blocks"] = v._blocks
28+
raw['dir(this)'] = dir(this)
29+
30+
raw.update({
31+
"context": context.get_all(),
32+
"filters": sorted(self.environment.filters.keys()),
33+
"tests": sorted(self.environment.tests.keys()),
34+
})
35+
md_str = f"""\
36+
```python
37+
{pprint.pformat(raw, depth=3, compact=True)}
38+
```
39+
"""
40+
options = {'type': 'markdown'}
41+
record = context['this']
42+
md = Markdown(md_str, record, options)
43+
return Markup(f"<details><summary>DEBUG INFO -></summary>{md}</details>")
44+
45+
46+
class DebugPlugin(Plugin):
47+
name = 'Lektor Debug'
48+
description = 'A Lektor Plugin for Debugging Help'
49+
50+
# TODO run this on all builds. on_before_build_all seems to sometimes error
51+
# with `TemplateSyntaxError: Encountered unknown tag 'debug'` and sometimes not.
52+
# def on_before_build_all(self, builder, **extra):
53+
def on_server_spawn(self, **extras):
54+
if "jinja2.ext.DebugExtension" in self.env.jinja_env.extensions:
55+
del self.env.jinja_env.extensions["jinja2.ext.DebugExtension"]
56+
57+
# Always add the extension so the tag always works. It is just a noop
58+
# unless LEKTOR_DEV==1. This let's us keep the tag in all the time.
59+
self.env.jinja_env.add_extension(LektorDebugExtension)
60+
61+
def on_setup_env(self, **extra):
62+
self.env.jinja_env.globals['str'] = str
63+
self.env.jinja_env.globals['dir'] = dir
64+
self.env.jinja_env.globals['type'] = type

setup.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import ast
2+
import io
3+
import re
4+
5+
from setuptools import setup, find_packages
6+
7+
with io.open('README.md', 'rt', encoding="utf8") as f:
8+
readme = f.read()
9+
10+
_description_re = re.compile(r'description\s+=\s+(?P<description>.*)')
11+
12+
with open('lektor_debug.py', 'rb') as f:
13+
description = str(ast.literal_eval(_description_re.search(
14+
f.read().decode('utf-8')).group(1)))
15+
16+
setup(
17+
author='Joseph Nix',
18+
author_email='[email protected]',
19+
description=description,
20+
keywords='Lektor plugin',
21+
license='BSD-3-Clause',
22+
long_description=readme,
23+
long_description_content_type='text/markdown',
24+
name='lektor-debug',
25+
packages=find_packages(),
26+
py_modules=['lektor_debug'],
27+
url='https://github.com/terminal-labs/lektor-debug',
28+
version='0.1',
29+
classifiers=[
30+
'Framework :: Lektor',
31+
'Environment :: Plugins',
32+
],
33+
entry_points={
34+
'lektor.plugins': [
35+
'debug = lektor_debug:DebugPlugin',
36+
]
37+
}
38+
)

0 commit comments

Comments
 (0)