Skip to content

Commit

Permalink
Ensure local output succeeds with missing page ID when root page ID i…
Browse files Browse the repository at this point in the history
…s set
  • Loading branch information
hunyadi committed Sep 20, 2024
1 parent a74c3d4 commit 52af6dc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions md2conf/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import requests

from . import __version__
from .api import ConfluenceAPI
from .application import Application
from .converter import ConfluenceDocumentOptions
Expand All @@ -30,6 +31,7 @@ class Arguments(argparse.Namespace):
def main() -> None:
parser = argparse.ArgumentParser()
parser.prog = os.path.basename(os.path.dirname(__file__))
parser.add_argument("--version", action="version", version=__version__)
parser.add_argument(
"mdpath", help="Path to Markdown file or directory to convert and publish."
)
Expand Down
11 changes: 11 additions & 0 deletions md2conf/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,10 @@ class ConfluenceQualifiedID:
page_id: str
space_key: Optional[str] = None

def __init__(self, page_id: str, space_key: Optional[str] = None):
self.page_id = page_id
self.space_key = space_key


def extract_qualified_id(string: str) -> Tuple[Optional[ConfluenceQualifiedID], str]:
"Extracts the Confluence page ID and space key from a Markdown document."
Expand Down Expand Up @@ -798,6 +802,13 @@ def __init__(

# extract Confluence page ID
qualified_id, text = extract_qualified_id(text)
if qualified_id is None:
# look up Confluence page ID in metadata
metadata = page_metadata.get(path)
if metadata is not None:
qualified_id = ConfluenceQualifiedID(
metadata.page_id, metadata.space_key
)
if qualified_id is None:
raise ValueError("missing Confluence page ID")
self.id = qualified_id
Expand Down
12 changes: 10 additions & 2 deletions md2conf/processor.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hashlib
import logging
import os
from pathlib import Path
Expand All @@ -7,6 +8,7 @@
ConfluenceDocument,
ConfluenceDocumentOptions,
ConfluencePageMetadata,
ConfluenceQualifiedID,
extract_qualified_id,
)
from .properties import ConfluenceProperties
Expand Down Expand Up @@ -92,9 +94,15 @@ def _get_page(self, absolute_path: Path) -> ConfluencePageMetadata:
with open(absolute_path, "r", encoding="utf-8") as f:
document = f.read()

qualified_id, _ = extract_qualified_id(document)
qualified_id, document = extract_qualified_id(document)
if qualified_id is None:
raise ValueError("required: page ID for local output")
if self.options.root_page_id is not None:
hash = hashlib.md5(document.encode("utf-8"))
digest = "".join(f"{c:x}" for c in hash.digest())
LOGGER.info(f"Identifier '{digest}' assigned to page: {absolute_path}")
qualified_id = ConfluenceQualifiedID(digest)
else:
raise ValueError("required: page ID for local output")

return ConfluencePageMetadata(
domain=self.properties.domain,
Expand Down
2 changes: 1 addition & 1 deletion sample/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/*.csf
*.csf
1 change: 1 addition & 0 deletions sample/parent/child.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A child page without an explicitly assigned Confluence page ID or space key.

0 comments on commit 52af6dc

Please sign in to comment.