Skip to content

Commit ee6a182

Browse files
committedAug 27, 2016
global data (Issue #2477)
1 parent ac49ce3 commit ee6a182

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed
 

‎CHANGES.txt

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Features
1414
* Added new options --html2text and --transform-to-markdown
1515
to WordPress importer (Issue #2261)
1616
* Listing: guess the lexer if cannot be determined from the file name.
17+
* Read files from data/ and insert data in global context (Issue #2477)
1718

1819
Bugfixes
1920
--------

‎docs/manual.txt

+14
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,20 @@ In that case, the template engine used will be your theme's and the arguments yo
956956
as well as the global context from your ``conf.py``, are available to the template you
957957
are creating.
958958

959+
.. sidebar:: The Global Context
960+
961+
There is a ``GLOBAL_CONTEXT`` field in your ``conf.py`` where you can
962+
put things you want to make available to your templates.
963+
964+
It will also contain things you put in a ``data/`` directory within your
965+
site. For example, if you create ``data/foo.json`` containing this::
966+
967+
{"bar": "bat"}
968+
969+
Then your templates can use things like ``${data['foo']['bar']}`` and
970+
it will be replaced by "bat".
971+
972+
959973
Redirections
960974
------------
961975

‎nikola/nikola.py

+19-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from copy import copy
3333
from pkg_resources import resource_filename
3434
import datetime
35+
import glob
3536
import locale
3637
import os
3738
import json
@@ -896,7 +897,8 @@ def __init__(self, **config):
896897
else:
897898
self.bad_compilers.add(k)
898899

899-
self._set_global_context()
900+
self._set_global_context_from_config()
901+
self._set_global_context_from_data()
900902

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

1039-
def _set_global_context(self):
1040-
"""Create global context from configuration."""
1041+
def _set_global_context_from_config(self):
1042+
"""Create global context from configuration.
1043+
1044+
These are options that are used by templates, so they always need to be
1045+
available.
1046+
"""
10411047
self._GLOBAL_CONTEXT['url_type'] = self.config['URL_TYPE']
10421048
self._GLOBAL_CONTEXT['timezone'] = self.tzinfo
10431049
self._GLOBAL_CONTEXT['_link'] = self.link
@@ -1128,6 +1134,16 @@ def _set_global_context(self):
11281134

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

1137+
def _set_global_context_from_data(self):
1138+
"""Load files from data/ and put them in the global context."""
1139+
self._GLOBAL_CONTEXT['data'] = {}
1140+
for fname in glob.glob('data/*'):
1141+
data = utils.load_data(fname)
1142+
key = os.path.basename(fname)
1143+
key = os.path.splitext(key)[0]
1144+
self._GLOBAL_CONTEXT['data'][key] = data
1145+
1146+
11311147
def _activate_plugins_of_category(self, category):
11321148
"""Activate all the plugins of a given category and return them."""
11331149
# this code duplicated in tests/base.py

0 commit comments

Comments
 (0)
Please sign in to comment.