# Copyright 2010-2015 Lars Wirzenius # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . import cliapp import os import shutil import string import time import jtlib class NewCommand(cliapp.Plugin): def enable(self): self.app.add_subcommand('new', self.run, arg_synopsis='TITLE') def run(self, args): '''Create a new journal entry draft.''' self._check_args_and_settings(args) topic = self.app.settings['topic'] values = { 'title': args[0], 'date': time.strftime('%Y-%m-%d %H:%M', self.app.now_tuple), 'topiclink': self._get_topic_link(topic), } drafts_dir = jtlib.DraftsDirectory(self.app.drafts_dir()) drafts_dir.create_if_missing() 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): if not args: raise cliapp.AppException('Usage: journal-note new TITLE') self.app.settings.require('source') topic = self.app.settings['topic'] if topic and not self._topic_page_exists(topic): raise cliapp.AppException('Topic %s does not exist yet' % topic) def _topic_page_exists(self, topic): pathname = os.path.join(self.app.settings['source'], topic + '.mdwn') return os.path.exists(pathname) def _get_topic_link(self, topic): if topic: return '[[!meta link="%s"]]' % topic else: return '' def _get_new_note_template(self): filename = self.app.settings['new-note-template'] if filename: with open(filename) as f: return f.read() else: return self.app.read_template('new-note.tmpl')