55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""规范第 6 章属性表解析(v3:去反斜杠 + 合并 strong)"""
|
||
import re, json, html, os
|
||
|
||
BASE = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||
MD = os.path.join(BASE, "DBJT15-145-2018_全文含表格.md")
|
||
OUT = os.path.join(BASE, "DBJT15-145-2018_第6章_属性表.json")
|
||
|
||
lines = open(MD, encoding="utf-8").read().split("\n")
|
||
text = "\n".join(lines[5707:18611])
|
||
t2 = text.replace("\\", "")
|
||
t2 = re.sub(r"</strong>\s*<strong>", "", t2)
|
||
t2 = re.sub(r"<strong>|</strong>", "", t2)
|
||
|
||
|
||
def strip_tags(s):
|
||
s = re.sub(r"<img[^>]*>", "", s)
|
||
s = re.sub(r"<br\s*/?>", " ", s)
|
||
s = re.sub(r"<[^>]+>", "", s)
|
||
s = s.replace(" ", " ")
|
||
return html.unescape(s).strip()
|
||
|
||
|
||
heads = []
|
||
for m in re.finditer(r"表\s*6\s*\.\s*(\d+)\s*\.\s*(\d+)\s*-?\s*(\d*)\s*([^<>\n]{0,60}?)属性定义表", t2):
|
||
no = f"6.{m.group(1)}.{m.group(2)}" + (f"-{m.group(3)}" if m.group(3) else "")
|
||
heads.append((m.start(), no, m.group(4).strip()))
|
||
|
||
result = []
|
||
for i, (pos, no, name) in enumerate(heads):
|
||
nxt = heads[i + 1][0] if i + 1 < len(heads) else len(t2)
|
||
chunk = t2[pos:nxt]
|
||
tbl = re.search(r"<table>(.*?)</table>", chunk, re.S)
|
||
rows = []
|
||
if tbl:
|
||
for r in re.findall(r"<tr>(.*?)</tr>", tbl.group(1), re.S):
|
||
cells = re.findall(r"<td[^>]*>(.*?)</td>", r, re.S)
|
||
vals = [strip_tags(c) for c in cells]
|
||
if vals:
|
||
rows.append(vals)
|
||
result.append({"no": no, "element": name.strip(), "rows": rows})
|
||
|
||
json.dump(result, open(OUT, "w", encoding="utf-8"), ensure_ascii=False, indent=1)
|
||
print("tables:", len(result))
|
||
tot = 0
|
||
nos = []
|
||
for t in result:
|
||
props = [r[1] for r in t["rows"][1:] if len(r) >= 2 and r[1] and r[1] != "属性名称"]
|
||
tot += len(props)
|
||
nos.append(t["no"])
|
||
print(f"{t['no']:10s} {t['element'][:40]:42s} props={len(props)}")
|
||
print("total prop defs:", tot)
|
||
missing = []
|
||
print("表号序列:", nos)
|