Skip to content

Commit

Permalink
First shot at #2761.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfontein committed Apr 26, 2018
1 parent cabdff6 commit 1572a4e
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 23 deletions.
33 changes: 24 additions & 9 deletions docs/manual.rst
Expand Up @@ -300,7 +300,14 @@ date

tags
Comma-separated tags of the post. Some tags have special meaning, including
``draft``, ``private``, ``mathjax``
``draft``, ``private``, ``mathjax``. These special meanings can be disabled
with the ``USE_TAG_METADATA`` configuration setting.

status
Can be set to ``published`` (default), ``draft``, or ``private``.

has_math
If set to ``True``, ...

category
Like tags, except each post can have only one, and they usually have
Expand Down Expand Up @@ -809,11 +816,15 @@ Or you can completely customize the link using the ``READ_MORE_LINK`` option.
Drafts
~~~~~~

If you add a "draft" tag to a post, then it will not be shown in indexes and feeds.
It *will* be compiled, and if you deploy it it *will* be made available, so use
with care. If you wish your drafts to be not available in your deployed site, you
can set ``DEPLOY_DRAFTS = False`` in your configuration. This will not work if
lazily include ``nikola build`` in your ``DEPLOY_COMMANDS``.
If you set the ``status`` metadata field of a post to ``draft``, it will not be shown
in indexes and feeds. It *will* be compiled, and if you deploy it it *will* be made
available, so use with care. If you wish your drafts to be not available in your
deployed site, you can set ``DEPLOY_DRAFTS = False`` in your configuration. This will
not work if lazily include ``nikola build`` in your ``DEPLOY_COMMANDS``.

If not disabled with the ``USE_TAG_METADATA`` configuration setting, you can also add
a ``draft`` tag to your post to achieve the same result as setting ``status`` to
``draft``. This mechanism was the only one available in older versions of Nikola.

Also if a post has a date in the future, it will not be shown in indexes until
you rebuild after that date. This behavior can be disabled by setting
Expand All @@ -826,9 +837,13 @@ Generally, you want FUTURE_IS_NOW and DEPLOY_FUTURE to be the same value.
Private Posts
~~~~~~~~~~~~~

If you add a "private" tag to a post, then it will not be shown in indexes and feeds.
It *will* be compiled, and if you deploy it it *will* be made available, so it will
not generate 404s for people who had linked to it.
If you set the ``status`` metadata field of a post to ``private``, it will not be shown
in indexes and feeds. It *will* be compiled, and if you deploy it it *will* be made
available, so it will not generate 404s for people who had linked to it.

If not disabled with the ``USE_TAG_METADATA`` configuration setting, you can also add
a ``private`` tag to your post to achieve the same result as setting ``status`` to
``private``. This mechanism was the only one available in older versions of Nikola.

Queuing Posts
~~~~~~~~~~~~~
Expand Down
9 changes: 9 additions & 0 deletions nikola/conf.py.in
Expand Up @@ -1300,3 +1300,12 @@ GLOBAL_CONTEXT = {}
# GLOBAL_CONTEXT as parameter when the template is about to be
# rendered
GLOBAL_CONTEXT_FILLER = []

# If set to True, the tags 'draft', 'mathjax' and 'private' have special
# meaning. If set to False, these tags are handled like regular tags.
# USE_TAG_METADATA = True

