Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add support for theme meta files (#2758)
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed May 14, 2017
1 parent 3270735 commit 19891cc
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 19 deletions.
10 changes: 10 additions & 0 deletions nikola/data/themes/base-jinja/base-jinja.theme
@@ -0,0 +1,10 @@
[Theme]
engine = jinja
parent = base
author = The Nikola Contributors
author_url = https://getnikola.com/
license = MIT

[Family]
family = base
mako_version = base
1 change: 0 additions & 1 deletion nikola/data/themes/base-jinja/engine

This file was deleted.

1 change: 0 additions & 1 deletion nikola/data/themes/base-jinja/parent

This file was deleted.

9 changes: 9 additions & 0 deletions nikola/data/themes/base/base.theme
@@ -0,0 +1,9 @@
[Theme]
engine = mako
author = The Nikola Contributors
author_url = https://getnikola.com/
license = MIT

[Family]
family = base
jinja_version = base-jinja
1 change: 0 additions & 1 deletion nikola/data/themes/base/engine

This file was deleted.

13 changes: 13 additions & 0 deletions nikola/data/themes/bootstrap3-jinja/bootstrap3-jinja.theme
@@ -0,0 +1,13 @@
[Theme]
engine = jinja
parent = base-jinja
author = The Nikola Contributors
author_url = https://getnikola.com/
based_on = Bootstrap 3 <http://getbootstrap.com/>
license = MIT
tags = bootstrap

[Family]
family = bootstrap3
mako_version = bootstrap3
variants = bootstrap3-gradients, bootstrap3-gradients-jinja
1 change: 0 additions & 1 deletion nikola/data/themes/bootstrap3-jinja/engine

This file was deleted.

1 change: 0 additions & 1 deletion nikola/data/themes/bootstrap3-jinja/parent

This file was deleted.

13 changes: 13 additions & 0 deletions nikola/data/themes/bootstrap3/bootstrap3.theme
@@ -0,0 +1,13 @@
[Theme]
engine = mako
parent = base
author = The Nikola Contributors
author_url = https://getnikola.com/
based_on = Bootstrap 3 <http://getbootstrap.com/>
license = MIT
tags = bootstrap

[Family]
family = bootstrap3
jinja_version = bootstrap3-jinja
variants = bootstrap3-gradients, bootstrap3-gradients-jinja
1 change: 0 additions & 1 deletion nikola/data/themes/bootstrap3/engine

This file was deleted.

1 change: 0 additions & 1 deletion nikola/data/themes/bootstrap3/parent

This file was deleted.

3 changes: 3 additions & 0 deletions nikola/nikola.py
Expand Up @@ -2267,6 +2267,9 @@ def generic_page_renderer(self, lang, post, filters, context=None):
deps = post.deps(lang)
uptodate_deps = post.deps_uptodate(lang)
deps.extend(utils.get_asset_path(x, self.THEMES) for x in ('bundles', 'parent', 'engine'))
_theme_ini = utils.get_asset_path(self.config['THEME'] + '.theme', self.THEMES)
if _theme_ini:
deps.append(_theme_ini)

context = copy(context) if context else {}
context['post'] = post
Expand Down
50 changes: 38 additions & 12 deletions nikola/utils.py
Expand Up @@ -28,6 +28,7 @@

from __future__ import print_function, unicode_literals, absolute_import
import calendar
import configparser
import datetime
import dateutil.tz
import hashlib
Expand Down Expand Up @@ -607,27 +608,52 @@ def get_theme_path(theme):
return theme


def parse_theme_meta(theme_dir):
"""Parse a .theme meta file."""
cp = configparser.ConfigParser()
# The `or` case is in case theme_dir ends with a trailing slash
theme_name = os.path.basename(theme_dir) or os.path.basename(os.path.dirname(theme_dir))
theme_meta_path = os.path.join(theme_dir, theme_name + '.theme')
cp.read(theme_meta_path)
return cp if cp.has_section('Theme') else None


def get_template_engine(themes):
"""Get template engine used by a given theme."""
for theme_name in themes:
engine_path = os.path.join(theme_name, 'engine')
if os.path.isfile(engine_path):
with open(engine_path) as fd:
return fd.readlines()[0].strip()
# default
return 'mako'
meta = parse_theme_meta(theme_name)
if meta:
e = meta.get('Theme', 'engine', fallback=None)
if e:
return e
else:
# Theme still uses old-style parent/engine files
engine_path = os.path.join(theme_name, 'engine')
if os.path.isfile(engine_path):
with open(engine_path) as fd:
return fd.readlines()[0].strip()
# default
return 'mako'


def get_parent_theme_name(theme_name, themes_dirs=None):
"""Get name of parent theme."""
parent_path = os.path.join(theme_name, 'parent')
if os.path.isfile(parent_path):
with open(parent_path) as fd:
parent = fd.readlines()[0].strip()
if themes_dirs:
meta = parse_theme_meta(theme_name)
if meta:
parent = meta.get('Theme', 'parent', fallback=None)
if themes_dirs and parent:
return get_theme_path_real(parent, themes_dirs)
return parent
return None
else:
# Theme still uses old-style parent/engine files
parent_path = os.path.join(theme_name, 'parent')
if os.path.isfile(parent_path):
with open(parent_path) as fd:
parent = fd.readlines()[0].strip()
if themes_dirs:
return get_theme_path_real(parent, themes_dirs)
return parent
return None


def get_theme_chain(theme, themes_dirs):
Expand Down

0 comments on commit 19891cc

Please sign in to comment.