# This file is part of OpenCV project. # It is subject to the license terms in the LICENSE file found in the top-level directory # of this distribution and at http://opencv.org/license.html. # Copyright (C) 2026, BigVision LLC, all rights reserved. # Third party copyrights are property of their respective owners. """Doxygen-flavored .markdown -> MyST translation (the source-read engine).""" from __future__ import annotations import pathlib, re, os as _os, shutil as _shutil, textwrap as _textwrap from .state import * def _normalize_lang(lang: str) -> str: lang = (lang or "").strip(".").strip().lower() or "text" return _LANG_ALIASES.get(lang, lang) def _read_snippet(rel_path: str, label: str | None) -> tuple[str, str]: """Return (code_text, language) for an @include / @snippet directive.""" rel_norm = rel_path.lstrip("/") p = next((b / rel_norm for b in _SNIPPET_BASES if (b / rel_norm).is_file()), None) # Doxygen EXAMPLE_RECURSIVE basename lookup. if p is None: hit = _SNIPPET_INDEX.get(pathlib.Path(rel_norm).name) if hit and hit.is_file(): p = hit if p is None: return f"// not found: {rel_path}\n", "text" text = p.read_text(encoding="utf-8", errors="replace") ext = p.suffix.lower() lang = {".cpp": "cpp", ".hpp": "cpp", ".h": "cpp", ".c": "c", ".py": "python", ".java": "java", ".xml": "xml", ".html": "html", ".sh": "bash", ".bash": "bash"}.get(ext, "text") if label is None: return text, lang # Match `[label]` after any comment marker. pat = re.compile(r"^[^\[\n]*(?://!|//|##|#|" raw = p.read_text(encoding="utf-8", errors="replace") # Allow quoted attrs so a `=>` inside onload="..." doesn't end the tag. body = re.search( r"\"']|\"[^\"]*\"|'[^']*')*>(.*?)", raw, re.DOTALL | re.IGNORECASE) inner = body.group(1).strip() if body else raw return f"\n```{{raw}} html\n{inner}\n```\n" text = re.sub(r"^@htmlinclude\s+(?P\S+)\s*$", _htmlinclude_repl, text, flags=re.MULTILINE) # 5. @snippet path [Label] def _snippet_repl(m: re.Match) -> str: code, lang = _read_snippet(m.group("path"), m.group("label")) return _emit_codeblock(m.group("indent") or "", lang, code) text = re.sub( r"^(?P[ \t]*)@snippet\s+(?P\S+)\s+(?P