Skip to content

Commit

Permalink
Fix an issue with index page not assuming parent role
Browse files Browse the repository at this point in the history
  • Loading branch information
hunyadi committed Oct 25, 2024
1 parent b90e19e commit 2a58565
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 22 deletions.
2 changes: 1 addition & 1 deletion integration_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ def test_synchronize_create(self) -> None:
with open(child, "r", encoding="utf-8") as f:
self.assertEqual(
f.read(),
"<!-- confluence-page-id: 86269493445 -->\n"
"<!-- confluence-page-id: 86971744339 -->\n"
f"<!-- confluence-space-key: {TEST_SPACE} -->\n"
"This is a document without an explicitly linked Confluence document.\n",
)
Expand Down
28 changes: 18 additions & 10 deletions md2conf/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def synchronize_page(self, page_path: Path) -> None:
def synchronize_directory(self, local_dir: Path) -> None:
"Synchronizes a directory of Markdown pages with Confluence."

LOGGER.info(f"Synchronizing directory: {local_dir}")
LOGGER.info("Synchronizing directory: %s", local_dir)
local_dir = local_dir.resolve(True)

# Step 1: build index of all page metadata
Expand All @@ -72,7 +72,7 @@ def synchronize_directory(self, local_dir: Path) -> None:
else None
)
self._index_directory(local_dir, root_id, page_metadata)
LOGGER.info(f"indexed {len(page_metadata)} page(s)")
LOGGER.info("Indexed %d page(s)", len(page_metadata))

# Step 2: convert each page
for page_path in page_metadata.keys():
Expand All @@ -85,7 +85,7 @@ def _synchronize_page(
) -> None:
base_path = page_path.parent

LOGGER.info(f"Synchronizing page: {page_path}")
LOGGER.info("Synchronizing page: %s", page_path)
document = ConfluenceDocument(page_path, self.options, page_metadata)

if document.id.space_key:
Expand All @@ -102,7 +102,7 @@ def _index_directory(
) -> None:
"Indexes Markdown files in a directory recursively."

LOGGER.info(f"Indexing directory: {local_dir}")
LOGGER.info("Indexing directory: %s", local_dir)

matcher = Matcher(MatcherOptions(source=".mdignore", extension="md"), local_dir)

Expand All @@ -118,18 +118,26 @@ def _index_directory(
directories.append(Path(local_dir) / entry.name)

# make page act as parent node in Confluence
parent_id: Optional[ConfluenceQualifiedID] = None
parent_doc: Optional[Path] = None
if (Path(local_dir) / "index.md") in files:
parent_id = read_qualified_id(Path(local_dir) / "index.md")
parent_doc = Path(local_dir) / "index.md"
elif (Path(local_dir) / "README.md") in files:
parent_id = read_qualified_id(Path(local_dir) / "README.md")
parent_doc = Path(local_dir) / "README.md"

if parent_id is None:
if parent_doc is not None:
files.remove(parent_doc)

metadata = self._get_or_create_page(parent_doc, root_id)
LOGGER.debug("Indexed parent %s with metadata: %s", parent_doc, metadata)
page_metadata[parent_doc] = metadata

parent_id = read_qualified_id(parent_doc) or root_id
else:
parent_id = root_id

for doc in files:
metadata = self._get_or_create_page(doc, parent_id)
LOGGER.debug(f"indexed {doc} with metadata: {metadata}")
LOGGER.debug("Indexed %s with metadata: %s", doc, metadata)
page_metadata[doc] = metadata

for directory in directories:
Expand Down Expand Up @@ -226,7 +234,7 @@ def _update_document(self, document: ConfluenceDocument, base_path: Path) -> Non
)

content = document.xhtml()
LOGGER.debug(f"generated Confluence Storage Format document:\n{content}")
LOGGER.debug("Generated Confluence Storage Format document:\n%s", content)
self.api.update_page(document.id.page_id, content)

