Skip to content
Permalink
Browse files

metadata format helper

  • Loading branch information
ralsina committed Jun 5, 2017
1 parent 61b50a1 commit f7434435a74d45888fcb30d3e49fcc57e208251e
Showing with 30 additions and 1 deletion.
  1. +30 −1 nikola/plugin_categories.py
@@ -34,8 +34,16 @@

from yapsy.IPlugin import IPlugin
from doit.cmd_base import Command as DoitCommand
try:
import toml
except ImportError:
toml = None
try:
import yaml
except ImportError:
yaml = None

from .utils import LOGGER, first_line
from .utils import LOGGER, first_line, req_missing

__all__ = (
'Command',
@@ -320,6 +328,27 @@ def compile_html(self, source, dest, is_two_file=True):
"""Compile the source, save it on dest (DEPRECATED)."""
raise NotImplementedError()

def format_metadata(self, metadata, format='nikola'):
"""Return a string with metadata formatted according to preferences."""
format = self.site.config.get('METADATA_FORMAT', format).lower()

if format not in ['nikola', 'yaml', 'toml']:
LOGGER.warn('Unknown METADATA_FORMAT %s', format)

if format == 'yaml':
if yaml is None:
req_missing('pyyaml', 'use YAML metadata', optional=False)
return '\n'.join(('---', yaml.dump(metadata), '---'))
elif format == 'toml':
if toml is None:
req_missing('toml', 'use TOML metadata', optional=False)
return '\n'.join(('+++', toml.dumps(metadata), '+++'))
else: # Nikola
results = []
for k, v in metadata.items:
results.append('.. {k}: {v}'.format(k=k, v=v))
return '\n'.join(results)

def create_post(self, path, content=None, onefile=False, is_page=False, **kw):
"""Create post file with optional metadata."""
raise NotImplementedError()

0 comments on commit f743443

Please sign in to comment.