#!/usr/bin/env python3"""为代码块添加引用语根据清华大学出版社规范:- 代码用"代码如下:"引出- 命令用"命令如下:"引出"""import osimport redef is_command_block(code_content):"""判断是否为命令块"""# 常见的命令特征command_patterns = [r'^(npm|pnpm|yarn|bun|node|python|pip|git|docker|curl|wget|ssh|scp)',r'^(cd|ls|mkdir|rm|cp|mv|cat|grep|find|chmod|chown)',r'^(openclaw|clawhub)',r'^\$', # shell提示符r'^#', # root提示符]lines = code_content.strip().split('\n')if not lines:return Falsefirst_line = lines[0].strip()for pattern in command_patterns:if re.match(pattern, first_line, re.IGNORECASE):return Truereturn Falsedef add_code_references(filepath):"""为文件中的代码块添加引用语"""with open(filepath, 'r', encoding='utf-8') as f:content = f.read()# 查找所有代码块pattern = r'(^|\n)(```[\w]*\n)(.*?)(^```$)'def replace_code_block(match):prefix = match.group(1)code_start = match.group(2)code_content = match.group(3)code_end = match.group(4)# 检查前面是否已经有引用语before_code = content[:match.start()]last_lines = before_code.split('\n')[-3:] # 检查前3行has_reference = Falsefor line in last_lines:if '代码如下' in line or '命令如下' in line:has_reference = Truebreakif has_reference:# 已经有引用语,不添加return match.group(0)# 判断是代码还是命令if is_command_block(code_content):reference = "命令如下:\n\n"else:reference = "代码如下:\n\n"return prefix + reference + code_start + code_content + code_endnew_content = re.sub(pattern, replace_code_block, content, flags=re.MULTILINE | re.DOTALL)# 统计添加的数量added_count = new_content.count('代码如下:') + new_content.count('命令如下:') - \content.count('代码如下:') - content.count('命令如下:')if added_count > 0:with open(filepath, 'w', encoding='utf-8') as f:f.write(new_content)return added_countreturn 0def main():"""主函数"""print("开始为代码块添加引用语...")print()total_added = 0processed_files = 0for root, dirs, files in os.walk("docs"):for file in sorted(files):if file.endswith(".md") and not file.endswith(".emoji-backup"):filepath = os.path.join(root, file)added = add_code_references(filepath)if added > 0:print(f"✓ {filepath}: 添加了 {added} 个引用语")total_added += addedprocessed_files += 1print()print(f"完成! 共处理 {processed_files} 个文件,添加了 {total_added} 个引用语")if __name__ == "__main__":main()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。