From d8b539a9f2a18369400444ada72dff4a83595bab Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 3 Oct 2015 12:41:21 +0300 Subject: Use jinja2 for templates --- jtlib/app.py | 7 +++++++ jtlib/plugins/new_person_plugin.py | 15 ++++++--------- jtlib/plugins/new_plugin.py | 5 +++-- jtlib/plugins/new_topic_plugin.py | 7 +++++-- jtlib/templates/new-note.j2 | 4 ++++ jtlib/templates/new-note.tmpl | 5 ----- jtlib/templates/new_person.j2 | 3 +++ jtlib/templates/new_person.tmpl | 3 --- jtlib/templates/new_topic.j2 | 3 +++ jtlib/templates/new_topic.tmpl | 3 --- 10 files changed, 31 insertions(+), 24 deletions(-) create mode 100644 jtlib/templates/new-note.j2 delete mode 100644 jtlib/templates/new-note.tmpl create mode 100644 jtlib/templates/new_person.j2 delete mode 100644 jtlib/templates/new_person.tmpl create mode 100644 jtlib/templates/new_topic.j2 delete mode 100644 jtlib/templates/new_topic.tmpl 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.j2 b/jtlib/templates/new_person.j2 new file mode 100644 index 0000000..483215b --- /dev/null +++ b/jtlib/templates/new_person.j2 @@ -0,0 +1,3 @@ +[[!meta title="{{ name }}"]] + +[[!inline archive=yes pages="link(.)"]] diff --git a/jtlib/templates/new_person.tmpl b/jtlib/templates/new_person.tmpl deleted file mode 100644 index 86d5162..0000000 --- a/jtlib/templates/new_person.tmpl +++ /dev/null @@ -1,3 +0,0 @@ -[[!meta title="%(name)s"]] - -[[!inline archive=yes pages="link(.)"]] diff --git a/jtlib/templates/new_topic.j2 b/jtlib/templates/new_topic.j2 new file mode 100644 index 0000000..7aa0b15 --- /dev/null +++ b/jtlib/templates/new_topic.j2 @@ -0,0 +1,3 @@ +[[!meta title="{{ title }}"]] + +[[!inline pages="link(.)" archive=yes reverse=yes trail=yes]] diff --git a/jtlib/templates/new_topic.tmpl b/jtlib/templates/new_topic.tmpl deleted file mode 100644 index 5321b7f..0000000 --- a/jtlib/templates/new_topic.tmpl +++ /dev/null @@ -1,3 +0,0 @@ -[[!meta title="%(title)s"]] - -[[!inline pages="link(.)" archive=yes reverse=yes trail=yes]] -- cgit v1.2.1