Skip to content

Instantly share code, notes, and snippets.

@Japanuspus
Created August 28, 2021 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Japanuspus/6073019a3a150cacbcec166369538a2b to your computer and use it in GitHub Desktop.
Save Japanuspus/6073019a3a150cacbcec166369538a2b to your computer and use it in GitHub Desktop.
note_cleaner: remove duplicate titles inside notes as added by repeated nvpy/resoph/nvalt migrations
from pathlib import Path
import re, sys, os
def clean_note_lines(title: str, lines: list[str]):
"""
Remove any lines matching title
"""
nn = re.compile(r"[^a-zA-Z0-9]")
def norm(s):
return nn.sub('', s).lower()
s0 = norm(title)
for line in lines:
if not norm(line)==s0:
yield line
def clone_mtime(src: Path, dst: Path):
s = src.stat()
os.utime(dst, (s.st_ctime, s.st_mtime))
def clean_note(p: Path, po: Path):
print(f"Cleaning: {p} --> {po}")
with p.open(encoding='utf8') as f, po.open(mode='w',encoding='utf8', newline='\n') as fo:
fo.writelines(clean_note_lines(p.stem, f))
clone_mtime(p, po)
def clean_name(s):
return re.sub('_', ' ', s)
def main():
p0 = Path(sys.argv[1])
p1 = Path(sys.argv[2])
for p in p0.glob('*.md'):
clean_note(p, p1 / clean_name(str(p.relative_to(p0))))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment