feat: add Fortran support and tab controls
This commit is contained in:
@@ -4,7 +4,7 @@ import ast
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
from .document_tree import CPP_EXTENSIONS, PYTHON_EXTENSIONS
|
||||
from .document_tree import CPP_EXTENSIONS, FORTRAN_EXTENSIONS, PYTHON_EXTENSIONS
|
||||
from .markdown_utils import Heading
|
||||
from .text_io import DEFAULT_TEXT_PREVIEW_BYTES, read_text_with_fallback
|
||||
|
||||
@@ -13,6 +13,14 @@ _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"}
|
||||
_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)
|
||||
|
||||
|
||||
def extract_code_headings(path: Path, source: str | None = None) -> list[Heading]:
|
||||
@@ -23,6 +31,8 @@ def extract_code_headings(path: Path, source: str | None = None) -> list[Heading
|
||||
return _extract_python_headings(source)
|
||||
if suffix in CPP_EXTENSIONS:
|
||||
return _extract_cpp_headings(source)
|
||||
if suffix in FORTRAN_EXTENSIONS:
|
||||
return _extract_fortran_headings(source)
|
||||
return []
|
||||
|
||||
|
||||
@@ -78,3 +88,27 @@ def _extract_cpp_headings(source: str) -> list[Heading]:
|
||||
headings.append(Heading(level=2, title=f"function {func_match.group(1)}", slug=f"line-{lineno}"))
|
||||
pending_template = False
|
||||
return headings
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user