From 45d0dd1ed5202d40b97b154072fc371c473d4e56 Mon Sep 17 00:00:00 2001 From: Gianluca Marraffa Date: Wed, 31 Jan 2024 12:05:55 +0100 Subject: [PATCH] fix(mark2confluence): Check if mark headers are present at the beginning of the page (#18) The function `has_mark_headers` was returning `True` when a mark header was present in the file. However files with headers in the content, for example: ```markdown # How to push to confluence Create a markdown file with mark headers: ", re.IGNORECASE) +def begins_with_mark_headers(path: str, headers: List[str] = ["Space", "Parent", "Title"]) -> bool: with open(path, 'r+') as f: - data = f.read().split("\n") - for line in data: - if space_re.match(line): + first_row = f.readline() + for header in headers: + regex = re.compile(f"^", re.IGNORECASE) + if regex.match(first_row): return True return False @@ -270,7 +270,7 @@ def main()->int: default_parents = get_default_parents(cfg.inputs.DEFAULT_PARENTS) status = {} for path in files: - if path[-3:] == '.md' and has_mark_headers(path): + if path[-3:] == '.md' and begins_with_mark_headers(path): logger.info(f"Processing file {path}") inject_default_parents(path, default_parents) diff --git a/tests/resources/markdown/test_begins_with_mark_headers/with_mark_headers_in_the_content.md b/tests/resources/markdown/test_begins_with_mark_headers/with_mark_headers_in_the_content.md new file mode 100644 index 0000000..1e59103 --- /dev/null +++ b/tests/resources/markdown/test_begins_with_mark_headers/with_mark_headers_in_the_content.md @@ -0,0 +1,5 @@ +# I HAVE MARK HEADERS IN THE CONTENT + +```markdown + +``` diff --git a/tests/resources/markdown/test_has_mark_headers/with_mark_parent_header.md b/tests/resources/markdown/test_begins_with_mark_headers/with_mark_parent_header.md similarity index 100% rename from tests/resources/markdown/test_has_mark_headers/with_mark_parent_header.md rename to tests/resources/markdown/test_begins_with_mark_headers/with_mark_parent_header.md diff --git a/tests/resources/markdown/test_has_mark_headers/with_mark_space_header.md b/tests/resources/markdown/test_begins_with_mark_headers/with_mark_space_header.md similarity index 100% rename from tests/resources/markdown/test_has_mark_headers/with_mark_space_header.md rename to tests/resources/markdown/test_begins_with_mark_headers/with_mark_space_header.md diff --git a/tests/resources/markdown/test_has_mark_headers/with_mark_title_header.md b/tests/resources/markdown/test_begins_with_mark_headers/with_mark_title_header.md similarity index 100% rename from tests/resources/markdown/test_has_mark_headers/with_mark_title_header.md rename to tests/resources/markdown/test_begins_with_mark_headers/with_mark_title_header.md diff --git a/tests/resources/markdown/test_has_mark_headers/without_mark_headers.md b/tests/resources/markdown/test_begins_with_mark_headers/without_mark_headers.md similarity index 100% rename from tests/resources/markdown/test_has_mark_headers/without_mark_headers.md rename to tests/resources/markdown/test_begins_with_mark_headers/without_mark_headers.md diff --git a/tests/test_main.py b/tests/test_main.py index ddfeff4..1d1e7fb 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -32,12 +32,13 @@ def test_load_env_prefixes(): assert main.cfg.actions["FOO"] == "foo" assert main.cfg.runner["FOO"] == "foo" -def test_has_mark_headers(): - resource_directory = f"{RESOURCE_DIR}/markdown/test_has_mark_headers" - assert main.has_mark_headers(f"{resource_directory}/with_mark_space_header.md") - assert main.has_mark_headers(f"{resource_directory}/with_mark_parent_header.md") - assert main.has_mark_headers(f"{resource_directory}/with_mark_title_header.md") - assert not main.has_mark_headers(f"{resource_directory}/without_mark_headers.md") +def test_begins_with_mark_headers(): + resource_directory = f"{RESOURCE_DIR}/markdown/test_begins_with_mark_headers" + assert main.begins_with_mark_headers(f"{resource_directory}/with_mark_space_header.md") + assert main.begins_with_mark_headers(f"{resource_directory}/with_mark_parent_header.md") + assert main.begins_with_mark_headers(f"{resource_directory}/with_mark_title_header.md") + assert not main.begins_with_mark_headers(f"{resource_directory}/without_mark_headers.md") + assert not main.begins_with_mark_headers(f"{resource_directory}/with_mark_headers_in_the_content.md") def test_check_header_template(): assert main.check_header_template("Valid Jinja2 {{ var }}")