summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2015-10-03 12:41:21 +0300
committerLars Wirzenius <liw@liw.fi>2015-10-03 13:08:17 +0300
commitd8b539a9f2a18369400444ada72dff4a83595bab (patch)
tree56d920df78424dd5ed486dd2bc5a240cfa0a39d0
parent7cb2a4b266c1d140ebd3c9198ca5754acda576ae (diff)
downloadjt-d8b539a9f2a18369400444ada72dff4a83595bab.tar.gz
Use jinja2 for templates
-rw-r--r--jtlib/app.py7
-rw-r--r--jtlib/plugins/new_person_plugin.py15
-rw-r--r--jtlib/plugins/new_plugin.py5
-rw-r--r--jtlib/plugins/new_topic_plugin.py7
-rw-r--r--jtlib/templates/new-note.j24
-rw-r--r--jtlib/templates/new-note.tmpl5
-rw-r--r--jtlib/templates/new_person.j2 (renamed from jtlib/templates/new_person.tmpl)2
-rw-r--r--jtlib/templates/new_topic.j2 (renamed from jtlib/templates/new_topic.tmpl)2
8 files changed, 27 insertions, 20 deletions
diff --git a/jtlib/app.py b/jtlib/app.py
index 4b0c54e..6e33646 100644
--- a/jtlib/app.py
+++ b/jtlib/app.py
@@ -18,6 +18,8 @@ import cliapp
import os
import time
+import jinja2
+
import jtlib
@@ -106,3 +108,8 @@ class JournalTool(cliapp.Application):
if os.path.exists(filename):
with open(filename) as f:
return f.read()
+
+ def render_template(self, basename, vars):
+ text = self.read_template(basename)
+ template = jinja2.Template(text)
+ return template.render(**vars)
diff --git a/jtlib/plugins/new_person_plugin.py b/jtlib/plugins/new_person_plugin.py
index 050e97a..a7cc76a 100644
--- a/jtlib/plugins/new_person_plugin.py
+++ b/jtlib/plugins/new_person_plugin.py
@@ -53,13 +53,10 @@ class NewPersonCommand(cliapp.Plugin):
if os.path.exists(pathname):
raise cliapp.AppException('File %s already exists' % pathname)
- template = self.app.read_template('new_person.tmpl')
+ vars = {
+ 'name': name,
+ 'basename': basename,
+ }
+ rendered_template = self.app.render_template('new_person.j2', vars)
with open(pathname, 'w') as f:
- f.write(
- template %
- {
- 'name': name,
- 'basename': basename,
- })
-
-
+ f.write(rendered_template)
diff --git a/jtlib/plugins/new_plugin.py b/jtlib/plugins/new_plugin.py
index 8134fa5..25facf8 100644
--- a/jtlib/plugins/new_plugin.py
+++ b/jtlib/plugins/new_plugin.py
@@ -42,8 +42,9 @@ class NewCommand(cliapp.Plugin):
drafts_dir = jtlib.DraftsDirectory(self.app.drafts_dir())
drafts_dir.create_if_missing()
- draft_id = drafts_dir.create_draft(
- self._get_new_note_template() % values)
+
+ rendered_template = self.app.render_template('new-note.j2', values)
+ draft_id = drafts_dir.create_draft(rendered_template)
self.app.edit_file(drafts_dir.get_draft_pathname(draft_id))
def _check_args_and_settings(self, args):
diff --git a/jtlib/plugins/new_topic_plugin.py b/jtlib/plugins/new_topic_plugin.py
index 4f44f2b..1114bb4 100644
--- a/jtlib/plugins/new_topic_plugin.py
+++ b/jtlib/plugins/new_topic_plugin.py
@@ -49,6 +49,9 @@ class NewTopicCommand(cliapp.Plugin):
if not os.path.exists(dirname):
os.makedirs(dirname)
- template = self.app.read_template('new_topic.tmpl')
+ vars = {
+ 'title': title,
+ }
+ rendered_template = self.app.render_template('new_topic.j2', vars)
with open(pathname, 'w') as f:
- f.write(template % {'title': title})
+ f.write(rendered_template)
diff --git a/jtlib/templates/new-note.j2 b/jtlib/templates/new-note.j2
new file mode 100644
index 0000000..9e4c98d
--- /dev/null
+++ b/jtlib/templates/new-note.j2
@@ -0,0 +1,4 @@
+[[!meta title="{{ title }}"]]
+[[!tag ]]
+[[!meta date="{{ date }}"]]
+{{ topiclink }}
diff --git a/jtlib/templates/new-note.tmpl b/jtlib/templates/new-note.tmpl
deleted file mode 100644
index 0db1798..0000000
--- a/jtlib/templates/new-note.tmpl
+++ /dev/null
@@ -1,5 +0,0 @@
-[[!meta title="%(title)s"]]
-[[!tag ]]
-[[!meta date="%(date)s"]]
-%(topiclink)s
-
diff --git a/jtlib/templates/new_person.tmpl b/jtlib/templates/new_person.j2
index 86d5162..483215b 100644
--- a/jtlib/templates/new_person.tmpl
+++ b/jtlib/templates/new_person.j2
@@ -1,3 +1,3 @@
-[[!meta title="%(name)s"]]
+[[!meta title="{{ name }}"]]
[[!inline archive=yes pages="link(.)"]]
diff --git a/jtlib/templates/new_topic.tmpl b/jtlib/templates/new_topic.j2
index 5321b7f..7aa0b15 100644
--- a/jtlib/templates/new_topic.tmpl
+++ b/jtlib/templates/new_topic.j2
@@ -1,3 +1,3 @@
-[[!meta title="%(title)s"]]
+[[!meta title="{{ title }}"]]
[[!inline pages="link(.)" archive=yes reverse=yes trail=yes]]