import argparse import sys import yaml def print_ir(yaml_path: str) -> None: with open(yaml_path, "r", encoding="utf-8") as f: data = yaml.safe_load(f) if not isinstance(data, list): print(f"Expected a YAML list, got {type(data).__name__}") sys.exit(1) for i, item in enumerate(data): yc = item.get("converted") if yc is None: continue # yaml_content is a raw string that happens to look like YAML — # parse it then pretty-print it back if isinstance(yc, str): stripped = yc.strip() # Strip markdown code fences: ```yaml / ``` at start, ``` at end if stripped.startswith("```"): first_newline = stripped.find("\n") stripped = stripped[first_newline + 1:] if first_newline != -1 else stripped[3:] if stripped.endswith("```"): stripped = stripped[:-3] try: parsed = yaml.safe_load(stripped) except yaml.YAMLError: print(yc) raise else: parsed = yc if isinstance(parsed, (dict, list)): print(yaml.dump(parsed, allow_unicode=True, default_flow_style=False, sort_keys=False).rstrip()) else: print(str(parsed)) print("=" * 60) if __name__ == "__main__": parser = argparse.ArgumentParser(description="Pretty-print yaml_content fields from an analysis YAML file.") parser.add_argument("input", metavar="file.yaml", help="Path to a _文字.yaml or _图片.yaml file") args = parser.parse_args() print_ir(args.input)