Skip to content

Instantly share code, notes, and snippets.

@Maarrk
Created October 11, 2022 13:34
Show Gist options
  • Save Maarrk/2e07b983e7c8ba74dc286c67e858c043 to your computer and use it in GitHub Desktop.
Save Maarrk/2e07b983e7c8ba74dc286c67e858c043 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import argparse
from glob import glob
from os.path import expanduser
import re
parser = argparse.ArgumentParser()
parser.add_argument('pattern', metavar='PATTERN',
help='part of filename to match')
parser.add_argument('--dir', default='~/.dendron/vault', help='vault location')
parser.add_argument('-o', '--output', help='output markdown filename')
args = parser.parse_args()
vault_dir = expanduser(args.dir)
filenames = glob(vault_dir + f'/*{args.pattern}*.md')
filenames = [f + '.md' for f in sorted([s[:-3] for s in filenames])]
output = args.output if args.output is not None else args.pattern + '.md'
title_pattern = re.compile(
r"""---\n[a-z:\d\s]+title: ([A-Za-zĄĆĘŁŃÓŚŹŻąćęłńóśźż"" ()]+)\n[a-z:\d\s"']+\n---""")
with open(output, 'w', encoding='utf-8') as out_file:
for filename in filenames:
with open(filename, 'r', encoding='utf-8') as file:
content = file.read()
replaced = title_pattern.sub(r'# \1', content)
out_file.write(replaced)
out_file.write('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment