Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
system-wide shortcode support
  • Loading branch information
ralsina committed Dec 24, 2015
1 parent 7a97475 commit ce07d6b
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions nikola/nikola.py
Expand Up @@ -1294,19 +1294,24 @@ def url_replacer(self, src, dst, lang=None, url_type=None):
return result

def _register_templated_shortcodes(self):
"""Register shortcodes provided by templates in shortcodes/ folder."""
if not os.path.isdir('shortcodes'):
return
for fname in os.listdir('shortcodes'):
name, ext = os.path.splitext(fname)
if ext == '.tmpl':
with open(os.path.join('shortcodes', fname)) as fd:
template_data = fd.read()

def render_shortcode(t_data=template_data, **kw):
return self.template_system.render_template_to_string(t_data, kw)

self.register_shortcode(name, render_shortcode)
"""Register shortcodes provided by templates in shortcodes/ folders."""

builtin_sc_dir = resource_filename('nikola', os.path.join('data', 'shortcodes', utils.get_template_engine(self.THEMES)))
sc_dirs = [builtin_sc_dir]
if os.path.isdir('shortcodes'):
sc_dirs.append('shortcodes')

for sc_dir in sc_dirs:
for fname in os.listdir(sc_dir):
name, ext = os.path.splitext(fname)
if ext == '.tmpl':
with open(os.path.join(sc_dir, fname)) as fd:
template_data = fd.read()

def render_shortcode(t_data=template_data, **kw):
return self.template_system.render_template_to_string(t_data, kw)

self.register_shortcode(name, render_shortcode)

def register_shortcode(self, name, f):
"""Register function f to handle shortcode "name"."""
Expand Down

0 comments on commit ce07d6b

Please sign in to comment.