Skip to content

Commit a17b943

Browse files
author
yangjian
committed
修复文集导出文件mrdoc.yaml文件结构不正确的问题
1 parent fe46c5d commit a17b943

1 file changed

Lines changed: 27 additions & 51 deletions

File tree

app_doc/report_utils.py

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -78,59 +78,35 @@ def work(self):
7878
project_toc_list['project_role'] = self.project_data.role
7979
project_toc_list['toc'] = []
8080
# 读取指定文集的文档数据
81-
data = Doc.objects.filter(top_doc=self.pro_id, parent_doc=0).order_by("sort")
82-
# 遍历一级文档
83-
for d in data:
84-
top_item = {
85-
'name': validate_title(d.name),
86-
'file': validate_title(d.name)+'.md',
87-
}
88-
md_name = validate_title(d.name) # 文档名称
89-
# 文档内容,如果使用Markdown编辑器编写则导出Markdown文本,如果使用富文本编辑器编写则导出HTML文本
90-
md_content = self.operat_md_media(d.pre_content) \
91-
if d.editor_mode in [1,2] else self.operat_md_media(d.content)
92-
81+
data = Doc.objects.filter(
82+
top_doc=self.pro_id,
83+
status=1
84+
).order_by('sort', 'create_time').values(
85+
'name', 'editor_mode', 'pre_content', 'content', 'parent_doc', 'id'
86+
)
87+
if data.count() == 0:
88+
return None
89+
out = {}
90+
for p in data:
91+
doc_pre_content = p['pre_content']
92+
doc_content = p['content']
93+
p['name'] = validate_title(p['name'])
94+
p['file'] = '{}-{}.md'.format(validate_title(p['name']), p['id'])
95+
del p['pre_content']
96+
del p['content']
97+
out.setdefault(p['parent_doc'], {'children': []})
98+
out.setdefault(p['id'], {'children': []})
99+
out[p['id']].update(p)
100+
out[p['parent_doc']]['children'].append(out[p['id']])
101+
102+
# 处理文档内的图片,如果使用Markdown编辑器编写则导出Markdown文本,如果使用富文本编辑器编写则导出HTML文本
103+
md_content = self.operat_md_media(doc_content) \
104+
if p['editor_mode'] in [3] else self.operat_md_media(doc_pre_content)
93105
# 新建MD文件
94-
with open('{}/{}.md'.format(self.project_path,md_name),'w',encoding='utf-8') as files:
106+
file_path = '{}/{}-{}.md'.format(self.project_path, p['name'], p['id'])
107+
with open(file_path, 'w', encoding='utf-8') as files:
95108
files.write(md_content)
96-
97-
# 查询二级文档
98-
data_2 = Doc.objects.filter(parent_doc=d.id).order_by("sort")
99-
if data_2.count() > 0:
100-
top_item['children'] = []
101-
for d2 in data_2:
102-
sec_item = {
103-
'name': validate_title(d2.name),
104-
'file': validate_title(d2.name)+'.md',
105-
}
106-
107-
md_name_2 = validate_title(d2.name)
108-
md_content_2 = self.operat_md_media(d2.pre_content) \
109-
if d2.editor_mode in [1,2] else self.operat_md_media(d2.content)
110-
111-
# 新建MD文件
112-
with open('{}/{}.md'.format(self.project_path, md_name_2), 'w', encoding='utf-8') as files:
113-
files.write(md_content_2)
114-
115-
# 获取第三级文档
116-
data_3 = Doc.objects.filter(parent_doc=d2.id).order_by("sort")
117-
if data_3.count() > 0:
118-
sec_item['children'] = []
119-
for d3 in data_3:
120-
item = {
121-
'name': validate_title(d3.name),
122-
'file': validate_title(d3.name)+'.md',
123-
}
124-
sec_item['children'].append(item)
125-
md_name_3 = validate_title(d3.name)
126-
md_content_3 = self.operat_md_media(d3.pre_content) \
127-
if d3.editor_mode in [1,2] else self.operat_md_media(d3.content)
128-
129-
# 新建MD文件
130-
with open('{}/{}.md'.format(self.project_path, md_name_3), 'w', encoding='utf-8') as files:
131-
files.write(md_content_3)
132-
top_item['children'].append(sec_item)
133-
project_toc_list['toc'].append(top_item)
109+
project_toc_list['toc'] = out[0]['children']
134110

135111
# 写入层级YAML
136112
with open('{}/mrdoc.yaml'.format(self.project_path), 'a+', encoding='utf-8') as toc_yaml:

0 commit comments

Comments
 (0)