# Copyright (c) 2022-2024 Chris Pressey, Cat's Eye Technologies
# This file is distributed under a 2-clause BSD license. See LICENSES directory:
# SPDX-License-Identifier: LicenseRef-BSD-2-Clause-X-Eqthy
import codecs
import os
import re
from typing import List, Tuple
from eqthy.objects import Document
from eqthy.parser import Parser
class Loader:
def __init__(self, format: str):
self.format = format
def load(self, path: str) -> List[Document]:
dirname, filename = os.path.split(path)
return self.load_file_from_dir(dirname, filename)
def load_file_from_dir(self, dirname: str, filename: str) -> List[Document]:
path = os.path.join(dirname, filename)
with codecs.open(path, 'r', encoding='UTF-8') as f:
text = f.read()
documents = []
if self.format in "eqthy":
pass
elif self.format == "markdown":
text, subdocs = self.extract_from_markdown(text, dirname)
documents.extend(subdocs)
else:
raise NotImplementedError(self.format)
p = Parser(text, filename)
document = p.document()
documents.append(document)
return documents
def extract_from_markdown(self, text: str, dirname: str) -> Tuple[str, List[Document]]:
new_lines = []
subdocs = []
for line in text.split("\n"):
if line.startswith(" "):
new_lines.append(line[4:])
else:
match = re.match(r'^Requires \[.*?\]\((.*?)\).*?$', line, re.IGNORECASE)
if match:
subdocs = self.load_file_from_dir(dirname, match.group(1))
return "\n".join(new_lines), subdocs