1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# 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 <http://www.gnu.org/licenses/>.
import cliapp
import os
import shutil
import string
import time
import jtlib
class NewCommand(cliapp.Plugin):
_default_new_note_template = '''\
[[!meta title="%(title)s"]]
[[!tag ]]
[[!meta date="%(date)s"]]
%(topiclink)s
'''
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()
draft_id = drafts_dir.create_draft(
self._get_new_note_template() % values)
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._default_new_note_template
|