Files
MDHelper/mdhelper/code_structure.py
T

115 lines
4.6 KiB
Python
Raw Normal View History

2026-06-14 09:51:16 +08:00
from __future__ import annotations
import ast
import re
from pathlib import Path
2026-06-22 18:07:34 +08:00
from .document_tree import CPP_EXTENSIONS, FORTRAN_EXTENSIONS, PYTHON_EXTENSIONS
2026-06-14 09:51:16 +08:00
from .markdown_utils import Heading
from .text_io import DEFAULT_TEXT_PREVIEW_BYTES, read_text_with_fallback
_CPP_TYPE_RE = re.compile(r"^\s*(class|struct)\s+([A-Za-z_]\w*)\b")
_CPP_FUNCTION_RE = re.compile(
r"^\s*(?:template\s*<[^;{}]+>\s*)?(?:[A-Za-z_~][\w:<>~*&\s,]*\s+)?([A-Za-z_~][\w:]*)\s*\([^;{}]*\)\s*(?:const\s*)?(?:noexcept\s*)?(?:->\s*[^{};]+\s*)?(?:\{|$)"
)
_CPP_CONTROL_WORDS = {"if", "for", "while", "switch", "catch", "return", "sizeof"}
2026-06-22 18:07:34 +08:00
_FORTRAN_PROGRAM_RE = re.compile(r"^\s*program\s+([A-Za-z_]\w*)\b", re.IGNORECASE)
_FORTRAN_MODULE_RE = re.compile(r"^\s*module\s+(?!procedure\b)([A-Za-z_]\w*)\b", re.IGNORECASE)
_FORTRAN_SUBROUTINE_RE = re.compile(r"^\s*(?:(?:recursive|pure|elemental|impure)\s+)*subroutine\s+([A-Za-z_]\w*)\b", re.IGNORECASE)
_FORTRAN_FUNCTION_RE = re.compile(
r"^\s*(?:(?:recursive|pure|elemental|impure)\s+)*(?:(?:type|class)\s*\([^)]*\)\s+|(?:integer|real|double\s+precision|complex|logical|character)\b(?:\s*\([^)]*\))?\s+)?function\s+([A-Za-z_]\w*)\b",
re.IGNORECASE,
)
_FORTRAN_TYPE_RE = re.compile(r"^\s*type\s*(?:(?:,\s*[^:]*)?::\s*|\s+)([A-Za-z_]\w*)\b", re.IGNORECASE)
2026-06-14 09:51:16 +08:00
def extract_code_headings(path: Path, source: str | None = None) -> list[Heading]:
suffix = path.suffix.lower()
if source is None:
source = read_text_with_fallback(path, max_bytes=DEFAULT_TEXT_PREVIEW_BYTES)
if suffix in PYTHON_EXTENSIONS:
return _extract_python_headings(source)
if suffix in CPP_EXTENSIONS:
return _extract_cpp_headings(source)
2026-06-22 18:07:34 +08:00
if suffix in FORTRAN_EXTENSIONS:
return _extract_fortran_headings(source)
2026-06-14 09:51:16 +08:00
return []
def _extract_python_headings(source: str) -> list[Heading]:
try:
tree = ast.parse(source)
except SyntaxError:
return []
headings: list[Heading] = []
def walk(nodes: list[ast.stmt], level: int) -> None:
for node in nodes:
if isinstance(node, ast.ClassDef):
headings.append(Heading(level=min(level, 6), title=f"class {node.name}", slug=f"line-{node.lineno}"))
walk(node.body, min(level + 1, 6))
elif isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
prefix = "async def" if isinstance(node, ast.AsyncFunctionDef) else "def"
headings.append(Heading(level=min(level, 6), title=f"{prefix} {node.name}", slug=f"line-{node.lineno}"))
walk(node.body, min(level + 1, 6))
walk(list(tree.body), 1)
return headings
def _extract_cpp_headings(source: str) -> list[Heading]:
headings: list[Heading] = []
pending_template = False
for lineno, line in enumerate(source.splitlines(), start=1):
stripped = line.strip()
if not stripped or stripped.startswith(("//", "#")):
continue
if stripped.startswith("template"):
pending_template = True
continue
type_match = _CPP_TYPE_RE.match(line)
if type_match:
kind, name = type_match.groups()
headings.append(Heading(level=1, title=f"{kind} {name}", slug=f"line-{lineno}"))
pending_template = False
continue
function_line = f"template <> {line}" if pending_template else line
func_match = _CPP_FUNCTION_RE.match(function_line)
if not func_match:
pending_template = False
continue
name = func_match.group(1).split("::")[-1]
if name in _CPP_CONTROL_WORDS:
pending_template = False
continue
headings.append(Heading(level=2, title=f"function {func_match.group(1)}", slug=f"line-{lineno}"))
pending_template = False
return headings
2026-06-22 18:07:34 +08:00
def _extract_fortran_headings(source: str) -> list[Heading]:
headings: list[Heading] = []
matchers = (
("program", _FORTRAN_PROGRAM_RE, 1),
("module", _FORTRAN_MODULE_RE, 1),
("type", _FORTRAN_TYPE_RE, 2),
("subroutine", _FORTRAN_SUBROUTINE_RE, 2),
("function", _FORTRAN_FUNCTION_RE, 2),
)
for lineno, line in enumerate(source.splitlines(), start=1):
stripped = line.strip()
lowered = stripped.lower()
if not stripped or lowered.startswith("!") or lowered.startswith("end "):
continue
for kind, pattern, level in matchers:
match = pattern.match(line)
if match:
headings.append(Heading(level=level, title=f"{kind} {match.group(1)}", slug=f"line-{lineno}"))
break
return headings