Skip to content

pymdownx_superfence_filter_lines

pymdownx_superfence_filter_lines ¤

A custom superfence for pymdown-extensions that can filters lines and plays nice with MkDocs.

Classes¤

pymdownx_superfence_filter_lines.Inputs ¤

Bases: TypedDict

Raw input options before they are validated.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
class Inputs(TypedDict):
    """Raw input options before they are validated."""

    show_lines: NotRequired[str]
    """Lines to show option."""
Attributes¤
show_lines instance-attribute ¤
show_lines: NotRequired[str]

Lines to show option.

pymdownx_superfence_filter_lines.LinesRange dataclass ¤

A range of lines.

start and end are inclusive and the indices start from 1.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
@dataclasses.dataclass(frozen=True, kw_only=True)
class LinesRange:
    """A range of lines.

    `start` and `end` are inclusive and the indices start from 1.
    """

    start: int | None = None
    """Start line.

    `None` means the beginning of the file.
    """

    end: int | None = None
    """End line.

    `None` means the end of the file.
    """

    def __post_init__(self) -> None:
        """Validate inputs upon creation."""
        if self.start is None and self.end is None:
            raise ValueError("Cannot have both start and end as `None`")
        if self.start is not None and self.start < 1:
            raise ValueError("Start must be at least 1")
        if self.end is not None and self.end < 1:
            raise ValueError("End must be at least 1")
        if self.start is not None and self.end is not None and self.start > self.end:
            raise ValueError("Start must be less than or equal to end")

    def __contains__(self, item: int) -> bool:
        """Whether the item is inside this range."""
        if self.start is None:
            assert self.end is not None
            return item <= self.end

        if self.end is None:
            return self.start <= item

        return self.start <= item <= self.end

    @classmethod
    def parse(cls, text: str) -> Self:
        """Create from a string.

        The string can be in the following formats:

        - `start`
        - `start:end`
        - `:end`
        - `start:`

        where `start` and `end` are integers.

        If `start` is empty, it is assumed to be 1.
        If `end` is empty, it is assumed to be the end of the file.
        If `start` appears without `:`, it is assumed to be both `start` and `end`.

        Lines are 1-indexed.

        Args:
            text: String to parse.

        Returns:
            The created range.

        Raises:
            ValueError: If the string is invalid.
        """
        splitted = text.split(":", 1)
        stripped = map(lambda s: s.strip(), splitted)
        match tuple(map(lambda s: s if s else None, stripped)):
            case (None,):
                raise ValueError("Empty start")
            case () | (None, None):
                raise ValueError("Both start and end are empty")
            case (str() as start,):
                return cls(start=int(start), end=int(start))
            case (str() as start, None):
                return cls(start=int(start))
            case (None, str() as end):
                return cls(end=int(end))
            case (str() as start, str() as end):
                return cls(start=int(start), end=int(end))
            case _ as invalid:
                raise ValueError(f"Invalid: {invalid!r}")

    def __str__(self) -> str:
        """Get the string representation."""
        if self.start is None:
            assert self.end is not None
            return f":{self.end}"
        if self.end is None:
            return f"{self.start}:"
        return f"{self.start}:{self.end}"
Attributes¤
end class-attribute instance-attribute ¤
end: int | None = None

End line.

None means the end of the file.

start class-attribute instance-attribute ¤
start: int | None = None

Start line.

None means the beginning of the file.

Methods:¤
__contains__ ¤
__contains__(item: int) -> bool

Whether the item is inside this range.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
def __contains__(self, item: int) -> bool:
    """Whether the item is inside this range."""
    if self.start is None:
        assert self.end is not None
        return item <= self.end

    if self.end is None:
        return self.start <= item

    return self.start <= item <= self.end
__post_init__ ¤
__post_init__() -> None

Validate inputs upon creation.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
def __post_init__(self) -> None:
    """Validate inputs upon creation."""
    if self.start is None and self.end is None:
        raise ValueError("Cannot have both start and end as `None`")
    if self.start is not None and self.start < 1:
        raise ValueError("Start must be at least 1")
    if self.end is not None and self.end < 1:
        raise ValueError("End must be at least 1")
    if self.start is not None and self.end is not None and self.start > self.end:
        raise ValueError("Start must be less than or equal to end")
