summaryrefslogtreecommitdiff
path: root/z-import
blob: ffdd089cc5f4f223c3abbea15321709a3b99224f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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")