Skip to content

Commit

Permalink
Feature - add bracketed date support when composing new entry (jrnl-o…
Browse files Browse the repository at this point in the history
…rg#1915)

Bracketed dates like [2020-01-01] will be parsed and used as the entry date, thus be removed from the entry text.
  • Loading branch information
chwt committed Sep 24, 2024
1 parent 4d84c49 commit d12d5c0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
27 changes: 25 additions & 2 deletions jrnl/journals/Journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ def _parse(self, journal_txt: str) -> list[Entry]:
new_date = time.parse(date_blob, bracketed=True)

if new_date:
# Fill in text of entry created in previous loop
if entries:
entries[-1].text = journal_txt[last_entry_pos : match.start()]
last_entry_pos = match.end()
Expand Down Expand Up @@ -363,7 +364,7 @@ def new_entry(self, raw: str, date=None, sort: bool = True) -> Entry:
raw = raw.replace("\\n ", "\n").replace("\\n", "\n")
# Split raw text into title and body
sep = re.search(r"\n|[?!.]+ +\n?", raw)
first_line = raw[: sep.end()].strip() if sep else raw
first_line = raw[:sep.end()].strip() if sep else raw
starred = False

if not date:
Expand All @@ -376,7 +377,29 @@ def new_entry(self, raw: str, date=None, sort: bool = True) -> Entry:
)
if date: # Parsed successfully, strip that from the raw text
starred = raw[:colon_pos].strip().endswith("*")
raw = raw[colon_pos + 1 :].strip()
raw = raw[(colon_pos + 1):].strip()
else:
date_blob_re = re.compile("(?:^|\n)\\[([^\\]]+)\\] ")
match = date_blob_re.search(raw)
if match is not None:
date_blob = match.groups()[0]
try:
date = datetime.datetime.strptime(
date_blob, self.config["timeformat"]
)
except ValueError:
# Passing in a date that had brackets around it
date = time.parse(
date_blob,
bracketed=True,
default_hour=self.config["default_hour"],
default_minute=self.config["default_minute"],
)

if date: # Parsed successfully, strip that from the raw text
starred = raw[:match.start()].strip().endswith("*")
raw = raw[match.end():].strip()

starred = (
starred
or first_line.startswith("*")
Expand Down
15 changes: 15 additions & 0 deletions tests/bdd/features/datetime.feature
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ Feature: Reading and writing to journal with custom date formats
| little_endian_dates.yaml | 2032-02-01: Test. | 01.02.2032 09:00 Test. |
| little_endian_dates.yaml | 2020-01-01: Test. | 01.01.2020 09:00 Test. |
| little_endian_dates.yaml | 2020-12-31: Test. | 31.12.2020 09:00 Test. |
# @todo: is it fine that default time be used here and not 00:00?
| little_endian_dates.yaml | [2020-12-31] bracket. | 31.12.2020 09:00 bracket. |
| little_endian_dates.yaml | [2020-12-31 10:00 PM] brkt. | 31.12.2020 22:00 brkt. |
| little_endian_dates.yaml | [2020-12-31]: brkt colon. | 31.12.2020 09:00 brkt colon. |
| little_endian_dates.yaml | [2020-12-31 12:34]: b colon. | 31.12.2020 12:34 b colon. |
| little_endian_dates.yaml | [2019-02-29] brkt neg. | [2019-02-29] brkt neg. |
| little_endian_dates.yaml | [2020-08-32] brkt neg. | [2020-08-32] brkt neg. |


Scenario Outline: Searching for dates with custom date
Expand Down Expand Up @@ -78,6 +85,14 @@ Feature: Reading and writing to journal with custom date formats
Then the output should contain "10.05.2013 09:00 I saw Elvis."
And the output should contain "He's alive."

Scenario: Writing an entry at the prompt with custom date in bracket format
Given we use the config "little_endian_dates.yaml"
When we run "jrnl" and type "[2013-05-10 12:34] I saw Elvis. He's alive."
Then we should get no error
When we run "jrnl -999"
Then the output should contain "10.05.2013 12:34 I saw Elvis."
And the output should contain "He's alive."


Scenario: Viewing today's entries does not print the entire journal
# see: https://github.com/jrnl-org/jrnl/issues/741
Expand Down
25 changes: 25 additions & 0 deletions tests/bdd/features/star.feature
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,31 @@ Feature: Starring entries
| empty_folder.yaml |
| dayone.yaml |

Scenario Outline: Starring an entry with bracketed date will mark it in the journal file
Given we use the config "<config_file>"
When we run "jrnl <command>"
Then we should get no error
When we run "jrnl -on 2013-07-20 -starred"
Then the output should contain "2013-07-20 09:00 Best day of my life!"

Examples: configs
| config_file | command |
| simple.yaml | [2013-07-20] Best day of my life! * |
| empty_folder.yaml | [2013-07-20] Best day of my life! * |
# Note: this one fail due to whitespace, cmp. next config
# | dayone.yaml | [2013-07-20] Best day of my life! * |
| dayone.yaml | [2013-07-20] Best day of my life!* |
| simple.yaml | [2013-07-20]*: Best day of my life! |
| empty_folder.yaml | [2013-07-20]*: Best day of my life! |
| dayone.yaml | [2013-07-20]*: Best day of my life! |
| simple.yaml | [2013-07-20] * : Best day of my life! |
| empty_folder.yaml | [2013-07-20] * : Best day of my life! |
| dayone.yaml | [2013-07-20] * : Best day of my life! |
| simple.yaml | [2013-07-20] * Best day of my life! |
| empty_folder.yaml | [2013-07-20] * Best day of my life! |
# Note: this one fails in having the star in the output too
# | dayone.yaml | [2013-07-20] * Best day of my life! |

Scenario Outline: Filtering by starred entries will show only starred entries
Given we use the config "<config_file>"
When we run "jrnl -starred"
Expand Down

0 comments on commit d12d5c0

Please sign in to comment.