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