Skip to content
Permalink
Browse files

Merge pull request #2478 from getnikola/global-data

global data (Issue #2477)
  • Loading branch information
ralsina committed Aug 28, 2016
2 parents 5da1b87 + 8378474 commit 8eafc13fa0b21662048662ce6a151a54676a60a2
Showing with 33 additions and 3 deletions.
  1. +1 −0 CHANGES.txt
  2. +14 −0 docs/manual.txt
  3. +18 −3 nikola/nikola.py
@@ -15,6 +15,7 @@ Features
* Added new options --html2text and --transform-to-markdown
to WordPress importer (Issue #2261)
* Listing: guess the lexer if cannot be determined from the file name.
* Read files from data/ and insert data in global context (Issue #2477)

Bugfixes
--------
@@ -956,6 +956,20 @@ In that case, the template engine used will be your theme's and the arguments yo
as well as the global context from your ``conf.py``, are available to the template you
are creating.

.. sidebar:: The Global Context

There is a ``GLOBAL_CONTEXT`` field in your ``conf.py`` where you can
put things you want to make available to your templates.

It will also contain things you put in a ``data/`` directory within your
site. For example, if you create ``data/foo.json`` containing this::

{"bar": "bat"}

Then your templates can use things like ``${data['foo']['bar']}`` and
it will be replaced by "bat".


Redirections
------------

@@ -32,6 +32,7 @@
from copy import copy
from pkg_resources import resource_filename
import datetime
import glob
import locale
import os
import json
@@ -896,7 +897,8 @@ def __init__(self, **config):
else:
self.bad_compilers.add(k)

self._set_global_context()
self._set_global_context_from_config()
self._set_global_context_from_data()

# Set persistent state facility
self.state = Persistor('state_data.json')
@@ -1036,8 +1038,12 @@ def plugin_position_in_places(plugin):
self._register_templated_shortcodes()
signal('configured').send(self)

def _set_global_context(self):
"""Create global context from configuration."""
def _set_global_context_from_config(self):
"""Create global context from configuration.
These are options that are used by templates, so they always need to be
available.
"""
self._GLOBAL_CONTEXT['url_type'] = self.config['URL_TYPE']
self._GLOBAL_CONTEXT['timezone'] = self.tzinfo
self._GLOBAL_CONTEXT['_link'] = self.link
@@ -1128,6 +1134,15 @@ def _set_global_context(self):

self._GLOBAL_CONTEXT.update(self.config.get('GLOBAL_CONTEXT', {}))

def _set_global_context_from_data(self):
"""Load files from data/ and put them in the global context."""
self._GLOBAL_CONTEXT['data'] = {}
for fname in glob.glob('data/*'):
data = utils.load_data(fname)
key = os.path.basename(fname)
key = os.path.splitext(key)[0]
self._GLOBAL_CONTEXT['data'][key] = data

def _activate_plugins_of_category(self, category):
"""Activate all the plugins of a given category and return them."""
# this code duplicated in tests/base.py

0 comments on commit 8eafc13

Please sign in to comment.