from pathlib import Path from subprocess import run import re import sys entry_re = re.compile(r'^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})', re.MULTILINE) matches = [] for fpath in sorted((Path.home() / 'workspace' / 'journal').glob('*.md')): header, *tmp = entry_re.split(fpath.read_text()) entries = list(zip(tmp[::2], tmp[1::2])) for (timestamp, content) in sorted(entries, key=lambda x: x[0]): content = '\n'.join( part.replace('\n', ' ') for part in content.split('\n\n') ) if sys.argv[1].lower() in content.lower().split(): matches.append((timestamp, content)) buf = '' for (ts, c) in matches: c = c.replace('\n', ' ').strip() buf += (f'[[{ts}]] {c}')[:80] + '\n' run(['nvim', '-'], input=buf.encode('utf-8'))