From cf5270ab70e0753c3fa2c016233963f287253794 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Sun, 29 Jan 2017 22:18:25 +0100 Subject: Pass search results to a template --- lesana/command.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lesana/command.py b/lesana/command.py index 6d31b34..dd1864a 100644 --- a/lesana/command.py +++ b/lesana/command.py @@ -1,8 +1,10 @@ import logging import os import subprocess +import sys import guacamole +import jinja2 from . import Collection, Entry @@ -175,11 +177,27 @@ class Search(guacamole.Command): pagesize = ctx.args.pagesize or 12 collection = Collection(ctx.args.collection) collection.start_search(' '.join(ctx.args.query)) - # TODO: pass the entries to a proper template - for entry in collection.get_search_results( - offset, - pagesize): - print(entry) + results = collection.get_search_results( + offset, + pagesize) + if ctx.args.template: + env = jinja2.Environment( + loader=jinja2.FileSystemLoader( + searchpath='.', + followlinks=True, + ), + # TODO: add autoescaping settings + ) + try: + template = env.get_template(ctx.args.template) + except jinja2.exceptions.TemplateNotFound as e: + logging.error("Could not find template: {}".format(e)) + sys.exit(1) + else: + print(template.render(entries=results)) + else: + for entry in results: + print(entry) class Init(guacamole.Command): -- cgit v1.2.3