Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config_role plugin #290

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions v7/config_role/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Add the `config` role, it reads a value from the `conf.py` file.

Usage:

```rst
Send us an email to :config:`BLOG_EMAIL`
```

Here `BLOG_EMAIL` is the variable name from the `conf.py` file.
Note that the text is rendered as an email link, not just as plain text.
This is because the value is interpreted as reStructuredText.
13 changes: 13 additions & 0 deletions v7/config_role/config_role.plugin
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Core]
Name = config_role
Module = config_role

[Nikola]
Compiler = rest
PluginCategory = RestExtension

[Documentation]
Author = Santos Gallegos
Version = 0.1
Website = http://plugins.getnikola.com/#config_role
Description = Inserts values from config.py
52 changes: 52 additions & 0 deletions v7/config_role/config_role.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
stsewd marked this conversation as resolved.
Show resolved Hide resolved
Add the ``config`` role, it reads a value from the `conf.py` file.

Usage:

Send us an email to :config:`BLOG_EMAIL`

Here `BLOG_EMAIL` is the variable name from the conf.py file.
Note that the text is rendered as an email link, not just as plain text.
This is because the value is interpreted as reStructuredText.
"""

from docutils.frontend import OptionParser
from docutils.parsers.rst import Parser, roles
from docutils.utils import new_document
from nikola.plugin_categories import RestExtension


class Plugin(RestExtension):

name = 'config_role'

def set_site(self, site):
roles.register_canonical_role('config', config_role(site))
return super().set_site(site)


def config_role(site):
"""
Inject a value from the ``conf.py`` file.

Before injecting the value, it is parsed by the rst parser,
that way we can have a link when reading an email from the config
for example.

This role needs access to the `site` object.
As we can't pass extra args to a role function we are using a closure.
"""
def role(name, rawtext, text, lineno, inliner, options=None, content=None):
parser = Parser()
settings = OptionParser(components=(Parser,)).get_default_values()
document = new_document('Raw rst from config value', settings)

config_value = site.config.get(text, '')
parser.parse(config_value, document)

# The whole text is interpreted as a big paragraph
# causing a new line to be inserted at the end,
# so we only take the elements inside that paragraph.
paragraph, *_ = document.children
return paragraph.children, []
return role