__str__ ¤
__str__() -> str

Get the string representation.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
def __str__(self) -> str:
    """Get the string representation."""
    if self.start is None:
        assert self.end is not None
        return f":{self.end}"
    if self.end is None:
        return f"{self.start}:"
    return f"{self.start}:{self.end}"
parse classmethod ¤
parse(text: str) -> Self

Create from a string.

The string can be in the following formats:

  • start
  • start:end
  • :end
  • start:

where start and end are integers.

If start is empty, it is assumed to be 1. If end is empty, it is assumed to be the end of the file. If start appears without :, it is assumed to be both start and end.

Lines are 1-indexed.

PARAMETER DESCRIPTION
text

String to parse.

TYPE: str

RETURNS DESCRIPTION
Self

The created range.

RAISES DESCRIPTION
ValueError

If the string is invalid.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
@classmethod
def parse(cls, text: str) -> Self:
    """Create from a string.

    The string can be in the following formats:

    - `start`
    - `start:end`
    - `:end`
    - `start:`

    where `start` and `end` are integers.

    If `start` is empty, it is assumed to be 1.
    If `end` is empty, it is assumed to be the end of the file.
    If `start` appears without `:`, it is assumed to be both `start` and `end`.

    Lines are 1-indexed.

    Args:
        text: String to parse.

    Returns:
        The created range.

    Raises:
        ValueError: If the string is invalid.
    """
    splitted = text.split(":", 1)
    stripped = map(lambda s: s.strip(), splitted)
    match tuple(map(lambda s: s if s else None, stripped)):
        case (None,):
            raise ValueError("Empty start")
        case () | (None, None):
            raise ValueError("Both start and end are empty")
        case (str() as start,):
            return cls(start=int(start), end=int(start))
        case (str() as start, None):
            return cls(start=int(start))
        case (None, str() as end):
            return cls(end=int(end))
        case (str() as start, str() as end):
            return cls(start=int(start), end=int(end))
        case _ as invalid:
            raise ValueError(f"Invalid: {invalid!r}")

pymdownx_superfence_filter_lines.LinesRanges dataclass ¤

A set of line ranges.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
@dataclasses.dataclass(frozen=True)
class LinesRanges:
    """A set of line ranges."""

    ranges: Set[LinesRange]
    """The lines ranges."""

    def __post_init__(self) -> None:
        """Validate."""
        if not self.ranges:
            raise ValueError("Cannot have empty ranges")

    def __contains__(self, item: int) -> bool:
        """Whether the item is inside any of the ranges."""
        return any(item in r for r in self.ranges)

    @classmethod
    def parse(cls, text: str) -> tuple[Self | None, list[ValueError]]:
        """Create from a string.

        The string must be a comma-separated list of ranges, where each range is in the
        format described in
        [`LinesRange.parse`][pymdownx_superfence_filter_lines.LinesRange.parse].

        If no ranges are given, `None` is returned. If any ranges are invalid, they are
        ignored and a list of errors is returned.

        Args:
            text: String to parse.

        Returns:
            The created ranges, or `None` if no ranges are given, plus a list of errors,
                if any.
        """
        ranges: set[LinesRange] = set()
        errors: list[ValueError] = []
        for n, range_str in enumerate(text.split(","), start=1):
            try:
                lines_range = LinesRange.parse(range_str.strip())
            except ValueError as exc:
                error = ValueError(f"Range {n} ({range_str!r}) is invalid: {exc}")
                error.__cause__ = exc
                errors.append(error)
                continue
            ranges.add(lines_range)
        return cls(ranges) if ranges else None, errors

    def __str__(self) -> str:
        """Get the string representation."""
        return ",".join(map(str, self.ranges))
Attributes¤
ranges instance-attribute ¤
ranges: Set[LinesRange]

The lines ranges.

Methods:¤
__contains__ ¤
__contains__(item: int) -> bool

Whether the item is inside any of the ranges.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
def __contains__(self, item: int) -> bool:
    """Whether the item is inside any of the ranges."""
    return any(item in r for r in self.ranges)
__post_init__ ¤
__post_init__() -> None

