mirror of
https://github.com/jrnl-org/jrnl.git
synced 2025-07-04 07:26:14 +02:00
String replacement to deal with more dates
deals with `tues`, `thurs`, `sept`, and ` o'clock`
This commit is contained in:
parent
aede53a9f4
commit
b0a29736d7
2 changed files with 21 additions and 0 deletions
|
@ -254,6 +254,11 @@ class Journal(object):
|
|||
# Use the default time.
|
||||
date = datetime(year=int(date_str), month=1, day=1, hour=self.config['default_hour'], minute=self.config['default_minute'])
|
||||
else:
|
||||
replacements = (u"september", u"sep"), (u"sept", u"sep"), (u"tuesday", u"tue"), \
|
||||
(u"tues", u"tue"), (u"thursday", u"thu"), (u"thurs", u"thu"), \
|
||||
(u" o'clock", u":00")
|
||||
date_str = util.multiple_replace(date_str, *replacements)
|
||||
|
||||
try:
|
||||
date = dateutil.parser.parse(date_str)
|
||||
flag = 1 if date.hour == 0 and date.minute == 0 else 2
|
||||
|
|
16
jrnl/util.py
16
jrnl/util.py
|
@ -141,3 +141,19 @@ def slugify(string):
|
|||
slug = re.sub(r'[-\s]+', '-', no_punctuation)
|
||||
return u(slug)
|
||||
|
||||
# Use the following two functions to do multiple replacements in one pass
|
||||
# from http://stackoverflow.com/questions/6116978/python-replace-multiple-strings
|
||||
#
|
||||
# Useage:
|
||||
# >>> replacements = (u"café", u"tea"), (u"tea", u"café"), (u"like", u"love")
|
||||
# >>> print multiple_replace(u"Do you like café? No, I prefer tea.", *replacements)
|
||||
# output: Do you love tea? No, I prefer café.
|
||||
|
||||
def multiple_replacer(*key_values):
|
||||
replace_dict = dict(key_values)
|
||||
replacement_function = lambda match: replace_dict[match.group(0)]
|
||||
pattern = re.compile("|".join([re.escape(k) for k, v in key_values]), re.M | re.I)
|
||||
return lambda string: pattern.sub(replacement_function, string)
|
||||
|
||||
def multiple_replace(string, *key_values):
|
||||
return multiple_replacer(*key_values)(string)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue