Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #3066 from getnikola/implement-2761
[WIP] Change draft, private and mathjax to own metadata (#2761)
  • Loading branch information
Kwpolska committed May 1, 2018
2 parents 0d47aaf + 51f3e7f commit af3c6a1
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 40 deletions.
6 changes: 6 additions & 0 deletions CHANGES.txt
Expand Up @@ -7,6 +7,9 @@ Important compatibility changes
* Rename ``crumbs.tmpl`` to ``ui_helper.tmpl`` and the breadcrumbs
``bar`` function to ``breadcrumbs`` (your templates may need
changing as well)
* Rename ``post.is_mathjax`` to ``post.has_math``. Themes using
``post.is_mathjax`` must be updated; it is recommended that they are
changed to use ``math_helper.tmpl``.
* Reading reST docinfo metadata, including first heading as title,
requires ``USE_REST_DOCINFO_METADATA`` now (Issue #2987)
* RSS feeds might have changed their places due to ``RSS_PATH``
Expand All @@ -16,6 +19,9 @@ Important compatibility changes
Features
--------

* Tags ``draft``, ``private`` and ``mathjax`` are no longer treated
special if ``USE_TAG_METADATA`` is set to ``False`` (default for
new sites) (Issue #2761)
* Produce a better error message when a template referenced in another
template is missing (Issue #3055)
* Support captioned images and image ordering in galleries (Issue #3017)
Expand Down
37 changes: 21 additions & 16 deletions docs/manual.rst
Expand Up @@ -3,7 +3,8 @@
.. date: 2012-03-30 23:00:00 UTC-03:00
.. link:
.. description:
.. tags: mathjax
.. tags:
.. has_math: true
.. author: The Nikola Team
The Nikola Handbook
Expand Down Expand Up @@ -299,8 +300,14 @@ date
Adding a timezone is recommended. (required for posts)

tags
Comma-separated tags of the post. Some tags have special meaning, including
``draft``, ``private``, ``mathjax``
Comma-separated tags of the post.

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

has_math
If set to ``true`` or ``yes``, MathJax resp. KaTeX support is enabled
for this post.

category
Like tags, except each post can have only one, and they usually have
Expand Down Expand Up @@ -809,11 +816,11 @@ 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``.

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 +833,9 @@ 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.

Queuing Posts
~~~~~~~~~~~~~
Expand Down Expand Up @@ -967,8 +974,6 @@ Please note that tags are case-sensitive and that you cannot have two tags that
ERROR: Nikola: Tag Nikola is used in: posts/second-post.rst
ERROR: Nikola: Tag nikola is used in: posts/1.rst
Nikola uses some tags to mark a post as “special” — those are ``draft``, ``private``, ``mathjax`` (for math support).

You can also generate a tag cloud with the `tx3_tag_cloud <https://plugins.getnikola.com/#tx3_tag_cloud>`_ plugin.

Categories
Expand Down Expand Up @@ -2290,9 +2295,9 @@ Nikola uses MathJax by default. If you want to use KaTeX (faster and prettier,
but may not support every feature yet), set ``USE_KATEX = True`` in
``conf.py``.

To use mathematics in a post, you **must** add the ``mathjax`` tag, no matter
which renderer you are using. (Exception: posts that are Jupyter Notebooks are
automatically marked as math)
To use mathematics in a post, you **must** set the ``has_math`` metadata field
to ``true``. (Exception: posts that are Jupyter Notebooks are automatically
marked as math)

.. Note to editors: the paragraph below uses U+200B, zero-width space. Don’t break it.
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 = False

# 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
8 changes: 4 additions & 4 deletions nikola/data/themes/base-jinja/templates/math_helper.tmpl
Expand Up @@ -45,25 +45,25 @@
{% endmacro %}

{% macro math_scripts_ifpost(post) %}
{% if post.is_mathjax %}
{% if post.has_math %}
{{ math_scripts() }}
{% endif %}
{% endmacro %}

{% macro math_scripts_ifposts(posts) %}
{% if posts|selectattr("is_mathjax")|list %}
{% if posts|selectattr("has_math")|list %}
{{ math_scripts() }}
{% endif %}
{% endmacro %}

{% macro math_styles_ifpost(post) %}
{% if post.is_mathjax %}
{% if post.has_math %}
{{ math_styles() }}
{% endif %}
{% endmacro %}

{% macro math_styles_ifposts(posts) %}
{% if posts|selectattr("is_mathjax")|list %}
{% if posts|selectattr("has_math")|list %}
{{ math_styles() }}
{% endif %}
{% endmacro %}
8 changes: 4 additions & 4 deletions nikola/data/themes/base/templates/math_helper.tmpl
Expand Up @@ -45,25 +45,25 @@
</%def>

<%def name="math_scripts_ifpost(post)">
%if post.is_mathjax:
%if post.has_math:
${math_scripts()}
%endif
</%def>

<%def name="math_scripts_ifposts(posts)">
%if any(post.is_mathjax for post in posts):
%if any(post.has_math for post in posts):
${math_scripts()}
%endif
</%def>

<%def name="math_styles_ifpost(post)">
%if post.is_mathjax:
%if post.has_math:
${math_styles()}
%endif
</%def>

<%def name="math_styles_ifposts(posts)">
%if any(post.is_mathjax for post in posts):
%if any(post.has_math for post in posts):
${math_styles()}
%endif
</%def>
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': True,
'WRITE_TAG_CLOUD': False,
'DEPLOY_DRAFTS': True,
'DEPLOY_FUTURE': False,
Expand Down
10 changes: 7 additions & 3 deletions nikola/plugins/command/import_wordpress.py
Expand Up @@ -915,17 +915,19 @@ def import_postpage_item(self, item, wordpress_namespace, out_folder=None, attac

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

if '$latex' in content:
tags.append('mathjax')
has_math = "yes"

for i, cat in enumerate(categories[:]):
cat = self._sanitize(cat, True)
Expand Down Expand Up @@ -1010,6 +1012,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
73 changes: 61 additions & 12 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,56 @@ 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:
is_private = False
is_draft = False
if status in ('published', 'featured'):
post_status = status
elif status == 'private':
post_status = status
is_private = True
elif status == 'draft':
post_status = status
is_draft = True
else:
LOGGER.warn(('The post "{0}" has the unknown status "{1}". ' +
'Valid values are "published", "featured", "private" and "draft".').format(self.source_path, status))

if self.config['WARN_ABOUT_TAG_METADATA']:
show_warning = False
if 'draft' in [_.lower() for _ in self._tags[lang]]:
LOGGER.warn('The post "{0}" uses the "draft" tag.'.format(self.source_path))
show_warning = True
if 'private' in self._tags[lang]:
LOGGER.warn('The post "{0}" uses the "private" tag.'.format(self.source_path))
show_warning = True
if 'mathjax' in self._tags[lang]:
LOGGER.warn('The post "{0}" uses the "mathjax" tag.'.format(self.source_path))
show_warning = True
if show_warning:
LOGGER.warn('It is suggested that you convert special tags to metadata and set '
'USE_TAG_METADATA to False. Change the WARN_ABOUT_TAG_METADATA '
'configuration to disable this warning.')
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 @@ -311,15 +348,27 @@ def _has_pretty_url(self, lang):
return self.has_pretty_url(lang)

@property
def is_mathjax(self):
"""Return True if this post has the mathjax tag in the current language or is a python notebook."""
def has_math(self):
"""Return True if this post has has_math set to True or is a python notebook.
Alternatively, it will return True if it has set the mathjax tag in the
current language and the USE_TAG_METADATA config setting is True.
"""
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') in ('true', 'True', 'yes', '1', 1, True):
return True
if self.config['USE_TAG_METADATA']:
return 'mathjax' in self.tags_for_language(lang)
# 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') in ('true', 'True', 'yes', '1', 1, True):
return True
if self.config['USE_TAG_METADATA']:
return 'mathjax' in self.alltags
return False

@property
def alltags(self):
Expand Down
2 changes: 1 addition & 1 deletion scripts/jinjify.py
Expand Up @@ -14,7 +14,7 @@
import jinja2

dumb_replacements = [
["{% if any(post.is_mathjax for post in posts) %}", '{% if posts|selectattr("is_mathjax")|list %}'],
["{% if any(post.has_math for post in posts) %}", '{% if posts|selectattr("has_math")|list %}'],
["json.dumps(title)", "title|tojson"],
["{{ parent.extra_head() }}", "{{ super() }}"],
["{{ parent.content() }}", "{{ super() }}"],
Expand Down

0 comments on commit af3c6a1

Please sign in to comment.