|
| 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.') |
0 commit comments