Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ ignore = [
"PLR5501",

"UP035", # [*] Import from `collections.abc` instead: `Iterator`
"UP038", # [*] Use `X | Y` in `isinstance` call instead of `(X, Y)` (conflict with Pylint)
]

# Avoid trying to fix flake8-bugbear (`B`) violations.
Expand Down
2 changes: 1 addition & 1 deletion strictdoc/backend/sdoc/grammar_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def read_from_file(
if unpickled_content is not None:
return unpickled_content

with open(file_path, encoding="utf8") as file:
with open(file_path, encoding="utf-8-sig") as file:
grammar_content = file.read()

try:
Expand Down
7 changes: 6 additions & 1 deletion strictdoc/backend/sdoc/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from strictdoc.core.project_config import ProjectConfig
from strictdoc.helpers.cast import assert_cast
from strictdoc.helpers.exception import StrictDocException
from strictdoc.helpers.string import strip_bom
from strictdoc.helpers.textx import drop_textx_meta


Expand All @@ -36,6 +37,8 @@ def _read(
file_path: Optional[str] = None,
migrate_sections: bool = False,
) -> Tuple[SDocDocument, ParseContext]:
input_string = strip_bom(input_string)

parse_context = ParseContext(
path_to_sdoc_file=file_path, migrate_sections=migrate_sections
)
Expand Down Expand Up @@ -100,7 +103,9 @@ def read_from_file(
if unpickled_content:
return assert_cast(unpickled_content, SDocDocument)

with open(file_path, encoding="utf8") as file:
# utf-8-sig is important here because it strips the UTF BOM markers
# from the beginning of source files created on Windows.
with open(file_path, encoding="utf-8-sig") as file:
sdoc_content = file.read()

sdoc, parse_context = self.read_with_parse_context(
Expand Down
2 changes: 1 addition & 1 deletion strictdoc/backend/sdoc_source_code/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def read(
return source_file_traceability_info

def read_from_file(self, file_path: str) -> SourceFileTraceabilityInfo:
with open(file_path, encoding="utf-8") as file:
with open(file_path, encoding="utf-8-sig") as file:
sdoc_content = file.read()
sdoc = self.read(sdoc_content, file_path=file_path)
return sdoc
7 changes: 7 additions & 0 deletions strictdoc/helpers/string.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
REGEX_TRAILING_WHITESPACE_SINGLELINE = re.compile(r"\s{2,}")
REGEX_TRAILING_WHITESPACE_MULTILINE = re.compile(r" +\n")

UTF8_BOM = "\ufeff"


# WIP: Check if this is used.
def escape(string: str) -> str:
Expand Down Expand Up @@ -95,3 +97,8 @@ def tokenize(text: str) -> List[str]:
pattern = r"[a-z0-9]+(?:[-_./][a-z0-9]+)*"
tokens = re.findall(pattern, text)
return tokens


def strip_bom(s: str) -> str:
# U+FEFF is the BOM character when in str form
return s.lstrip(UTF8_BOM)
15 changes: 15 additions & 0 deletions tests/unit/strictdoc/backend/sdoc/test_dsl_passthrough.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,21 @@ def test_089_document_config_use_mid(default_project_config):
assert input_sdoc == output


def test_090_byte_order_mark_symbol_does_not_cause_parsing_errors():
input_sdoc = (
"\ufeff"
"""\
[DOCUMENT]
TITLE: Test Doc
"""
)

reader = SDReader()

document = reader.read(input_sdoc)
assert isinstance(document, SDocDocument)


def test__validation__30__composite_node_start_end_tags_do_not_match():
input_sdoc = """\
[DOCUMENT]
Expand Down
Loading