# If set to True, a warning is issued if one of the 'draft', 'mathjax'
# and 'private' tags are found in a post. Useful for checking that
# migration was successful.
# WARN_ABOUT_TAG_METADATA = False
2 changes: 2 additions & 0 deletions nikola/nikola.py
Expand Up @@ -586,7 +586,9 @@ def __init__(self, **config):
'USE_KATEX': False,
'USE_OPEN_GRAPH': True,
'USE_SLUGIFY': True,
'USE_TAG_METADATA': True,
'TIMEZONE': 'UTC',
'WARN_ABOUT_TAG_METADATA': False,
'WRITE_TAG_CLOUD': False,
'DEPLOY_DRAFTS': True,
'DEPLOY_FUTURE': False,
Expand Down
16 changes: 13 additions & 3 deletions nikola/plugins/command/import_wordpress.py
Expand Up @@ -915,17 +915,23 @@ def import_postpage_item(self, item, wordpress_namespace, out_folder=None, attac

tags = []
categories = []
post_status = 'published'
has_math = False
if status == 'trash':
LOGGER.warn('Trashed post "{0}" will not be imported.'.format(title))
return False
elif status == 'private':
tags.append('private')
if self.site.config['USE_TAG_METADATA']:
tags.append('private')
is_draft = False
is_private = True
post_status = 'private'
elif status != 'publish':
tags.append('draft')
if self.site.config['USE_TAG_METADATA']:
tags.append('draft')
is_draft = True
is_private = False
post_status = 'draft'
else:
is_draft = False
is_private = False
Expand All @@ -943,7 +949,9 @@ def import_postpage_item(self, item, wordpress_namespace, out_folder=None, attac
tags.append(text)

if '$latex' in content:
tags.append('mathjax')
if self.site.config['USE_TAG_METADATA']:
tags.append('mathjax')
has_math = True

for i, cat in enumerate(categories[:]):
cat = self._sanitize(cat, True)
Expand Down Expand Up @@ -1010,6 +1018,8 @@ def import_postpage_item(self, item, wordpress_namespace, out_folder=None, attac
"date": post_date,
"description": description,
"tags": ','.join(tags),
"status": post_status,
"has_math": has_math,
}
meta.update(other_meta)
if self.onefile:
Expand Down
64 changes: 53 additions & 11 deletions nikola/post.py
Expand Up @@ -241,6 +241,7 @@ def __init__(

is_draft = False
is_private = False
post_status = 'published'
self._tags = {}
for lang in self.translated_to:
if isinstance(self.meta[lang]['tags'], (list, tuple, set)):
Expand All @@ -251,20 +252,50 @@ def __init__(
list(set([x.strip() for x in _tag_list])),
alg=natsort.ns.F | natsort.ns.IC)
self._tags[lang] = [t for t in self._tags[lang] if t]
if 'draft' in [_.lower() for _ in self._tags[lang]]:
is_draft = True
LOGGER.debug('The post "{0}" is a draft.'.format(self.source_path))
self._tags[lang].remove('draft')

if 'private' in self._tags[lang]:
is_private = True
LOGGER.debug('The post "{0}" is private.'.format(self.source_path))
self._tags[lang].remove('private')
status = self.meta[lang].get('status')
if status:
if status == 'published':
post_status = status
is_private = False
is_draft = False
elif status == 'private':
post_status = status
is_private = True
is_draft = False
elif status == 'draft':
post_status = status
is_private = False
is_draft = True
else:
LOGGER.warn('The post "{0}" has the unknown status "{1}". ' +
'Valid values are "published", "private" and "draft".'.format(status))

if self.config['WARN_ABOUT_TAG_METADATA']:
if 'draft' in [_.lower() for _ in self._tags[lang]]:
LOGGER.warn('The post "{0}" uses the "draft" tag.'.format(self.source_path))
if 'private' in self._tags[lang]:
LOGGER.warn('The post "{0}" uses the "private" tag.'.format(self.source_path))
if 'mathjax' in self._tags[lang]:
LOGGER.warn('The post "{0}" uses the "mathjax" tag.'.format(self.source_path))
if self.config['USE_TAG_METADATA']:
if 'draft' in [_.lower() for _ in self._tags[lang]]:
is_draft = True
LOGGER.debug('The post "{0}" is a draft.'.format(self.source_path))
self._tags[lang].remove('draft')
post_status = 'draft'

if 'private' in self._tags[lang]:
is_private = True
LOGGER.debug('The post "{0}" is private.'.format(self.source_path))
self._tags[lang].remove('private')
post_status = 'private'

# While draft comes from the tags, it's not really a tag
self.is_draft = is_draft
self.is_private = is_private
self.is_post = use_in_feeds
self.post_status = post_status
self.use_in_feeds = use_in_feeds and not is_draft and not is_private \
and not self.publish_later

Expand Down Expand Up @@ -312,14 +343,25 @@ def _has_pretty_url(self, lang):

@property
def is_mathjax(self):
"""Return True if this post has the mathjax tag in the current language or is a python notebook."""
"""Return True if this post has has_math set to True, hast set the
mathjax tag in the current language or is a python notebook."""
if self.compiler.name == 'ipynb':
return True
lang = nikola.utils.LocaleBorg().current_lang
if self.is_translation_available(lang):
return 'mathjax' in self.tags_for_language(lang)
if self.meta[lang].get('has_math'):
return True
if self.config['USE_TAG_METADATA']:
has_math = 'mathjax' in self.tags_for_language(lang)
return has_math
# If it has math in ANY other language, enable it. Better inefficient than broken.
return 'mathjax' in self.alltags
for lang in self.translated_to:
if self.meta[lang].get('has_math'):
return True
if self.config['USE_TAG_METADATA']:
has_math = 'mathjax' in self.alltags
return has_math
return False

@property
def alltags(self):
Expand Down

0 comments on commit 1572a4e

Please sign in to comment.