81 lines
2.8 KiB
Python
81 lines
2.8 KiB
Python
from app.engine.loaders.projectJson import *
|
|
|
|
class MarkDown:
|
|
def __init__(self,table:JsonTable,path:str) -> None:
|
|
self._table = table
|
|
self._path = path
|
|
|
|
def build(self):
|
|
flds:Dict[str,Field] = self._table.fields()
|
|
records:List[Record] = self._table.records()
|
|
columns:list = []
|
|
colComments:list = []
|
|
ignores:List[str] = []
|
|
for name,fld in flds.items():
|
|
if self._table.name() == '工程属性' and (name =='_id' or name =='nodeType' or name =='relTbId'):
|
|
ignores.append(name)
|
|
continue
|
|
|
|
columns.append(fld.value('name'))
|
|
colComments.append(fld.value('alias'))
|
|
|
|
rowdatas = []
|
|
for record in records:
|
|
datas = []
|
|
for col in columns:
|
|
if col in ignores:
|
|
continue
|
|
txt:str = record.value(col)
|
|
content = txt.replace('\n',"")
|
|
content = content.replace('\r',"")
|
|
datas.append(content)
|
|
rowdatas.append(datas)
|
|
|
|
content = self.convert(self._table.name(),self._table.comment(),columns,colComments,rowdatas)
|
|
with open(self._path, 'w',encoding='utf-8') as file:
|
|
file.write(content)
|
|
|
|
def convert(self,tableName:str,tableComment:str,columns:list,colComments:list,rowdatas:list):
|
|
strTitle = "# " + tableName + '\n'
|
|
if tableName!='':
|
|
strTitle+= f"备注:{tableComment}" + '\n'
|
|
|
|
for i in range(len(columns)):
|
|
strTitle+= f"- 字段名称:{columns[i]}" + '\n'
|
|
comment = colComments[i]
|
|
if comment!='':
|
|
strTitle+= f" - 备注:{comment}" + '\n'
|
|
|
|
markdown_table = "|"
|
|
# 添加列标题
|
|
markdown_table += "|".join(columns) + "|\n"
|
|
# 添加分隔行
|
|
markdown_table += "|" + "|".join(['---' for _ in columns]) + "|\n"
|
|
# 遍历每个数据行
|
|
for row in rowdatas:
|
|
# 添加数据行
|
|
markdown_table += "|" + "|".join(row) + "|\n"
|
|
return strTitle + "\n" + markdown_table
|
|
|
|
|
|
if __name__ == "__main__":
|
|
intputDir = 'C:\\Users\\wanyaokun\\Desktop\\markdown\\Project'
|
|
outputDir = 'C:\\Users\\wanyaokun\\Desktop\\markdown\\data'
|
|
|
|
subdirectories = {}
|
|
for dp, dn, fn in os.walk(intputDir):
|
|
for d in dn:
|
|
subdirectories[d] = os.path.join(dp, d)
|
|
for dirName,dirPath in subdirectories.items():
|
|
prjSon = ProjectJson(dirPath)
|
|
prjSon.parse()
|
|
tables = prjSon.tables()
|
|
outPut = os.path.join(outputDir,dirName)
|
|
os.makedirs(outPut,exist_ok=True)
|
|
for name,table in tables.items():
|
|
outputPath = os.path.join(outPut,f'{table.name()}.md')
|
|
mdObj = MarkDown(table,outputPath)
|
|
mdObj.build()
|
|
|
|
|