aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-01-29 22:18:25 +0100
committerElena ``of Valhalla'' Grandi <valhalla@trueelena.org>2017-01-29 22:18:25 +0100
commitcf5270ab70e0753c3fa2c016233963f287253794 (patch)
tree15e325da265c23b5941978d7b9d391179723f69f
parent8a293151ae1939e84ff93b98c96954fa490aff25 (diff)
Pass search results to a template
-rw-r--r--lesana/command.py28
1 files 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):