def _update_markdown(
Expand Down
8 changes: 4 additions & 4 deletions md2conf/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ def _transform_link(self, anchor: ET._Element) -> Optional[ET._Element]:
if is_absolute_url(url):
return None

LOGGER.debug(f"found link {url} relative to {self.path}")
LOGGER.debug("Found link %s relative to %s", url, self.path)
relative_url: ParseResult = urlparse(url)

if (
Expand All @@ -361,7 +361,7 @@ def _transform_link(self, anchor: ET._Element) -> Optional[ET._Element]:
and not relative_url.params
and not relative_url.query
):
LOGGER.debug(f"found local URL: {url}")
LOGGER.debug("Found local URL: %s", url)
if self.options.heading_anchors:
# <ac:link ac:anchor="anchor"><ac:link-body>...</ac:link-body></ac:link>
target = relative_url.fragment.lstrip("#")
Expand Down Expand Up @@ -406,7 +406,7 @@ def _transform_link(self, anchor: ET._Element) -> Optional[ET._Element]:
raise DocumentError(msg)

LOGGER.debug(
f"found link to page {relative_path} with metadata: {link_metadata}"
"found link to page %s with metadata: %s", relative_path, link_metadata
)
self.links.append(url)

Expand All @@ -425,7 +425,7 @@ def _transform_link(self, anchor: ET._Element) -> Optional[ET._Element]:
)
transformed_url = urlunparse(components)

LOGGER.debug(f"transformed relative URL: {url} to URL: {transformed_url}")
LOGGER.debug("Transformed relative URL: %s to URL: %s", url, transformed_url)
anchor.attrib["href"] = transformed_url
return None

Expand Down
2 changes: 1 addition & 1 deletion md2conf/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def render(source: str, output_format: Literal["png", "svg"] = "png") -> bytes:
root = os.path.dirname(__file__)
if is_docker():
cmd.extend(["-p", os.path.join(root, "puppeteer-config.json")])
LOGGER.debug(f"Executing: {' '.join(cmd)}")
LOGGER.debug("Executing: %s", " ".join(cmd))
try:
proc = subprocess.Popen(
cmd,
Expand Down
10 changes: 5 additions & 5 deletions md2conf/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ def process(self, path: Path) -> None:
def process_directory(self, local_dir: Path) -> None:
"Recursively scans a directory hierarchy for Markdown files."

LOGGER.info(f"Synchronizing directory: {local_dir}")
LOGGER.info("Synchronizing directory: %s", local_dir)
local_dir = local_dir.resolve(True)

# Step 1: build index of all page metadata
page_metadata: Dict[Path, ConfluencePageMetadata] = {}
self._index_directory(local_dir, page_metadata)
LOGGER.info(f"indexed {len(page_metadata)} page(s)")
LOGGER.info("Indexed %d page(s)", len(page_metadata))

# Step 2: convert each page
for page_path in page_metadata.keys():
Expand All @@ -79,7 +79,7 @@ def _index_directory(
) -> None:
"Indexes Markdown files in a directory recursively."

LOGGER.info(f"Indexing directory: {local_dir}")
LOGGER.info("Indexing directory: %s", local_dir)

matcher = Matcher(MatcherOptions(source=".mdignore", extension="md"), local_dir)

Expand All @@ -96,7 +96,7 @@ def _index_directory(

for doc in files:
metadata = self._get_page(doc)
LOGGER.debug(f"indexed {doc} with metadata: {metadata}")
LOGGER.debug("Indexed %s with metadata: %s", doc, metadata)
page_metadata[doc] = metadata

for directory in directories:
Expand All @@ -113,7 +113,7 @@ def _get_page(self, absolute_path: Path) -> ConfluencePageMetadata:
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}")
LOGGER.info("Identifier %s assigned to page: %s", digest, absolute_path)
qualified_id = ConfluenceQualifiedID(digest)
else:
raise ValueError("required: page ID for local output")
Expand Down
3 changes: 2 additions & 1 deletion sample/parent/child.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!-- confluence-page-id: 86269493445 -->
<!-- confluence-page-id: 86971744339 -->
<!-- confluence-space-key: DAP -->

If you are a user who wants to publish pages to Confluence, you should install the package [markdown-to-confluence](https://pypi.org/project/markdown-to-confluence/) from PyPI. If you are a developer who wants to contribute, you should clone the repository [md2conf](https://github.com/hunyadi/md2conf) from GitHub.

Expand Down
4 changes: 4 additions & 0 deletions sample/parent/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<!-- confluence-page-id: 86971318351 -->
<!-- confluence-space-key: DAP -->

This page is a parent page nested under the root page. It has a single child page. In the Confluence navigation bar, root page, parent page and child page should be nested one below the other.

0 comments on commit 2a58565

Please sign in to comment.