Token compile refactored
This commit is contained in:
1 parent
ec3e09b509
commit
1b5fc82e59
2 files changed
+29
-17
No files matched your search
@@ -11,9 +11,13 @@ class DocumentToken(Token):
|
||||
self.joinable = False
|
||||
self.build = None
|
||||
|
||||
async def compile(self):
|
||||
async def get_data(self):
|
||||
data = parse_file(self.data)
|
||||
document = await self.build(data)
|
||||
return document
|
||||
|
||||
async def compile(self):
|
||||
document = await self.get_data()
|
||||
|
||||
return create_document(document)
|
||||
|
||||
|
||||
@@ -38,8 +38,12 @@ class Token:
|
||||
self.data = data
|
||||
self.joinable = True
|
||||
|
||||
async def compile(self):
|
||||
return str(self.data)
|
||||
async def get_data(self):
|
||||
return self.data
|
||||
|
||||
async def compile(self) -> str:
|
||||
data = await self.get_data()
|
||||
return str(data)
|
||||
|
||||
def merge(self, token):
|
||||
return self
|
||||
@@ -73,7 +77,8 @@ class TextToken(Token):
|
||||
return TextToken(data.strip())
|
||||
|
||||
async def compile(self):
|
||||
return markdown_to_html(self.data)
|
||||
data = await self.get_data()
|
||||
return markdown_to_html(data)
|
||||
|
||||
|
||||
class FileToken(Token):
|
||||
@@ -85,8 +90,11 @@ class FileToken(Token):
|
||||
super().__init__(name='file', data=data)
|
||||
self.joinable = False
|
||||
|
||||
async def get_data(self):
|
||||
return parse_file(self.data)
|
||||
|
||||
async def compile(self):
|
||||
data = parse_file(self.data)
|
||||
data = await self.get_data()
|
||||
url = data['file']
|
||||
text = data['caption'] if data['caption'] else url
|
||||
return f'<a href="{url}">{text}</a>'
|
||||
@@ -107,9 +115,13 @@ class ImageToken(Token):
|
||||
merged_token.build = self.build
|
||||
return merged_token
|
||||
|
||||
async def get_data(self):
|
||||
data = parse_file(self.data)
|
||||
data = await self.build(data)
|
||||
return data
|
||||
|
||||
async def compile(self):
|
||||
img = self.parse_data()
|
||||
img = await self.build(img)
|
||||
img = await self.get_data()
|
||||
alt = img['alt']
|
||||
caption = markdown_to_html(img['caption'])
|
||||
src = img['src']
|
||||
@@ -124,9 +136,6 @@ class ImageToken(Token):
|
||||
'''
|
||||
return tpl.format(img).strip()
|
||||
|
||||
def parse_data(self):
|
||||
return parse_file(self.data)
|
||||
|
||||
|
||||
class ImageCollectionToken(ImageToken):
|
||||
def __init__(self, data) -> None:
|
||||
@@ -143,13 +152,16 @@ class ImageCollectionToken(ImageToken):
|
||||
merged_token.build = self.build
|
||||
return merged_token
|
||||
|
||||
async def compile(self):
|
||||
data = self.parse_data()
|
||||
async def get_data(self):
|
||||
items = [parse_file(x) for x in self.data]
|
||||
images = []
|
||||
for x in data:
|
||||
for x in items:
|
||||
img = await self.build(x)
|
||||
images.append(img)
|
||||
return images
|
||||
|
||||
async def compile(self):
|
||||
images = await self.get_data()
|
||||
images = [x['src'] for x in images]
|
||||
images = [f'<img src="{x}">' for x in images]
|
||||
images = '\n'.join(images)
|
||||
@@ -165,10 +177,6 @@ class ImageCollectionToken(ImageToken):
|
||||
</div>
|
||||
'''
|
||||
|
||||
def parse_data(self):
|
||||
return [parse_file(x) for x in self.data]
|
||||
|
||||
|
||||
class UrlToken(Token):
|
||||
@staticmethod
|
||||
def test(data: str):
|
||||
|
||||
Reference in new issue
Block a user