Skip to content

Commit

Permalink
metadata format helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Alsina committed Jun 5, 2017
1 parent 61b50a1 commit f743443
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion nikola/plugin_categories.py
Expand Up @@ -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',
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit f743443

Please sign in to comment.