62 lines
1.9 KiB
Python
62 lines
1.9 KiB
Python
#!/usr/bin/env python
|
|
|
|
import yaml
|
|
|
|
# The paths that are always to be ignored.
|
|
STATIC_IGNORE = [
|
|
'.obsidian'
|
|
]
|
|
|
|
MARKDOWN_PAT = '*.md'
|
|
|
|
|
|
class SourceNode:
|
|
def __init__(self, path, is_dir):
|
|
self._path = path
|
|
self._is_dir = is_dir
|
|
self._is_md = path.match(MARKDOWN_PAT)
|
|
self.metadata = None
|
|
self.text = None
|
|
|
|
def __str__(self):
|
|
return f"SourceNode({self._path}, {self._is_dir}) [is_md={self._is_md}]"
|
|
|
|
@classmethod
|
|
def generate_list(cls, source_root):
|
|
nodes = []
|
|
dirs = [source_root]
|
|
while len(dirs) > 0:
|
|
current_dir = dirs.pop(0)
|
|
for child in current_dir.iterdir():
|
|
rchild = child.relative_to(source_root)
|
|
add_me = True
|
|
for pat in STATIC_IGNORE:
|
|
if rchild.match(pat):
|
|
add_me = False
|
|
break
|
|
if add_me:
|
|
nodes.append(SourceNode(rchild, child.is_dir()))
|
|
if child.is_dir():
|
|
dirs.append(child)
|
|
return nodes
|
|
|
|
def load_metadata(self, source_dir):
|
|
if self._is_md:
|
|
with open(source_dir / self._path, "r", encoding="utf-8") as f:
|
|
print(">>>opened")
|
|
l = f.readline()
|
|
if l == '---\n':
|
|
print(">>>startmd")
|
|
metalines = []
|
|
l = f.readline()
|
|
while l != '---\n':
|
|
metalines.append(l)
|
|
l = f.readline()
|
|
self.metadata = yaml.full_load(''.join(metalines))
|
|
|
|
def parse_markdown(self, source_dir, markdown_parser):
|
|
if self._is_md:
|
|
markdown_parser.reset()
|
|
with open(source_dir / self._path, "r", encoding="utf-8") as f:
|
|
self.text = markdown_parser.convert(f.read())
|