"""测 extractUrls 认不认各种 URL 写法。 ★ 为什么值得单独一个脚本: 用户报「操作栏里应该有『打开网页』按钮,但没显示」。 根因是 extractUrls 只认 `](` 紧挨着的 markdown 链接, **裸 URL(`见 https://x.com`)这种模型最常写的形式反而漏掉** —— 返回空 → 按钮整个不显示。 ★ 手法:**直接从 Kotlin 源码里把正则抠出来**,用 Python 的 re 跑 (两边正则语法基本一致)。这样测的是真代码,不是"我凭记忆重写一遍"—— 第一版这个探针就是重写的,改了 Kotlin 之后它还在报旧结果,等于没测。 """ import os import re import sys try: sys.stdout.reconfigure(encoding="utf-8", errors="replace") except Exception: pass WS = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SRC = os.path.join(WS, "android", "app", "src", "main", "java", "com", "doubao", "lynxclone", "ui", "MarkdownText.kt") src = open(SRC, encoding="utf-8").read() def kotlin_regex(name: str) -> str: # ★ `\s*` 是必须的:源码里 `RE_MD_LINK =` 后面换行过(行太长), # 不加的话提取不到 —— 而报的是"找不到 RE_MD_LINK", # 看起来像"改了名字",其实是格式问题。 m = re.search(rf'private val {name}\s*=\s*\n?\s*Regex\("""(.*?)"""\)', src, re.DOTALL) if not m: print(f"!! 源码里找不到 {name}") sys.exit(1) return m.group(1).strip() RE_MD = kotlin_regex("RE_MD_LINK") RE_BARE = kotlin_regex("RE_BARE_URL") print("RE_MD_LINK =", RE_MD) print("RE_BARE_URL =", RE_BARE) print() # Kotlin 的 `[^\]\n]` 在 Python 里要写成 `[^\]\n]`(一样) CLEAN_TAIL = "。,、;:!?)】》」』….,;:!?" def clean(raw: str) -> str: """ 和 Kotlin 的 cleanUrl 等价。 ★ 注意这里**没有**处理"成对括号保留"那条(Kotlin 里有)。 `https://en.wikipedia.org/wiki/A_(b)` 正则已经整个吃掉、不需要补; Kotlin 那段是给"正则提前截断"的情况兜底的。 测这一条时两边结果会一致,所以不影响。 """ s = raw.strip().strip("<>\"'`") s = s.rstrip(CLEAN_TAIL) while s.endswith(")") and s.count(")") > s.count("("): s = s[:-1] while s.endswith("]") and s.count("]") > s.count("["): s = s[:-1] if not (s.startswith("http://") or s.startswith("https://")): return "" return s def extract(markdown: str): if "http://" not in markdown and "https://" not in markdown: return [] out, labels = [], {} for m in re.finditer(RE_MD, markdown): label = m.group(1).strip().strip("[]") url = clean(m.group(2)) if url: labels.setdefault(url, label) if url not in out: out.append(url) for m in re.finditer(RE_BARE, markdown): url = clean(m.group(0)) if url and url not in out: out.append(url) return [(u, labels.get(u, "")) for u in out] CASES = [ ("标准 markdown", "[文档](https://example.com/a)", 1), ("文字和括号之间有空格", "[文档] (https://example.com/a)", 1), ("括号里有空格", "[文档]( https://example.com/a )", 1), ("标题里有方括号", "[A[1]](https://example.com/a)", 1), ("★ 裸 URL", "直接访问 https://example.com/a 就行", 1), ("★ 中文标点紧跟", "见(https://example.com/a)。", 1), ("★ 尖括号包裹", "", 1), ("★ 行尾的裸 URL", "链接是 https://example.com/a", 1), ("★ 换行里的裸 URL", "第一行\nhttps://example.com/a\n第三行", 1), ("多个链接", "[A](https://a.com) 和 [B](https://b.com)", 2), ("markdown + 裸混排", "[A](https://a.com) 还有 https://b.com", 2), ("同链接出现两次只算一个", "[A](https://a.com) 又说 https://a.com", 1), ("中文标点不被吞进地址", "看 https://example.com/a,谢谢", 1), ("带括号的地址", "[x](https://en.wikipedia.org/wiki/A_(b))", 1), ("没有链接", "这段话里一个网址都没有", 0), ("相对路径不算", "[文档](docs/x.md)", 0), ] print("=== 认得多少 ===") miss = 0 for label, text, want in CASES: got = extract(text) okc = len(got) == want if not okc: miss += 1 detail = ", ".join(f"{u}" + (f"[{l}]" if l else "") for u, l in got) or "(空)" print(f" {'✓' if okc else '✗'} {label:22s} 期望{want} 得到{len(got)}: {detail}") # 链接文字要留住(列表里靠它区分同域名的多个链接) got = extract("[Example 官网](https://example.com) 和 [Example 中文](https://example.com/index-zh)") print() if len(got) == 2 and all(l for _, l in got): print(" ✓ 两条同域链接都拿到了各自的文字(列表里能分得清)") else: print(" ✗ 链接文字丢了:", got) miss += 1 print() if miss: print(f"❌ {miss} 项不合格") sys.exit(1) print("✅ 全部通过")