From 1b5fc82e5964727bae89217d2d6f94a1ed9da924 Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Sat, 29 Feb 2020 01:03:09 +0300 Subject: [PATCH] Token compile refactored --- modules/sync/kazimir/DocumentToken.py | 8 ++++-- modules/sync/kazimir/Token.py | 40 ++++++++++++++++----------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/modules/sync/kazimir/DocumentToken.py b/modules/sync/kazimir/DocumentToken.py index 60e6cb72..259eda98 100644 --- a/modules/sync/kazimir/DocumentToken.py +++ b/modules/sync/kazimir/DocumentToken.py @@ -10,10 +10,14 @@ class DocumentToken(Token): super().__init__(name='file', data=data) 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) diff --git a/modules/sync/kazimir/Token.py b/modules/sync/kazimir/Token.py index 8c8d2c37..dcdc2963 100644 --- a/modules/sync/kazimir/Token.py +++ b/modules/sync/kazimir/Token.py @@ -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'{text}' @@ -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'' for x in images] images = '\n'.join(images) @@ -165,10 +177,6 @@ class ImageCollectionToken(ImageToken): ''' - def parse_data(self): - return [parse_file(x) for x in self.data] - - class UrlToken(Token): @staticmethod def test(data: str):