diff piecrust/commands/builtin/info.py @ 1099:07c23be08029

help: Add new help topics on routes.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 17 Feb 2018 11:53:03 -0800
parents 58ae026b4c31
children 6462c4a87532
line wrap: on
line diff
--- a/piecrust/commands/builtin/info.py	Sat Feb 17 11:52:31 2018 -0800
+++ b/piecrust/commands/builtin/info.py	Sat Feb 17 11:53:03 2018 -0800
@@ -1,6 +1,7 @@
 import os.path
 import logging
-from piecrust.commands.base import ChefCommand
+from piecrust.commands.base import (
+    ChefCommand, ChefCommandExtension, _ResourcesHelpTopics)
 
 
 logger = logging.getLogger(__name__)
@@ -81,6 +82,9 @@
     def setupParser(self, parser, app):
         pass
 
+    def provideExtensions(self):
+        return [RoutesHelpTopic()]
+
     def run(self, ctx):
         for route in ctx.app.routes:
             logger.info("%s:" % route.uri_pattern)
@@ -91,6 +95,50 @@
                 ', '.join(route.uri_params)))
 
 
+class RoutesHelpTopic(ChefCommandExtension, _ResourcesHelpTopics):
+    command_name = 'help'
+
+    def getHelpTopics(self):
+        return [('routes_config',
+                 "Specifying URL routes for your site's content."),
+                ('route_params',
+                 "Show the available route parameters.")]
+
+    def getHelpTopic(self, topic, app):
+        if topic != 'route_params':
+            return _ResourcesHelpTopics.getHelpTopic(self, topic, app)
+
+        import textwrap
+
+        help_txt = (
+            textwrap.fill(
+                "Route parameters can be used as placeholders when specifying "
+                "route URL patterns in your site configuration. See "
+                "`chef help routes_config` for more information.") +
+            "\n\n")
+        if app.root_dir is None:
+            help_txt += textwrap.fill(
+                "Running this help command in a PieCrust website would show "
+                "the route parameters available for your site's sources. "
+                "However, no PieCrust website has been found in the current "
+                "working directory. ")
+            return help_txt
+
+        srcs_by_types = {}
+        for src in app.sources:
+            srcs_by_types.setdefault(src.SOURCE_NAME, []).append(src)
+
+        for type_name, srcs in srcs_by_types.items():
+            help_txt += textwrap.fill(
+                "Route parameters for '%s' sources (%s):" % (
+                    type_name, ', '.join([s.name for s in srcs])))
+            help_txt += "\n"
+            for rp in srcs[0].getSupportedRouteParameters():
+                help_txt += " - %s\n" % rp.param_name
+            help_txt += "\n"
+        return help_txt
+
+
 class ShowPathsCommand(ChefCommand):
     def __init__(self):
         super(ShowPathsCommand, self).__init__()