| 1234567891011121314151617181920212223242526272829303132333435 |
- # 把规格书的组件化 Markdown(<Heading>/<Paragraph>/<Table>…)转成紧凑纯文本,便于阅读
- import re
- import sys
- src = sys.argv[1]
- dst = sys.argv[2]
- text = open(src, encoding='utf-8').read()
- # Mark bold/italic 保留语义
- text = re.sub(r'<Mark bold>(.*?)</Mark>', r'**\1**', text, flags=re.S)
- text = re.sub(r'<Mark italic>(.*?)</Mark>', r'_\1_', text, flags=re.S)
- text = re.sub(r'<Mark[^>]*>(.*?)</Mark>', r'\1', text, flags=re.S)
- # 代码块
- text = re.sub(r'<CodeBlock[^>]*>(.*?)</CodeBlock>', r'\n```\n\1\n```\n', text, flags=re.S)
- # 结构标记 → 文本标记
- text = re.sub(r'<Heading[^>]*level="(\d)"[^>]*>', lambda m: '\n' + '#' * int(m.group(1)) + ' ', text)
- text = re.sub(r'<Divider[^>]*/?>', '\n---\n', text)
- text = re.sub(r'<NumberedList[^>]*>', '\n- ', text)
- text = re.sub(r'<BulletList[^>]*>', '\n- ', text)
- text = re.sub(r'<TableRow[^>]*>', '\n| ', text)
- text = re.sub(r'<TableCell[^>]*>', ' ', text)
- text = re.sub(r'<Table[^>]*>', '\n', text)
- text = re.sub(r'<Paragraph[^>]*>', '\n', text)
- text = re.sub(r'<BlockQuote[^>]*>', '\n> ', text)
- text = re.sub(r'<Callout[^>]*>', '\n[!] ', text)
- # 其余块级标签换行,行内/闭合标签直接删
- text = re.sub(r'</(Heading|NumberedList|BulletList|TableRow|Table|Paragraph|BlockQuote|Callout)>', '\n', text)
- text = re.sub(r'<[^>]+>', '', text)
- text = text.replace('\\<', '<').replace('\\>', '>')
- text = re.sub(r'\n{3,}', '\n\n', text)
- open(dst, 'w', encoding='utf-8').write(text)
- print('lines=%d chars=%d' % (text.count('\n') + 1, len(text)))
|