Skip to content
Permalink
Browse files
Added section -> category conversion.
  • Loading branch information
felixfontein committed May 2, 2018
1 parent 3881184 commit 685be9cb24cf13a4b9978baefa2e01c44ef2200d
Showing with 33 additions and 15 deletions.
  1. +11 −6 v8/upgrade_metadata_v8/README.md
  2. +22 −9 v8/upgrade_metadata_v8/upgrade_metadata_v8.py
@@ -1,12 +1,17 @@
Nikola Metadata Upgrade (v7 to v8)
==================================

This is a plugin to upgrade old-style tags metadata (`draft`, `private`, `mathjax`)
to new-style metadata, i.e. using the `status` and `has_math` metadata fields. The
tags were used in Nikola v7 and earlier, and are no longer used by default in
Nikola v8. If you are using them in your blog, **you will be warned** by
`nikola build` — otherwise, don’t install this plugin (it won’t do a thing anyway
and will only waste disk space and load time).
This plugin fulfills two purposes:

1. It upgrades old-style tags metadata (`draft`, `private`, `mathjax`)
to new-style metadata, i.e. using the `status` and `has_math` metadata fields.
The tags were used in Nikola v7 and earlier, and are no longer used by default
in Nikola v8. If you are using them in your blog, **you will be warned** by
`nikola build` — otherwise, don’t install this plugin (it won’t do a thing
anyway and will only waste disk space and load time).

2. It converts sections to categories. If both section and category are specified
in a post, a warning is printed and the category kept.

The plugin adds the `nikola upgrade_metadata_v8` command. Remove the plugin
manually after the upgrade.
@@ -34,7 +34,7 @@


class UpgradeMetadata(Command):
"""Convert special tags (draft, private, mathjax) to status and has_math metadata."""
"""Convert special tags (draft, private, mathjax) to status and has_math metadata. Also removes sections."""

name = 'upgrade_metadata_v8'
doc_purpose = 'Convert special tags (draft, private, mathjax) to metadata'
@@ -61,13 +61,19 @@ def _execute(self, options, args):
self.site.scan_posts()
flagged = []
for post in self.site.timeline:
flag = False
if post.has_oldstyle_metadata_tags:
flag = True
for lang in self.site.config['TRANSLATIONS'].keys():
if 'section' in post.meta[lang]:
flag = True
if flag:
flagged.append(post)
if flagged:
if len(flagged) == 1:
L.info('1 post (and/or its translations) contains old-style metadata:')
L.info('1 post (and/or its translations) contains old-style metadata or have section metadata:')
else:
L.info('{0} posts (and/or their translations) contain old-style metadata:'.format(len(flagged)))
L.info('{0} posts (and/or their translations) contain old-style metadata or has section metadata:'.format(len(flagged)))
for post in flagged:
L.info(' ' + (post.metadata_path if post.is_two_file else post.source_path))
L.warn('Please make a backup before running this plugin. It might eat your data.')
@@ -103,11 +109,9 @@ def _execute(self, options, args):
meta = extractor.extract_text(source_text)

# Consider metadata mappings
sources = {
'tags': 'tags',
'status': 'status',
'has_math': 'has_math',
}
sources = {}
for m in ('tags', 'status', 'has_math', 'section', 'category'):
sources[m] = m
for foreign, ours in self.site.config.get('METADATA_MAPPING', {}).get(extractor.map_from, {}).items():
if ours in sources:
sources[ours] = foreign
@@ -138,6 +142,15 @@ def _execute(self, options, args):
meta[sources['has_math']] = 'yes'
updated = True

if meta.get(sources['section']):
if meta.get(sources['category']):
L.warn('Cannot completely {0} (language {1}): both section and category are specified. Please determine the correct category to use yourself!'.format(fname, lang))
fully_converted = False
else:
meta[sources['category']] = meta[sources['section']]
del meta[sources['section']]
updated = True

if tags_are_string:
meta[sources['tags']] = ', '.join(tags)

@@ -166,5 +179,5 @@ def _execute(self, options, args):
else:
L.info('Metadata not upgraded.')
else:
L.info('No posts found with special tags. No action is required.')
L.info('No posts found with special tags or section metadata. No action is required.')
L.info('You can safely set the USE_TAG_METADATA and the WARN_ABOUT_TAG_METADATA settings to False.')

0 comments on commit 685be9c

Please sign in to comment.