# HG changeset patch # User Ludovic Chabant # Date 1524544647 25200 # Node ID 986ecdaa2a36f5b117dca138dd1bc4933db87b1e # Parent fe0af94ca757dc2ae8c2f3d13a8cd113d4193382 url: New `url` command to get the URL of a page from its path. diff -r fe0af94ca757 -r 986ecdaa2a36 piecrust/commands/builtin/info.py --- a/piecrust/commands/builtin/info.py Mon Apr 23 21:36:22 2018 -0700 +++ b/piecrust/commands/builtin/info.py Mon Apr 23 21:37:27 2018 -0700 @@ -1,3 +1,4 @@ +import os import os.path import logging from piecrust.commands.base import ( @@ -233,3 +234,62 @@ if pattern is None or fnmatch.fnmatch(name, pattern): logger.info(item.spec) + +class UrlCommand(ChefCommand): + def __init__(self): + super().__init__() + self.name = 'url' + self.description = "Gets the URL to a given page." + + def setupParser(self, parser, app): + parser.add_argument( + 'path', + help="The path to the page.") + parser.add_argument( + '-f', '--func', + dest='tpl_func', + action='store_true', + help="Return the template function call instead of the URL.") + + def run(self, ctx): + from piecrust.sources.fs import FSContentSourceBase + from piecrust.routing import RouteParameter + + # Find which source this page might belong to. + full_path = os.path.join(ctx.app.root_dir, ctx.args.path) + for src in ctx.app.sources: + if not isinstance(src, FSContentSourceBase): + continue + + if full_path.startswith(src.fs_endpoint_path + os.sep): + parent_src = src + break + else: + raise Exception("Can't find which source this page belongs to.") + + route = ctx.app.getSourceRoute(parent_src.name) + content_item = parent_src.findContentFromSpec(full_path) + route_params = content_item.metadata['route_params'] + + if ctx.args.tpl_func: + if not route.func_name: + raise Exception("Source '%s' doesn't have a route with " + "a template function name defined." % + parent_src.name) + + url = '%s(' % route.func_name + for i, p in enumerate(route.uri_params): + if i > 0: + url += ', ' + pinfo = route.getParameter(p) + if pinfo.param_type == RouteParameter.TYPE_INT2: + url += '%02d' % route_params[p] + elif pinfo.param_type == RouteParameter.TYPE_INT4: + url += '%04d' % route_params[p] + else: + url += str(route_params[p]) + url += ')' + logger.info(url) + else: + url = route.getUri(route_params) + logger.info(url) diff -r fe0af94ca757 -r 986ecdaa2a36 piecrust/plugins/builtin.py --- a/piecrust/plugins/builtin.py Mon Apr 23 21:36:22 2018 -0700 +++ b/piecrust/plugins/builtin.py Mon Apr 23 21:37:27 2018 -0700 @@ -12,7 +12,7 @@ from piecrust.commands.builtin.info import ( RootCommand, ShowConfigCommand, FindCommand, ShowSourcesCommand, ShowRoutesCommand, - ShowPathsCommand) + ShowPathsCommand, UrlCommand) from piecrust.commands.builtin.plugins import PluginsCommand from piecrust.commands.builtin.publishing import PublishCommand from piecrust.commands.builtin.scaffolding import PrepareCommand @@ -34,6 +34,7 @@ ShowSourcesCommand(), ShowRoutesCommand(), ShowPathsCommand(), + UrlCommand(), ThemesCommand(), PluginsCommand(), BakeCommand(),