2021-05-07 04:51:23 +12:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# --!-- coding: utf8 --!--
|
2022-11-19 11:32:08 +13:00
|
|
|
import collections
|
2021-05-15 05:46:39 +12:00
|
|
|
import os
|
2021-05-07 04:51:23 +12:00
|
|
|
import re
|
|
|
|
|
|
|
|
from manuskript.io.abstractFile import AbstractFile
|
|
|
|
|
|
|
|
|
|
|
|
class MmdFile(AbstractFile):
|
|
|
|
|
2021-05-09 12:14:36 +12:00
|
|
|
def __init__(self, path, metaSpacing=16):
|
2021-05-09 10:39:12 +12:00
|
|
|
AbstractFile.__init__(self, path)
|
|
|
|
|
|
|
|
self.metaSpacing = metaSpacing
|
|
|
|
|
2021-05-07 04:51:23 +12:00
|
|
|
def loadMMD(self, ignoreBody: bool = True):
|
2022-11-19 11:32:08 +13:00
|
|
|
metadata = collections.OrderedDict()
|
2021-05-07 04:51:23 +12:00
|
|
|
body = None
|
|
|
|
|
|
|
|
metaPattern = re.compile(r"^([^\s].*?):\s*(.*)\n$")
|
|
|
|
metaValuePattern = re.compile(r"^(\s+)(.*)\n$")
|
|
|
|
metaKey = None
|
|
|
|
metaValue = None
|
|
|
|
|
|
|
|
with open(self.path, 'rt', encoding='utf-8') as file:
|
2023-03-22 20:26:09 +13:00
|
|
|
line = file.readline()
|
|
|
|
while line:
|
2021-05-07 04:51:23 +12:00
|
|
|
m = metaPattern.match(line)
|
|
|
|
|
|
|
|
if not (m is None):
|
|
|
|
if not (metaKey is None):
|
|
|
|
metadata[metaKey] = metaValue
|
|
|
|
|
|
|
|
metaKey = m.group(1)
|
|
|
|
metaValue = m.group(2)
|
2023-03-22 20:26:09 +13:00
|
|
|
else:
|
|
|
|
m = metaValuePattern.match(line)
|
2021-05-07 04:51:23 +12:00
|
|
|
|
2023-03-22 20:26:09 +13:00
|
|
|
if not (m is None):
|
|
|
|
metaValue += "\n" + m.group(2)
|
|
|
|
elif line == "\n":
|
|
|
|
break
|
2021-05-07 04:51:23 +12:00
|
|
|
|
2023-03-22 20:26:09 +13:00
|
|
|
line = file.readline()
|
2021-05-07 04:51:23 +12:00
|
|
|
|
2021-05-09 10:39:12 +12:00
|
|
|
if not (metaKey is None):
|
|
|
|
metadata[metaKey] = metaValue
|
|
|
|
|
2021-05-07 04:51:23 +12:00
|
|
|
if not ignoreBody:
|
|
|
|
body = file.read()
|
|
|
|
|
2022-12-06 06:28:10 +13:00
|
|
|
if (len(body) > 0) and (body[0] == "\n"):
|
|
|
|
body = body[1:]
|
2023-03-22 20:26:09 +13:00
|
|
|
elif file.seekable():
|
|
|
|
currentPosition = file.tell()
|
|
|
|
|
|
|
|
file.seek(0, 2)
|
|
|
|
endPosition = file.tell()
|
|
|
|
|
|
|
|
if endPosition - currentPosition == 1:
|
|
|
|
body = ""
|
2022-12-06 06:28:10 +13:00
|
|
|
|
2021-05-07 04:51:23 +12:00
|
|
|
return metadata, body
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
return self.loadMMD(False)
|
|
|
|
|
|
|
|
def save(self, content):
|
|
|
|
metadata, body = content
|
2021-05-09 10:39:12 +12:00
|
|
|
metaSpacing = self.metaSpacing
|
2021-05-07 04:51:23 +12:00
|
|
|
|
2021-05-09 12:14:36 +12:00
|
|
|
for (key, value) in metadata.items():
|
|
|
|
if value is None:
|
|
|
|
continue
|
|
|
|
|
2021-05-07 04:51:23 +12:00
|
|
|
metaSpacing = max(metaSpacing, len(key) + 2)
|
|
|
|
|
|
|
|
with open(self.path, 'wt', encoding='utf-8') as file:
|
|
|
|
for (key, value) in metadata.items():
|
2021-05-09 10:39:12 +12:00
|
|
|
if value is None:
|
|
|
|
continue
|
|
|
|
|
2021-05-07 04:51:23 +12:00
|
|
|
spacing = metaSpacing - (len(key) + 2)
|
2021-05-09 12:14:36 +12:00
|
|
|
lines = str(value).split("\n")
|
2021-05-07 04:51:23 +12:00
|
|
|
|
|
|
|
file.write(key + ": " + spacing * " " + lines[0] + "\n")
|
|
|
|
|
|
|
|
for line in lines[1:]:
|
|
|
|
file.write(metaSpacing * " " + line + "\n")
|
|
|
|
|
|
|
|
if not (body is None):
|
2021-05-09 10:39:12 +12:00
|
|
|
file.write("\n" + body)
|
2021-05-15 05:46:39 +12:00
|
|
|
|
|
|
|
def remove(self):
|
|
|
|
if os.path.exists(self.path):
|
|
|
|
os.remove(self.path)
|