Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(mark2confluence): Check if mark headers are present at the beginning of the page #18

Merged
merged 2 commits into from
Jan 31, 2024
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
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
Loading