mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-05-10 16:48:31 +02:00
Support for templates
This commit is contained in:
parent
43bb1f1cb2
commit
7c6b2e4bce
5 changed files with 31 additions and 9 deletions
|
@ -313,12 +313,6 @@ def open_journal(name, config, legacy=False):
|
||||||
backwards compatibility with jrnl 1.x
|
backwards compatibility with jrnl 1.x
|
||||||
"""
|
"""
|
||||||
config = config.copy()
|
config = config.copy()
|
||||||
journal_conf = config['journals'].get(name)
|
|
||||||
if type(journal_conf) is dict: # We can override the default config on a by-journal basis
|
|
||||||
log.debug('Updating configuration with specific journal overrides %s', journal_conf)
|
|
||||||
config.update(journal_conf)
|
|
||||||
else: # But also just give them a string to point to the journal file
|
|
||||||
config['journal'] = journal_conf
|
|
||||||
config['journal'] = os.path.expanduser(os.path.expandvars(config['journal']))
|
config['journal'] = os.path.expanduser(os.path.expandvars(config['journal']))
|
||||||
|
|
||||||
if os.path.isdir(config['journal']):
|
if os.path.isdir(config['journal']):
|
||||||
|
|
12
jrnl/cli.py
12
jrnl/cli.py
|
@ -153,6 +153,7 @@ def run(manual_args=None):
|
||||||
# If the first textual argument points to a journal file,
|
# If the first textual argument points to a journal file,
|
||||||
# use this!
|
# use this!
|
||||||
journal_name = args.text[0] if (args.text and args.text[0] in config['journals']) else 'default'
|
journal_name = args.text[0] if (args.text and args.text[0] in config['journals']) else 'default'
|
||||||
|
|
||||||
if journal_name is not 'default':
|
if journal_name is not 'default':
|
||||||
args.text = args.text[1:]
|
args.text = args.text[1:]
|
||||||
elif "default" not in config['journals']:
|
elif "default" not in config['journals']:
|
||||||
|
@ -160,6 +161,8 @@ def run(manual_args=None):
|
||||||
util.prompt(list_journals(config))
|
util.prompt(list_journals(config))
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
config = util.scope_config(config, journal_name)
|
||||||
|
|
||||||
# If the first remaining argument looks like e.g. '-3', interpret that as a limiter
|
# If the first remaining argument looks like e.g. '-3', interpret that as a limiter
|
||||||
if not args.limit and args.text and args.text[0].startswith("-"):
|
if not args.limit and args.text and args.text[0].startswith("-"):
|
||||||
try:
|
try:
|
||||||
|
@ -182,7 +185,14 @@ def run(manual_args=None):
|
||||||
# Piping data into jrnl
|
# Piping data into jrnl
|
||||||
raw = util.py23_read()
|
raw = util.py23_read()
|
||||||
elif config['editor']:
|
elif config['editor']:
|
||||||
raw = util.get_text_from_editor(config)
|
template = ""
|
||||||
|
if config['template']:
|
||||||
|
try:
|
||||||
|
template = open(config['template']).read()
|
||||||
|
except:
|
||||||
|
util.prompt("[Could not read template at '']".format(config['template']))
|
||||||
|
sys.exit(1)
|
||||||
|
raw = util.get_text_from_editor(config, template)
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
raw = util.py23_read("[Compose Entry; " + _exit_multiline_code + " to finish writing]\n")
|
raw = util.py23_read("[Compose Entry; " + _exit_multiline_code + " to finish writing]\n")
|
||||||
|
|
|
@ -47,6 +47,7 @@ default_config = {
|
||||||
},
|
},
|
||||||
'editor': os.getenv('VISUAL') or os.getenv('EDITOR') or "",
|
'editor': os.getenv('VISUAL') or os.getenv('EDITOR') or "",
|
||||||
'encrypt': False,
|
'encrypt': False,
|
||||||
|
'template': False,
|
||||||
'default_hour': 9,
|
'default_hour': 9,
|
||||||
'default_minute': 0,
|
'default_minute': 0,
|
||||||
'timeformat': "%Y-%m-%d %H:%M",
|
'timeformat': "%Y-%m-%d %H:%M",
|
||||||
|
|
|
@ -82,7 +82,7 @@ older versions of jrnl anymore.
|
||||||
for journal_name, path in encrypted_journals.items():
|
for journal_name, path in encrypted_journals.items():
|
||||||
util.prompt("\nUpgrading encrypted '{}' journal stored in {}...".format(journal_name, path))
|
util.prompt("\nUpgrading encrypted '{}' journal stored in {}...".format(journal_name, path))
|
||||||
backup(path, binary=True)
|
backup(path, binary=True)
|
||||||
old_journal = Journal.open_journal(journal_name, config, legacy=True)
|
old_journal = Journal.open_journal(journal_name, util.scope_config(config, journal_name), legacy=True)
|
||||||
new_journal = EncryptedJournal.from_journal(old_journal)
|
new_journal = EncryptedJournal.from_journal(old_journal)
|
||||||
new_journal.write()
|
new_journal.write()
|
||||||
util.prompt(" Done.")
|
util.prompt(" Done.")
|
||||||
|
@ -90,7 +90,7 @@ older versions of jrnl anymore.
|
||||||
for journal_name, path in plain_journals.items():
|
for journal_name, path in plain_journals.items():
|
||||||
util.prompt("\nUpgrading plain text '{}' journal stored in {}...".format(journal_name, path))
|
util.prompt("\nUpgrading plain text '{}' journal stored in {}...".format(journal_name, path))
|
||||||
backup(path)
|
backup(path)
|
||||||
old_journal = Journal.open_journal(journal_name, config, legacy=True)
|
old_journal = Journal.open_journal(journal_name, util.scope_config(config, journal_name), legacy=True)
|
||||||
new_journal = Journal.PlainJournal.from_journal(old_journal)
|
new_journal = Journal.PlainJournal.from_journal(old_journal)
|
||||||
new_journal.write()
|
new_journal.write()
|
||||||
util.prompt(" Done.")
|
util.prompt(" Done.")
|
||||||
|
|
17
jrnl/util.py
17
jrnl/util.py
|
@ -13,6 +13,9 @@ import subprocess
|
||||||
import codecs
|
import codecs
|
||||||
import unicodedata
|
import unicodedata
|
||||||
import shlex
|
import shlex
|
||||||
|
import logging
|
||||||
|
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
PY3 = sys.version_info[0] == 3
|
PY3 = sys.version_info[0] == 3
|
||||||
|
@ -122,6 +125,20 @@ def load_config(config_path):
|
||||||
return yaml.load(f)
|
return yaml.load(f)
|
||||||
|
|
||||||
|
|
||||||
|
def scope_config(config, journal_name):
|
||||||
|
if journal_name not in config['journals']:
|
||||||
|
return config
|
||||||
|
config = config.copy()
|
||||||
|
journal_conf = config['journals'].get(journal_name)
|
||||||
|
if type(journal_conf) is dict: # We can override the default config on a by-journal basis
|
||||||
|
log.debug('Updating configuration with specific journal overrides %s', journal_conf)
|
||||||
|
config.update(journal_conf)
|
||||||
|
else: # But also just give them a string to point to the journal file
|
||||||
|
config['journal'] = journal_conf
|
||||||
|
config.pop('journals')
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
def get_text_from_editor(config, template=""):
|
def get_text_from_editor(config, template=""):
|
||||||
filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt")
|
filehandle, tmpfile = tempfile.mkstemp(prefix="jrnl", text=True, suffix=".txt")
|
||||||
with codecs.open(tmpfile, 'w', "utf-8") as f:
|
with codecs.open(tmpfile, 'w', "utf-8") as f:
|
||||||
|
|
Loading…
Add table
Reference in a new issue