Skip to content
Permalink
Browse files

Put things in the right place

  • Loading branch information
ralsina committed Jun 5, 2017
1 parent f743443 commit 246b441c905576541e2c389d74da233c4eee8cb4
@@ -328,27 +328,6 @@ 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()
@@ -137,8 +137,11 @@ def create_post(self, path, **kw):
content += '\n'
with io.open(path, "w+", encoding="utf8") as fd:
if onefile:
format = self.site.config.get('METADATA_FORMAT', 'nikola')
if format == 'pelican':
format = 'pelican_md'
fd.write('<!-- \n')
fd.write(write_metadata(metadata))
fd.write(write_metadata(metadata, format))
fd.write('-->\n\n')
fd.write(content)

@@ -177,7 +177,10 @@ def create_post(self, path, **kw):
content += '\n'
with io.open(path, "w+", encoding="utf8") as fd:
if onefile:
fd.write(write_metadata(metadata))
format = self.site.config.get('METADATA_FORMAT', 'nikola')
if format == 'pelican':
format = 'pelican_rest'
fd.write(write_metadata(metadata, format))
fd.write('\n')
fd.write(content)

@@ -1458,24 +1458,48 @@ def get_translation_candidate(config, path, lang):
return config['TRANSLATIONS_PATTERN'].format(path=p, ext=e, lang=lang)


def write_metadata(data):
def write_metadata(data, format='nikola'):
"""Write metadata."""
order = ('title', 'slug', 'date', 'tags', 'category', 'link', 'description', 'type')
f = '.. {0}: {1}'
meta = []
for k in order:
try:
meta.append(f.format(k, data.pop(k)))
except KeyError:
pass

# Leftover metadata (user-specified/non-default).
for k in natsort.natsorted(list(data.keys()), alg=natsort.ns.F | natsort.ns.IC):
meta.append(f.format(k, data[k]))

meta.append('')
format = format.lower()
if format not in ['nikola', 'yaml', 'toml', 'pelican_rest', 'pelican_md']:
LOGGER.warn('Unknown METADATA_FORMAT %s, using "nikola" format', format)

return '\n'.join(meta)
if format == 'yaml':
if yaml is None:
req_missing('pyyaml', 'use YAML metadata', optional=False)
return '\n'.join(('---', yaml.dump(data), '---'), '')
elif format == 'toml':
if toml is None:
req_missing('toml', 'use TOML metadata', optional=False)
return '\n'.join(('+++', toml.dumps(data), '+++'), '')
elif format == 'pelican_rest':
title = data.pop('title')
results = [
'=' * len(title),
title,
'=' * len(title),
''
] + [':{0}: {1}'.format(k, v) for k, v in data.items()] + ['']
return '\n'.join(results)

elif format == 'pelican_md':
['{0}: {1}'.format(k, v) for k, v in data.items()] + ['']
return '\n'.join(results)

else: # Nikola, default
order = ('title', 'slug', 'date', 'tags', 'category', 'link', 'description', 'type')
f = '.. {0}: {1}'
meta = []
for k in order:
try:
meta.append(f.format(k, data.pop(k)))
except KeyError:
pass
# Leftover metadata (user-specified/non-default).
for k in natsort.natsorted(list(data.keys()), alg=natsort.ns.F | natsort.ns.IC):
meta.append(f.format(k, data[k]))
meta.append('')
return '\n'.join(meta)


def ask(query, default=None):

0 comments on commit 246b441

Please sign in to comment.