Validate.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
def __post_init__(self) -> None:
    """Validate."""
    if not self.ranges:
        raise ValueError("Cannot have empty ranges")
__str__ ¤
__str__() -> str

Get the string representation.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
def __str__(self) -> str:
    """Get the string representation."""
    return ",".join(map(str, self.ranges))
parse classmethod ¤
parse(text: str) -> tuple[Self | None, list[ValueError]]

Create from a string.

The string must be a comma-separated list of ranges, where each range is in the format described in LinesRange.parse.

If no ranges are given, None is returned. If any ranges are invalid, they are ignored and a list of errors is returned.

PARAMETER DESCRIPTION
text

String to parse.

TYPE: str

RETURNS DESCRIPTION
tuple[Self | None, list[ValueError]]

The created ranges, or None if no ranges are given, plus a list of errors, if any.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
@classmethod
def parse(cls, text: str) -> tuple[Self | None, list[ValueError]]:
    """Create from a string.

    The string must be a comma-separated list of ranges, where each range is in the
    format described in
    [`LinesRange.parse`][pymdownx_superfence_filter_lines.LinesRange.parse].

    If no ranges are given, `None` is returned. If any ranges are invalid, they are
    ignored and a list of errors is returned.

    Args:
        text: String to parse.

    Returns:
        The created ranges, or `None` if no ranges are given, plus a list of errors,
            if any.
    """
    ranges: set[LinesRange] = set()
    errors: list[ValueError] = []
    for n, range_str in enumerate(text.split(","), start=1):
        try:
            lines_range = LinesRange.parse(range_str.strip())
        except ValueError as exc:
            error = ValueError(f"Range {n} ({range_str!r}) is invalid: {exc}")
            error.__cause__ = exc
            errors.append(error)
            continue
        ranges.add(lines_range)
    return cls(ranges) if ranges else None, errors

pymdownx_superfence_filter_lines.Options ¤

Bases: TypedDict

Raw options before they are validated.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
class Options(TypedDict):
    """Raw options before they are validated."""

    show_lines: NotRequired[LinesRanges]
    """Lines to show."""
Attributes¤
show_lines instance-attribute ¤
show_lines: NotRequired[LinesRanges]

Lines to show.

Functions:¤

pymdownx_superfence_filter_lines.do_format ¤

do_format(
    src: str,
    language: str,
    class_name: str,
    options: Options,
    md: Markdown,
    **kwargs: Any
) -> Any

Filter the lines and run the default highlighter.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
def do_format(
    src: str,
    language: str,
    class_name: str,
    options: Options,
    md: markdown.Markdown,
    **kwargs: Any,
) -> Any:
    """Filter the lines and run the default highlighter."""
    # Filter the lines to show
    if show_lines := options.get("show_lines"):
        lines = []
        for n, line in enumerate(src.splitlines(keepends=True), 1):
            if n in show_lines:
                lines.append(line)
        src = "".join(lines)

    # Run through default highlighter
    fenced_code_block = md.preprocessors["fenced_code_block"]
    assert isinstance(fenced_code_block, SuperFencesBlockPreprocessor)
    return fenced_code_block.highlight(
        src=src,
        class_name=class_name,
        language=language,
        md=md,
        options=options,
        **kwargs,
    )

pymdownx_superfence_filter_lines.do_validate ¤

do_validate(
    language: str,
    inputs: Inputs,
    options: Options,
    attrs: dict[str, Any],
    md: Markdown,
) -> bool

Validate the inputs.

Source code in src/pymdownx_superfence_filter_lines/__init__.py
def do_validate(
    language: str,
    inputs: Inputs,
    options: Options,
    attrs: dict[str, Any],
    md: markdown.Markdown,
) -> bool:
    """Validate the inputs."""
    # Parse `show_lines` option
    if show_lines_option := inputs.get("show_lines"):
        lines_ranges, errors = LinesRanges.parse(show_lines_option)

        for error in errors:
            _warn(
                "Invalid `show_lines` option in %r, some lines will not be filtered: %s",
                show_lines_option,
                error,
            )

        if lines_ranges:
            options["show_lines"] = lines_ranges

        # Remove handled option from inputs
        del inputs["show_lines"]

    # Run default validator
    return cast(bool, highlight_validator(language, inputs, options, attrs, md))