74 lines
3.2 KiB
Python
74 lines
3.2 KiB
Python
import json, re, os, glob
|
|
|
|
input_dir = r'D:\desktop\feishu_docs_summary\output\feishu-docs'
|
|
output_dir = r'D:\desktop\feishu_docs_summary\output\feishu-docs\md'
|
|
os.makedirs(output_dir, exist_ok=True)
|
|
|
|
def html_to_md(html):
|
|
if not html:
|
|
return ''
|
|
text = html
|
|
text = re.sub(r'<img[^>]*alt="([^"]*?)"[^>]*/>', r'[图片: \1]', text)
|
|
text = re.sub(r'<img[^>]*/>', '[图片]', text)
|
|
text = re.sub(r'<sheet[^>]*/>', '[表格]', text)
|
|
text = re.sub(r'</?grid[^>]*>', '', text)
|
|
text = re.sub(r'<column[^>]*>', '', text)
|
|
text = re.sub(r'</column>', '', text)
|
|
text = re.sub(r'<latex[^>]*>(.*?)</latex>', r'$\1$', text, flags=re.DOTALL)
|
|
text = re.sub(r'<h1[^>]*>(.*?)</h1>', r'# \1', text, flags=re.DOTALL)
|
|
text = re.sub(r'<h2[^>]*>(.*?)</h2>', r'## \1', text, flags=re.DOTALL)
|
|
text = re.sub(r'<h3[^>]*>(.*?)</h3>', r'### \1', text, flags=re.DOTALL)
|
|
text = re.sub(r'<h4[^>]*>(.*?)</h4>', r'#### \1', text, flags=re.DOTALL)
|
|
text = re.sub(r'<b>(.*?)</b>', r'**\1**', text, flags=re.DOTALL)
|
|
text = re.sub(r'<strong>(.*?)</strong>', r'**\1**', text, flags=re.DOTALL)
|
|
text = re.sub(r'<i>(.*?)</i>', r'*\1*', text, flags=re.DOTALL)
|
|
text = re.sub(r'<em>(.*?)</em>', r'*\1*', text, flags=re.DOTALL)
|
|
text = re.sub(r'<a[^>]*href="([^"]*?)"[^>]*>(.*?)</a>', r'[\2](\1)', text, flags=re.DOTALL)
|
|
text = re.sub(r'<ol[^>]*>', '', text)
|
|
text = re.sub(r'</ol>', '', text)
|
|
text = re.sub(r'<ul[^>]*>', '', text)
|
|
text = re.sub(r'</ul>', '', text)
|
|
text = re.sub(r'<li[^>]*>', '- ', text)
|
|
text = re.sub(r'</li>', '', text)
|
|
text = re.sub(r'<blockquote[^>]*>', '> ', text)
|
|
text = re.sub(r'</blockquote>', '', text)
|
|
text = re.sub(r'<p[^>]*>', '', text)
|
|
text = re.sub(r'</p>', '\n', text)
|
|
text = re.sub(r'<hr[^>]*/>', '\n---\n', text)
|
|
text = re.sub(r'<code>(.*?)</code>', r'`\1`', text, flags=re.DOTALL)
|
|
text = re.sub(r'<[^>]+>', '', text)
|
|
text = re.sub(r'\n{3,}', '\n\n', text)
|
|
return text.strip()
|
|
|
|
count = 0
|
|
for f in sorted(glob.glob(os.path.join(input_dir, '*.json'))):
|
|
try:
|
|
with open(f, 'r', encoding='utf-8') as fh:
|
|
raw = fh.read()
|
|
lines = raw.split('\n')
|
|
json_lines = [l for l in lines if not l.startswith('2026/')]
|
|
data = json.loads('\n'.join(json_lines))
|
|
if not data.get('ok'):
|
|
print(f'SKIP: {os.path.basename(f)}')
|
|
continue
|
|
doc = data['data']['document']
|
|
doc_id = doc['document_id']
|
|
title = ''
|
|
content_html = doc['content']
|
|
title_match = re.search(r'<title>(.*?)</title>', content_html)
|
|
if title_match:
|
|
title = title_match.group(1)
|
|
md_content = html_to_md(content_html)
|
|
md_path = os.path.join(output_dir, f'{doc_id}.md')
|
|
with open(md_path, 'w', encoding='utf-8') as fh:
|
|
if title:
|
|
fh.write(f'# {title}\n\n')
|
|
fh.write(md_content)
|
|
fh.write(f'\n\n---\n*Source: feishu doc {doc_id}*\n')
|
|
print(f'OK: {doc_id} -> {title[:60] if title else "(untitled)"} ({len(md_content)} chars)')
|
|
count += 1
|
|
except Exception as e:
|
|
print(f'ERROR: {os.path.basename(f)}: {e}')
|
|
|
|
print(f'\nTotal: {count}')
|