[Sync] + CSVToken for csv to table rendering
This commit is contained in:
1 parent
60c80e3eef
commit
99d5d2a350
2 files changed
+59
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import csv
|
||||
import io
|
||||
|
||||
from kazimir.Token import FileToken, is_file
|
||||
|
||||
extensions = ['.csv']
|
||||
|
||||
|
||||
class CSVToken(FileToken):
|
||||
@staticmethod
|
||||
def test(data: str):
|
||||
return is_file(data, extensions)
|
||||
|
||||
def __init__(self, data) -> None:
|
||||
super().__init__(data=data)
|
||||
self.name = 'csv'
|
||||
self.joinable = False
|
||||
self.build = None
|
||||
|
||||
async def compile(self):
|
||||
data = await self.build(self.data)
|
||||
|
||||
f = io.StringIO(data)
|
||||
reader = csv.reader(f, delimiter=',', quotechar='|')
|
||||
|
||||
headers = next(reader, None)
|
||||
|
||||
row = lambda xs: '\n'.join([
|
||||
f'<td>{x}</td>' for x in xs
|
||||
])
|
||||
|
||||
head = '\n'.join([
|
||||
f'<th>{x}</th>' for x in headers
|
||||
])
|
||||
body = '\n'.join([
|
||||
f'<tr>{row(x)}</tr>' for x in reader
|
||||
])
|
||||
|
||||
return f'''
|
||||
<table class="MarkerTable">
|
||||
<thead><tr>
|
||||
{head}
|
||||
</tr></thead>
|
||||
<tbody>{body}</tbody>
|
||||
</table>
|
||||
'''.strip()
|
||||
@@ -1,6 +1,8 @@
|
||||
import asyncio
|
||||
import lxml.html
|
||||
from markdown import markdown
|
||||
|
||||
from kazimir.CSVToken import CSVToken
|
||||
from kazimir.InstagramToken import InstagramToken
|
||||
from kazimir.YoutubeToken import YoutubeToken
|
||||
from kazimir.Token import SplitToken, ImageToken, UrlToken, FileToken, TextToken, DocumentToken, TokenFactory, \
|
||||
@@ -156,6 +158,8 @@ Fye!
|
||||
["Ya"](http://ya.ru)
|
||||
|
||||
More text
|
||||
|
||||
file.csv
|
||||
'''
|
||||
|
||||
|
||||
@@ -172,6 +176,14 @@ More text
|
||||
}
|
||||
return document
|
||||
|
||||
async def read_csv(data):
|
||||
return '\n'.join([
|
||||
'N,Name',
|
||||
f'0,{data}',
|
||||
'1,Pop',
|
||||
'2,Top',
|
||||
])
|
||||
|
||||
|
||||
async def run():
|
||||
from kazimir.fix_links_quotes import fix_links_quotes
|
||||
@@ -183,6 +195,7 @@ More text
|
||||
m.add_token_factory(TokenFactory(UrlToken))
|
||||
m.add_token_factory(TokenFactory(ImageToken))
|
||||
m.add_token_factory(BuildTokenFactory(build=build_document))
|
||||
m.add_token_factory(BuildTokenFactory(CSVToken, build=read_csv))
|
||||
m.add_token_factory(TokenFactory(FileToken))
|
||||
m.add_token_factory(TokenFactory(TextToken))
|
||||
m.add_tree_middleware(fix_links_quotes)
|
||||
|
||||
Reference in new issue
Block a user