-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiddlify.py
executable file
·72 lines (59 loc) · 1.95 KB
/
tiddlify.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python3
import glob
import re
import sys
# Regex to remove all HTML tags
removeTagRegex = r"<[^>]+>"
# Matches <br>
brRegex = r"<br>"
placeholder = "PlAcEhOlDeR"
# Regex for multiple <br> in a row, maybe with line break
lotsOfBreaks = r"(<br>\n*){2,}"
if __name__ == '__main__':
# Pass file as parameter on command line
path = sys.argv[1]
fullpath = sys.argv[2].split(".")[-2]
# Get the file contents
myFile = open(path, "r")
contents = myFile.read()
# Swap <br> with a placeholder, so won't be removed with all other tags
contents = re.sub(brRegex, placeholder, contents)
# Search and remove all HTML tags
noTags = re.sub(removeTagRegex, "", contents)
# Put back line breaks
noTags = re.sub(placeholder, "<br>", noTags)
# Remove extraneous line breaks
noTags = re.sub(lotsOfBreaks, "<br><br>\n\n", noTags)
myFile.close()
# Add metedata to file
myFile = open(path, "w")
# Filename skipping .tid, in title case
filename = path[:-4].title()
# Get folder names
tags = fullpath.split("/")
# Take out ../
while ".." in tags:
tags.remove("..")
# Remove last tag, which is the filename itself
del tags[-1]
# Format tags: [tag] [tag] [tag]
tagsString = ""
for t in tags:
tagsString += "[[" + t + "]] "
# Add title of file to become title of Tiddler
# excluding .tid extension
myFile.write("title: " + filename + "\n")
# Add the tags
myFile.write("tags: " + tagsString + "\n")
# Add so TiddlyWiki will render as Tiddler, not text
# Not needed if have title attribute
#myFile.write("type: text/vnd.tiddlywiki\n\n")
# To separate metadata and data
myFile.write("\n\n")
# Add link to the actual file (in WikiText)
myFile.write("//This file is located at: ''" + sys.argv[2] + "''//\n\n\n")
# Add horizontal line
myFile.write("---\n")
# Write data
myFile.write(noTags)
myFile.close()