mirror of
https://github.com/TheFunny/ArisuAutoSweeper
synced 2026-10-10 04:52:05 +00:00
feat: add simple markdown table generator
This commit is contained in:
@@ -33,23 +33,33 @@ class TabWrapper:
|
|||||||
self.suffix += suffix
|
self.suffix += suffix
|
||||||
|
|
||||||
|
|
||||||
|
class VariableWrapper:
|
||||||
|
def __init__(self, name):
|
||||||
|
self.name = name
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return str(self.name)
|
||||||
|
|
||||||
|
__str__ = __repr__
|
||||||
|
|
||||||
|
|
||||||
class CodeGenerator:
|
class CodeGenerator:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.tab_count = 0
|
self.tab_count = 0
|
||||||
self.lines = []
|
self.lines = []
|
||||||
|
|
||||||
def generate(self) -> t.Iterable[str]:
|
|
||||||
yield ''
|
|
||||||
|
|
||||||
def add(self, line, comment=False, newline=True):
|
def add(self, line, comment=False, newline=True):
|
||||||
self.lines.append(self._line_with_tabs(line, comment=comment, newline=newline))
|
self.lines.append(self._line_with_tabs(line, comment=comment, newline=newline))
|
||||||
|
|
||||||
|
def generate(self) -> t.Iterable[str]:
|
||||||
|
return ''.join(self.lines)
|
||||||
|
|
||||||
def print(self):
|
def print(self):
|
||||||
lines = ''.join(self.lines)
|
lines = self.generate()
|
||||||
print(lines)
|
print(lines)
|
||||||
|
|
||||||
def write(self, file: str = None):
|
def write(self, file: str = None):
|
||||||
lines = ''.join(self.lines)
|
lines = self.generate()
|
||||||
with open(file, 'w', encoding='utf-8', newline='') as f:
|
with open(file, 'w', encoding='utf-8', newline='') as f:
|
||||||
f.write(lines)
|
f.write(lines)
|
||||||
|
|
||||||
@@ -77,7 +87,10 @@ class CodeGenerator:
|
|||||||
return TabWrapper(self)
|
return TabWrapper(self)
|
||||||
|
|
||||||
def Empty(self):
|
def Empty(self):
|
||||||
self.add('')
|
self.lines.append('\n')
|
||||||
|
|
||||||
|
def Pass(self):
|
||||||
|
self.add('pass')
|
||||||
|
|
||||||
def Import(self, text, empty=2):
|
def Import(self, text, empty=2):
|
||||||
for line in text.strip().split('\n'):
|
for line in text.strip().split('\n'):
|
||||||
@@ -86,6 +99,9 @@ class CodeGenerator:
|
|||||||
for _ in range(empty):
|
for _ in range(empty):
|
||||||
self.Empty()
|
self.Empty()
|
||||||
|
|
||||||
|
def Variable(self, name):
|
||||||
|
return VariableWrapper(name)
|
||||||
|
|
||||||
def Value(self, key=None, value=None, type_=None, **kwargs):
|
def Value(self, key=None, value=None, type_=None, **kwargs):
|
||||||
if key is not None:
|
if key is not None:
|
||||||
if type_ is not None:
|
if type_ is not None:
|
||||||
@@ -117,7 +133,7 @@ class CodeGenerator:
|
|||||||
if key is not None:
|
if key is not None:
|
||||||
return TabWrapper(self, prefix=str(key) + ' = [', suffix=']')
|
return TabWrapper(self, prefix=str(key) + ' = [', suffix=']')
|
||||||
else:
|
else:
|
||||||
return TabWrapper(self, prefix='[', suffix=']', newline=False)
|
return TabWrapper(self, prefix='[', suffix=']')
|
||||||
|
|
||||||
def ListItem(self, value):
|
def ListItem(self, value):
|
||||||
if isinstance(value, TabWrapper):
|
if isinstance(value, TabWrapper):
|
||||||
@@ -131,7 +147,7 @@ class CodeGenerator:
|
|||||||
if key is not None:
|
if key is not None:
|
||||||
return TabWrapper(self, prefix=str(key) + ' = {', suffix='}')
|
return TabWrapper(self, prefix=str(key) + ' = {', suffix='}')
|
||||||
else:
|
else:
|
||||||
return TabWrapper(self, prefix='{', suffix='}', newline=False)
|
return TabWrapper(self, prefix='{', suffix='}')
|
||||||
|
|
||||||
def DictItem(self, key=None, value=None):
|
def DictItem(self, key=None, value=None):
|
||||||
if isinstance(value, TabWrapper):
|
if isinstance(value, TabWrapper):
|
||||||
@@ -147,7 +163,7 @@ class CodeGenerator:
|
|||||||
if key is not None:
|
if key is not None:
|
||||||
return TabWrapper(self, prefix=f'{key} = {object_class}(', suffix=')')
|
return TabWrapper(self, prefix=f'{key} = {object_class}(', suffix=')')
|
||||||
else:
|
else:
|
||||||
return TabWrapper(self, prefix=f'{object_class}(', suffix=')', newline=False)
|
return TabWrapper(self, prefix=f'{object_class}(', suffix=')')
|
||||||
|
|
||||||
def ObjectAttr(self, key=None, value=None):
|
def ObjectAttr(self, key=None, value=None):
|
||||||
if isinstance(value, TabWrapper):
|
if isinstance(value, TabWrapper):
|
||||||
@@ -179,3 +195,33 @@ Value = generator.Value
|
|||||||
Comment = generator.Comment
|
Comment = generator.Comment
|
||||||
Dict = generator.Dict
|
Dict = generator.Dict
|
||||||
DictItem = generator.DictItem
|
DictItem = generator.DictItem
|
||||||
|
|
||||||
|
|
||||||
|
class MarkdownGenerator:
|
||||||
|
def __init__(self, column: t.List[str]):
|
||||||
|
self.rows = [column]
|
||||||
|
|
||||||
|
def add_row(self, row):
|
||||||
|
self.rows.append([str(ele) for ele in row])
|
||||||
|
|
||||||
|
def product_line(self, row, max_width):
|
||||||
|
row = [ele.ljust(width) for ele, width in zip(row, max_width)]
|
||||||
|
row = ' | '.join(row)
|
||||||
|
row = '| ' + row + ' |'
|
||||||
|
return row
|
||||||
|
|
||||||
|
def generate(self) -> t.List[str]:
|
||||||
|
import numpy as np
|
||||||
|
width = np.array([
|
||||||
|
[len(ele) for ele in row] for row in self.rows
|
||||||
|
])
|
||||||
|
max_width = np.max(width, axis=0)
|
||||||
|
dash = ['-' * width for width in max_width]
|
||||||
|
|
||||||
|
rows = [
|
||||||
|
self.product_line(self.rows[0], max_width),
|
||||||
|
self.product_line(dash, max_width),
|
||||||
|
] + [
|
||||||
|
self.product_line(row, max_width) for row in self.rows[1:]
|
||||||
|
]
|
||||||
|
return rows
|
||||||
|
|||||||
Reference in New Issue
Block a user