StrictDoc Documentation
strictdoc/backend/sdoc/models/anchor.py
Source file coverage
Path:
strictdoc/backend/sdoc/models/anchor.py
Lines:
70
Non-empty lines:
56
Non-empty lines covered with requirements:
56 / 56 (100.0%)
Functions:
8
Functions covered by requirements:
8 / 8 (100.0%)
1
"""
2
@relation(SDOC-SRS-150, scope=file)
3
"""
4
 
5
# mypy: disable-error-code="unreachable"
6
 
7
from typing import Optional
8
 
9
from strictdoc.backend.sdoc.models.model import (
10
    SDocDocumentIF,
11
    SDocNodeFieldIF,
12
    SDocNodeIF,
13
)
14
from strictdoc.helpers.auto_described import auto_described
15
from strictdoc.helpers.cast import assert_cast
16
from strictdoc.helpers.mid import MID
17
 
18
 
19
@auto_described
20
class Anchor:
21
    def __init__(
22
        self, parent: SDocNodeFieldIF, value: str, title: Optional[str]
23
    ) -> None:
24
        # FIXME: Cannot enable this assert because the parent can also be
25
        #        FreeTextContainer. Refactor the types.
26
        # assert isinstance(parent, SDocNodeFieldIF), parent  # noqa: ERA001
27
 
28
        self.parent: SDocNodeFieldIF = parent
29
        self.value: str = value
30
 
31
        if title is not None and len(title) > 0:
32
            self.title: str = title.strip('"')
33
            self.has_title = True
34
        else:
35
            self.title = value
36
            self.has_title = False
37
 
38
        # FIXME: Remove either mid or reserved_mid.
39
        self.mid: MID = MID.create()
40
        self.reserved_mid: MID = self.mid
41
 
42
    def get_display_title(
43
        self,
44
        include_toc_number: bool = True,  # noqa: ARG002
45
    ) -> str:
46
        return self.title
47
 
48
    def get_source_title(self) -> str:
49
        if "," in self.title or "]" in self.title:
50
            return f'"{self.title}"'
51
        return self.title
52
 
53
    def get_document(self) -> SDocDocumentIF:
54
        if isinstance(self.parent.parent, SDocDocumentIF):
55
            return self.parent.parent
56
        document: SDocDocumentIF = assert_cast(
57
            self.parent_node().get_document(), SDocDocumentIF
58
        )
59
        return document
60
 
61
    def get_parent_or_including_document(self) -> SDocDocumentIF:
62
        return self.parent_node().get_parent_or_including_document()
63
 
64
    def get_including_document(self) -> Optional[SDocDocumentIF]:
65
        return self.parent_node().get_including_document()
66
 
67
    def parent_node(self) -> SDocNodeIF:
68
        # Anchor -> SDocField -> SDocNode.
69
        parent_node: SDocNodeIF = assert_cast(self.parent.parent, SDocNodeIF)
70
        return parent_node