Repository: getnikola/nikola

Issue:
post-list shortcode doesn't work with Markdown
The manual says:

```
Using shortcode syntax (for other compilers):

{{% post-list stop=5 %}}{{% /post-list %}}
```

But when I put it in a Markdown file I get:

```
[2018-04-14T17:27:10Z] ERROR: Nikola: Unknown shortcode post-list (started at line 1, column 1)
```

In a `.rst` file it works (the `.. post-link` syntax. Maybe the shortcode doesn't work there either)


Discussion:
Works for me. Did you do anything to disable the reST plugins?
I can reproduce with `*.rst` and `*.txt` removed from POSTS and PAGES. Due to internal technical reasons, you can’t do that if you want to use certain shortcodes. I added a few warnings to the manual.

Feel free to reopen if you can reproduce after adding a `*.rst` entry to POSTS/PAGES.
You're right.  Adding`*.rst` to POSTS/PAGES makes it work.  Thanks.

This is a known bug, and the fix is to make the shortcode independent from the reSt plugin.

Resolving patch:
diff --git a/nikola/plugins/compile/rest/post_list.py b/nikola/plugins/compile/rest/post_list.py
--- a/nikola/plugins/compile/rest/post_list.py
+++ b/nikola/plugins/compile/rest/post_list.py
@@ -23,21 +23,15 @@
 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 # OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
 """Post list directive for reStructuredText."""
 
-
-import os
-import uuid
-import natsort
-import operator
-
 from docutils import nodes
 from docutils.parsers.rst import Directive, directives
 
 from nikola import utils
 from nikola.plugin_categories import RestExtension
-from nikola.packages.datecond import date_in_range
+
+# from nikola.plugins.shortcode.post_list import _do_post_list
 
 # WARNING: the directive name is post-list
 #          (with a DASH instead of an UNDERSCORE)
@@ -51,91 +45,13 @@ class Plugin(RestExtension):
     def set_site(self, site):
         """Set Nikola site."""
         self.site = site
-        self.site.register_shortcode('post-list', _do_post_list)
-        directives.register_directive('post-list', PostList)
-        PostList.site = site
+        directives.register_directive('post-list', PostListDirective)
+        PostListDirective.site = site
         return super(Plugin, self).set_site(site)
 
 
-class PostList(Directive):
-    """Provide a reStructuredText directive to create a list of posts.
-
-    Post List
-    =========
-    :Directive Arguments: None.
-    :Directive Options: lang, start, stop, reverse, sort, date, tags, categories, sections, slugs, post_type, template, id
-    :Directive Content: None.
-
-    The posts appearing in the list can be filtered by options.
-    *List slicing* is provided with the *start*, *stop* and *reverse* options.
-
-    The following not required options are recognized:
-
-    ``start`` : integer
-        The index of the first post to show.
-        A negative value like ``-3`` will show the *last* three posts in the
-        post-list.
-        Defaults to None.
-
-    ``stop`` : integer
-        The index of the last post to show.
-        A value negative value like ``-1`` will show every post, but not the
-        *last* in the post-list.
-        Defaults to None.
-
-    ``reverse`` : flag
-        Reverse the order of the post-list.
-        Defaults is to not reverse the order of posts.
-
-    ``sort`` : string
-        Sort post list by one of each post's attributes, usually ``title`` or a
-        custom ``priority``.  Defaults to None (chronological sorting).
-
-    ``date`` : string
-        Show posts that match date range specified by this option. Format:
-
-        * comma-separated clauses (AND)
-        * clause: attribute comparison_operator value (spaces optional)
-          * attribute: year, month, day, hour, month, second, weekday, isoweekday; or empty for full datetime
-          * comparison_operator: == != <= >= < >
-          * value: integer, 'now' or dateutil-compatible date input
-
-    ``tags`` : string [, string...]
-        Filter posts to show only posts having at least one of the ``tags``.
-        Defaults to None.
-
-    ``require_all_tags`` : flag
-        Change tag filter behaviour to show only posts that have all specified ``tags``.
-        Defaults to False.
-
-    ``categories`` : string [, string...]
-        Filter posts to show only posts having one of the ``categories``.
-        Defaults to None.
-
-    ``sections`` : string [, string...]
-        Filter posts to show only posts having one of the ``sections``.
-        Defaults to None.
-
-    ``slugs`` : string [, string...]
-        Filter posts to show only posts having at least one of the ``slugs``.
-        Defaults to None.
-
-    ``post_type`` (or ``type``) : string
-        Show only ``posts``, ``pages`` or ``all``.
-        Replaces ``all``. Defaults to ``posts``.
-
-    ``lang`` : string
-        The language of post *titles* and *links*.
-        Defaults to default language.
-
-    ``template`` : string
-        The name of an alternative template to render the post-list.
-        Defaults to ``post_list_directive.tmpl``
-
-    ``id`` : string
-        A manual id for the post list.
-        Defaults to a random name composed by 'post_list_' + uuid.uuid4().hex.
-    """
+class PostListDirective(Directive):
+    """Provide a reStructuredText directive to create a list of posts."""
 
     option_spec = {
         'start': int,
@@ -173,134 +89,30 @@ def run(self):
         date = self.options.get('date')
         filename = self.state.document.settings._nikola_source_path
 
-        output, deps = _do_post_list(start, stop, reverse, tags, require_all_tags, categories, sections, slugs, post_type, type,
-                                     lang, template, sort, state=self.state, site=self.site, date=date, filename=filename)
-        self.state.document.settings.record_dependencies.add("####MAGIC####TIMELINE")
+        output, deps = self.site.plugin_manager.getPluginByName(
+            'post_list', 'ShortcodePlugin').plugin_object.handler(
+                start,
+                stop,
+                reverse,
+                tags,
+                require_all_tags,
+                categories,
+                sections,
+                slugs,
+                post_type,
+                type,
+                lang,
+                template,
+                sort,
+                state=self.state,
+                site=self.site,
+                date=date,
+                filename=filename)
+        self.state.document.settings.record_dependencies.add(
+            "####MAGIC####TIMELINE")
         for d in deps:
             self.state.document.settings.record_dependencies.add(d)
         if output:
             return [nodes.raw('', output, format='html')]
         else:
             return []
-
-
-def _do_post_list(start=None, stop=None, reverse=False, tags=None, require_all_tags=False, categories=None,
-                  sections=None, slugs=None, post_type='post', type=False,
-                  lang=None, template='post_list_directive.tmpl', sort=None,
-                  id=None, data=None, state=None, site=None, date=None, filename=None, post=None):
-    if lang is None:
-        lang = utils.LocaleBorg().current_lang
-    if site.invariant:  # for testing purposes
-        post_list_id = id or 'post_list_' + 'fixedvaluethatisnotauuid'
-    else:
-        post_list_id = id or 'post_list_' + uuid.uuid4().hex
-
-    # Get post from filename if available
-    if filename:
-        self_post = site.post_per_input_file.get(filename)
-    else:
-        self_post = None
-
-    if self_post:
-        self_post.register_depfile("####MAGIC####TIMELINE", lang=lang)
-
-    # If we get strings for start/stop, make them integers
-    if start is not None:
-        start = int(start)
-    if stop is not None:
-        stop = int(stop)
-
-    # Parse tags/categories/sections/slugs (input is strings)
-    categories = [c.strip().lower() for c in categories.split(',')] if categories else []
-    sections = [s.strip().lower() for s in sections.split(',')] if sections else []
-    slugs = [s.strip() for s in slugs.split(',')] if slugs else []
-
-    filtered_timeline = []
-    posts = []
-    step = -1 if reverse is None else None
-
-    if type is not False:
-        post_type = type
-
-    if post_type == 'page' or post_type == 'pages':
-        timeline = [p for p in site.timeline if not p.use_in_feeds]
-    elif post_type == 'all':
-        timeline = [p for p in site.timeline]
-    else:  # post
-        timeline = [p for p in site.timeline if p.use_in_feeds]
-
-    # self_post should be removed from timeline because this is redundant
-    timeline = [p for p in timeline if p.source_path != filename]
-
-    if categories:
-        timeline = [p for p in timeline if p.meta('category', lang=lang).lower() in categories]
-
-    if sections:
-        timeline = [p for p in timeline if p.section_name(lang).lower() in sections]
-
-    if tags:
-        tags = {t.strip().lower() for t in tags.split(',')}
-        if require_all_tags:
-            compare = set.issubset
-        else:
-            compare = operator.and_
-        for post in timeline:
-            post_tags = {t.lower() for t in post.tags}
-            if compare(tags, post_tags):
-                filtered_timeline.append(post)
-    else:
-        filtered_timeline = timeline
-
-    if sort:
-        filtered_timeline = natsort.natsorted(filtered_timeline, key=lambda post: post.meta[lang][sort], alg=natsort.ns.F | natsort.ns.IC)
-
-    if date:
-        _now = utils.current_time()
-        filtered_timeline = [p for p in filtered_timeline if date_in_range(utils.html_unescape(date), p.date, now=_now)]
-
-    for post in filtered_timeline[start:stop:step]:
-        if slugs:
-            cont = True
-            for slug in slugs:
-                if slug == post.meta('slug'):
-                    cont = False
-
-            if cont:
-                continue
-
-        bp = post.translated_base_path(lang)
-        if os.path.exists(bp) and state:
-            state.document.settings.record_dependencies.add(bp)
-        elif os.path.exists(bp) and self_post:
-            self_post.register_depfile(bp, lang=lang)
-
-        posts += [post]
-
-    if not posts:
-        return '', []
-
-    template_deps = site.template_system.template_deps(template)
-    if state:
-        # Register template as a dependency (Issue #2391)
-        for d in template_deps:
-            state.document.settings.record_dependencies.add(d)
-    elif self_post:
-        for d in template_deps:
-            self_post.register_depfile(d, lang=lang)
-
-    template_data = {
-        'lang': lang,
-        'posts': posts,
-        # Need to provide str, not TranslatableSetting (Issue #2104)
-        'date_format': site.GLOBAL_CONTEXT.get('date_format')[lang],
-        'post_list_id': post_list_id,
-        'messages': site.MESSAGES,
-        '_link': site.link,
-    }
-    output = site.template_system.render_template(
-        template, None, template_data)
-    return output, template_deps
-
-
-# Request file name from shortcode (Issue #2412)
-_do_post_list.nikola_shortcode_pass_filename = True
diff --git a/nikola/plugins/shortcode/post_list.py b/nikola/plugins/shortcode/post_list.py
new file mode 100644
--- /dev/null
+++ b/nikola/plugins/shortcode/post_list.py
@@ -0,0 +1,243 @@
+# -*- coding: utf-8 -*-
+
+# Copyright © 2013-2018 Udo Spallek, Roberto Alsina and others.
+
+# Permission is hereby granted, free of charge, to any
+# person obtaining a copy of this software and associated
+# documentation files (the "Software"), to deal in the
+# Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the
+# Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice
+# shall be included in all copies or substantial portions of
+# the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+"""Post list shortcode."""
+
+
+import operator
+import os
+import uuid
+
+import natsort
+
+from nikola import utils
+from nikola.packages.datecond import date_in_range
+from nikola.plugin_categories import ShortcodePlugin
+
+
+class PostListShortcode(ShortcodePlugin):
+    """Provide a shortcode to create a list of posts.
+
+    Post List
+    =========
+    :Directive Arguments: None.
+    :Directive Options: lang, start, stop, reverse, sort, date, tags, categories, sections, slugs, post_type, template, id
+    :Directive Content: None.
+
+    The posts appearing in the list can be filtered by options.
+    *List slicing* is provided with the *start*, *stop* and *reverse* options.
+
+    The following not required options are recognized:
+
+    ``start`` : integer
+        The index of the first post to show.
+        A negative value like ``-3`` will show the *last* three posts in the
+        post-list.
+        Defaults to None.
+
+    ``stop`` : integer
+        The index of the last post to show.
+        A value negative value like ``-1`` will show every post, but not the
+        *last* in the post-list.
+        Defaults to None.
+
+    ``reverse`` : flag
+        Reverse the order of the post-list.
+        Defaults is to not reverse the order of posts.
+
+    ``sort`` : string
+        Sort post list by one of each post's attributes, usually ``title`` or a
+        custom ``priority``.  Defaults to None (chronological sorting).
+
+    ``date`` : string
+        Show posts that match date range specified by this option. Format:
+
+        * comma-separated clauses (AND)
+        * clause: attribute comparison_operator value (spaces optional)
+          * attribute: year, month, day, hour, month, second, weekday, isoweekday; or empty for full datetime
+          * comparison_operator: == != <= >= < >
+          * value: integer, 'now' or dateutil-compatible date input
+
+    ``tags`` : string [, string...]
+        Filter posts to show only posts having at least one of the ``tags``.
+        Defaults to None.
+
+    ``require_all_tags`` : flag
+        Change tag filter behaviour to show only posts that have all specified ``tags``.
+        Defaults to False.
+
+    ``categories`` : string [, string...]
+        Filter posts to show only posts having one of the ``categories``.
+        Defaults to None.
+
+    ``sections`` : string [, string...]
+        Filter posts to show only posts having one of the ``sections``.
+        Defaults to None.
+
+    ``slugs`` : string [, string...]
+        Filter posts to show only posts having at least one of the ``slugs``.
+        Defaults to None.
+
+    ``post_type`` (or ``type``) : string
+        Show only ``posts``, ``pages`` or ``all``.
+        Replaces ``all``. Defaults to ``posts``.
+
+    ``lang`` : string
+        The language of post *titles* and *links*.
+        Defaults to default language.
+
+    ``template`` : string
+        The name of an alternative template to render the post-list.
+        Defaults to ``post_list_directive.tmpl``
+
+    ``id`` : string
+        A manual id for the post list.
+        Defaults to a random name composed by 'post_list_' + uuid.uuid4().hex.
+    """
+
+    name = "post_list"
+
+    def handler(self, start=None, stop=None, reverse=False, tags=None, require_all_tags=False, categories=None,
+                sections=None, slugs=None, post_type='post', type=False,
+                lang=None, template='post_list_directive.tmpl', sort=None,
+                id=None, data=None, state=None, site=None, date=None, filename=None, post=None):
+        """Generate HTML for post-list."""
+        if lang is None:
+            lang = utils.LocaleBorg().current_lang
+        if site.invariant:  # for testing purposes
+            post_list_id = id or 'post_list_' + 'fixedvaluethatisnotauuid'
+        else:
+            post_list_id = id or 'post_list_' + uuid.uuid4().hex
+
+        # Get post from filename if available
+        if filename:
+            self_post = site.post_per_input_file.get(filename)
+        else:
+            self_post = None
+
+        if self_post:
+            self_post.register_depfile("####MAGIC####TIMELINE", lang=lang)
+
+        # If we get strings for start/stop, make them integers
+        if start is not None:
+            start = int(start)
+        if stop is not None:
+            stop = int(stop)
+
+        # Parse tags/categories/sections/slugs (input is strings)
+        categories = [c.strip().lower() for c in categories.split(',')] if categories else []
+        sections = [s.strip().lower() for s in sections.split(',')] if sections else []
+        slugs = [s.strip() for s in slugs.split(',')] if slugs else []
+
+        filtered_timeline = []
+        posts = []
+        step = -1 if reverse is None else None
+
+        if type is not False:
+            post_type = type
+
+        if post_type == 'page' or post_type == 'pages':
+            timeline = [p for p in site.timeline if not p.use_in_feeds]
+        elif post_type == 'all':
+            timeline = [p for p in site.timeline]
+        else:  # post
+            timeline = [p for p in site.timeline if p.use_in_feeds]
+
+        # self_post should be removed from timeline because this is redundant
+        timeline = [p for p in timeline if p.source_path != filename]
+
+        if categories:
+            timeline = [p for p in timeline if p.meta('category', lang=lang).lower() in categories]
+
+        if sections:
+            timeline = [p for p in timeline if p.section_name(lang).lower() in sections]
+
+        if tags:
+            tags = {t.strip().lower() for t in tags.split(',')}
+            if require_all_tags:
+                compare = set.issubset
+            else:
+                compare = operator.and_
+            for post in timeline:
+                post_tags = {t.lower() for t in post.tags}
+                if compare(tags, post_tags):
+                    filtered_timeline.append(post)
+        else:
+            filtered_timeline = timeline
+
+        if sort:
+            filtered_timeline = natsort.natsorted(filtered_timeline, key=lambda post: post.meta[lang][sort], alg=natsort.ns.F | natsort.ns.IC)
+
+        if date:
+            _now = utils.current_time()
+            filtered_timeline = [p for p in filtered_timeline if date_in_range(utils.html_unescape(date), p.date, now=_now)]
+
+        for post in filtered_timeline[start:stop:step]:
+            if slugs:
+                cont = True
+                for slug in slugs:
+                    if slug == post.meta('slug'):
+                        cont = False
+
+                if cont:
+                    continue
+
+            bp = post.translated_base_path(lang)
+            if os.path.exists(bp) and state:
+                state.document.settings.record_dependencies.add(bp)
+            elif os.path.exists(bp) and self_post:
+                self_post.register_depfile(bp, lang=lang)
+
+            posts += [post]
+
+        if not posts:
+            return '', []
+
+        template_deps = site.template_system.template_deps(template)
+        if state:
+            # Register template as a dependency (Issue #2391)
+            for d in template_deps:
+                state.document.settings.record_dependencies.add(d)
+        elif self_post:
+            for d in template_deps:
+                self_post.register_depfile(d, lang=lang)
+
+        template_data = {
+            'lang': lang,
+            'posts': posts,
+            # Need to provide str, not TranslatableSetting (Issue #2104)
+            'date_format': site.GLOBAL_CONTEXT.get('date_format')[lang],
+            'post_list_id': post_list_id,
+            'messages': site.MESSAGES,
+            '_link': site.link,
+        }
+        output = site.template_system.render_template(
+            template, None, template_data)
+        return output, template_deps
+
+
+# Request file name from shortcode (Issue #2412)
+PostListShortcode.handler.nikola_shortcode_pass_filename = True


Test patch: