Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dfc874d

Browse files
committedMay 1, 2018
Began working on plugin for #2761 (no real code yet).
1 parent af3c6a1 commit dfc874d

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[Core]
2+
Name = upgrade_metadata_v8
3+
Module = upgrade_metadata_v8
4+
5+
[Nikola]
6+
PluginCategory = Command
7+
MinVersion = 8.0.0
8+
9+
[Documentation]
10+
Author = Chris Warrick, Felix Fontein
11+
Version = 0.1
12+
website = https://getnikola.com/
13+
Description = Convert special tags (draft, private, mathjax) to metadata
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright © 2014–2015, Chris Warrick.
4+
# Copyright © 2018, Felix Fontein.
5+
6+
# Permission is hereby granted, free of charge, to any
7+
# person obtaining a copy of this software and associated
8+
# documentation files (the "Software"), to deal in the
9+
# Software without restriction, including without limitation
10+
# the rights to use, copy, modify, merge, publish,
11+
# distribute, sublicense, and/or sell copies of the
12+
# Software, and to permit persons to whom the Software is
13+
# furnished to do so, subject to the following conditions:
14+
#
15+
# The above copyright notice and this permission notice
16+
# shall be included in all copies or substantial portions of
17+
# the Software.
18+
#
19+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
20+
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
21+
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22+
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
23+
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25+
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27+
28+
from __future__ import unicode_literals
29+
import io
30+
import os
31+
import nikola.post
32+
from nikola.plugin_categories import Command
33+
from nikola import utils
34+
35+
36+
class UpgradeMetadata(Command):
37+
"""Convert special tags (draft, private, mathjax) to status and has_math metadata."""
38+
39+
name = 'upgrade_metadata_v8'
40+
doc_purpose = 'Convert special tags (draft, private, mathjax) to metadata'
41+
cmd_options = [
42+
{
43+
'name': 'yes',
44+
'short': 'y',
45+
'long': 'yes',
46+
'type': bool,
47+
'default': False,
48+
'help': 'Proceed without confirmation',
49+
},
50+
]
51+
52+
def _execute(self, options, args):
53+
L = utils.get_logger('upgrade_metadata_v8', utils.STDERR_HANDLER)
54+
55+
# scan posts
56+
self.site.scan_posts()
57+
flagged = []
58+
for post in self.site.timeline:
59+
if post.has_oldstyle_metadata_tags:
60+
flagged.append(post)
61+
if flagged:
62+
if len(flagged) == 1:
63+
L.info('1 post (and/or its translations) contains old-style metadata:')
64+
else:
65+
L.info('{0} posts (and/or their translations) contain old-style metadata:'.format(len(flagged)))
66+
for post in flagged:
67+
L.info(' ' + post.metadata_path)
68+
if not options['yes']:
69+
yesno = utils.ask_yesno("Proceed with metadata upgrade?")
70+
if options['yes'] or yesno:
71+
for post in flagged:
72+
for lang in self.site.config['TRANSLATIONS'].keys():
73+
if lang == post.default_lang:
74+
fname = post.metadata_path
75+
else:
76+
meta_path = os.path.splitext(post.source_path)[0] + '.meta'
77+
fname = utils.get_translation_candidate(post.config, meta_path, lang)
78+
79+
if os.path.exists(fname):
80+
with io.open(fname, 'r', encoding='utf-8') as fh:
81+
meta = fh.readlines()
82+
83+
if not meta[min(1, len(meta) - 1)].startswith('.. '):
84+
# check if we’re dealing with old style metadata
85+
with io.open(fname, 'w', encoding='utf-8') as fh:
86+
for k, v in zip(self.fields, meta):
87+
fh.write('.. {0}: {1}'.format(k, v))
88+
L.debug(fname)
89+
90+
L.info('{0} posts upgraded.'.format(len(flagged)))
91+
else:
92+
L.info('Metadata not upgraded.')
93+
else:
94+
L.info('No posts found with special tags. No action is required.')
95+
L.info('You can safely set the USE_TAG_METADATA and the WARN_ABOUT_TAG_METADATA settings to False.')

‎nikola/post.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ def __init__(
243243
is_private = False
244244
post_status = 'published'
245245
self._tags = {}
246+
self.has_oldstyle_metadata_tags = False
246247
for lang in self.translated_to:
247248
if isinstance(self.meta[lang]['tags'], (list, tuple, set)):
248249
_tag_list = self.meta[lang]['tags']
@@ -282,8 +283,10 @@ def __init__(
282283
show_warning = True
283284
if show_warning:
284285
LOGGER.warn('It is suggested that you convert special tags to metadata and set '
285-
'USE_TAG_METADATA to False. Change the WARN_ABOUT_TAG_METADATA '
286+
'USE_TAG_METADATA to False. You can use the upgrade_metadata_v8 '
287+
'command plugin for conversion. Change the WARN_ABOUT_TAG_METADATA '
286288
'configuration to disable this warning.')
289+
self.has_oldstyle_metadata_tags = True
287290
if self.config['USE_TAG_METADATA']:
288291
if 'draft' in [_.lower() for _ in self._tags[lang]]:
289292
is_draft = True

0 commit comments

Comments
 (0)
Please sign in to comment.