From 381b7d5d476b6c730fefbb26927251cb4a016ffa Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Fri, 5 Jun 2020 13:59:57 -0700 Subject: Add z and z-import --- z-import | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 z-import (limited to 'z-import') diff --git a/z-import b/z-import new file mode 100755 index 0000000..ffdd089 --- /dev/null +++ b/z-import @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +import datetime +import fileinput +import re +import os +import os.path +import subprocess + +notes = {} +with open(os.path.expanduser("~/org/wiki/notes.org"), "r") as orgnotes: + lines = orgnotes.read().split("\n* ") + for i, l in enumerate(lines): + notes[i] = l + +for i, text in notes.items(): + if i == 0: + continue + # metadata + print("=", i) + print("= getting metadata") + tags = re.search("[:](\S*)[:]", text).group(1).split(":") + title = re.sub(":\S*:?", "", text.split("\n")[0]).strip() + made = re.search(":made:\s+(.*)\n", text, flags=re.IGNORECASE) + if made: + created = made.group(1) + created = created.lstrip("[").rstrip("]") + created = datetime.datetime.strptime(created, "%Y-%m-%d %a %H:%M").strftime( + "%Y.%m.%d..%H.%M" + ) + else: + created = datetime.datetime.now().strftime("%Y.%m.%d..%H.%M") + orgcontent = text.split("\n")[1:] + print("= converting to markdown") + content = subprocess.check_output( + ["pandoc", "-f", "org", "-t", "markdown"], + input="\n".join(orgcontent), + text=True, + ).strip() + + print("= writing new file") + with open(os.path.expanduser(f"~/test-z-wiki/{i:03}.md"), "w") as outfile: + outfile.write(f"title: {title}\n") + outfile.write(f"created: {created}\n") + for t in tags: + outfile.write(f"tag: {t}\n") + outfile.write("---\n") + if content is "": + outfile.write("no note content") + else: + outfile.write(content) + outfile.write("\n") -- cgit v1.2.3