Skip to content

Commit

Permalink
fix(mark2confluence): Check if mark headers are present at the beginn…
Browse files Browse the repository at this point in the history
…ing 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:
     <!-- Space: FOO ->
     # Page Title
```
Were also processed, causing the `mark` binary to fail because it couldn't find headers.

This PR fixes it by checking the presence of mark headers in the first line of the file.
  • Loading branch information
gmarraff committed Jan 31, 2024
1 parent b560d01 commit 45d0dd1
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 12 deletions.
12 changes: 6 additions & 6 deletions mark2confluence/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ def publish(path: str)-> tuple:
return True, None


def has_mark_headers(path: str) -> bool:
space_re = re.compile("<!--.?(space|parent|title):.*-->", 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"^<!--.?{header}:.*-->", re.IGNORECASE)
if regex.match(first_row):
return True
return False

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# I HAVE MARK HEADERS IN THE CONTENT

```markdown
<!-- Space: Content -->
```
13 changes: 7 additions & 6 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}")
Expand Down

0 comments on commit 45d0dd1

Please sign in to comment.