无损检测期末复习笔记:十天章节笔记、考点精编、三套模拟卷(带Obsidian双链)
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import json
|
||||
import sys
|
||||
import os
|
||||
|
||||
def extract_text_from_content(content_list):
|
||||
"""Recursively extract text from content structure"""
|
||||
result = []
|
||||
for item in content_list:
|
||||
if isinstance(item, dict):
|
||||
if item.get('type') == 'text' and 'content' in item:
|
||||
text = item['content']
|
||||
if isinstance(text, str):
|
||||
result.append(text)
|
||||
elif 'content' in item:
|
||||
result.extend(extract_text_from_content(item['content']))
|
||||
return result
|
||||
|
||||
def json_to_markdown(json_path):
|
||||
with open(json_path, 'r', encoding='utf-8') as f:
|
||||
data = json.load(f)
|
||||
|
||||
md_lines = []
|
||||
for page in data['pages']:
|
||||
page_idx = page['page_idx']
|
||||
md_lines.append(f"\n<!-- Page {page_idx + 1} -->\n")
|
||||
for block in page['blocks']:
|
||||
btype = block.get('type', 'text')
|
||||
texts = extract_text_from_content(block.get('content', []))
|
||||
full_text = ''.join(texts).strip()
|
||||
|
||||
if not full_text:
|
||||
continue
|
||||
|
||||
if btype == 'doc_title':
|
||||
level = block.get('level', 1)
|
||||
md_lines.append(f"{'#' * level} {full_text}\n")
|
||||
elif btype == 'text':
|
||||
md_lines.append(full_text + "\n")
|
||||
elif btype == 'header':
|
||||
md_lines.append(f"<!-- header: {full_text} -->\n")
|
||||
elif btype == 'footer':
|
||||
md_lines.append(f"<!-- footer: {full_text} -->\n")
|
||||
elif btype == 'page_number':
|
||||
md_lines.append(f"<!-- page: {full_text} -->\n")
|
||||
else:
|
||||
md_lines.append(full_text + "\n")
|
||||
|
||||
return '\n'.join(md_lines)
|
||||
|
||||
if __name__ == '__main__':
|
||||
json_path = sys.argv[1]
|
||||
md = json_to_markdown(json_path)
|
||||
print(md)
|
||||
Reference in New Issue
Block a user