Skip to content

Commit

Permalink
Fix miscellaneous issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hunyadi committed Nov 26, 2024
1 parent 0c45da0 commit 19325a1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion integration_tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def test_upload_attachment(self) -> None:
with ConfluenceAPI() as api:
api.upload_attachment(
TEST_PAGE_ID,
self.sample_dir / "figure" / "interoperability.png",
"figure_interoperability.png",
attachment_path=self.sample_dir / "figure" / "interoperability.png",
comment="A sample figure",
force=True,
)
Expand Down
12 changes: 11 additions & 1 deletion md2conf/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,17 @@ def update_page(
new_content: str,
*,
space_key: Optional[str] = None,
title: Optional[str] = None,
) -> None:
"""
Update a page via the Confluence API.
:param page_id: The Confluence page ID.
:param new_content: Confluence Storage Format XHTML.
:param space_key: The Confluence space key (unless the default space is to be used).
:param title: New title to assign to the page. Needs to be unique within a space.
"""

page = self.get_page(page_id, space_key=space_key)

try:
Expand All @@ -435,7 +445,7 @@ def update_page(
data = {
"id": page_id,
"type": "page",
"title": page.title, # title needs to be unique within a space so the original title is maintained
"title": title or page.title,
"space": {"key": space_key or self.space_key},
"body": {"storage": {"value": new_content, "representation": "storage"}},
"version": {"minorEdit": True, "number": page.version + 1},
Expand Down
25 changes: 11 additions & 14 deletions md2conf/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@
from pathlib import Path
from typing import Dict, List, Optional

import yaml

from .api import ConfluencePage, ConfluenceSession
from .converter import (
ConfluenceDocument,
ConfluenceDocumentOptions,
ConfluencePageMetadata,
ConfluenceQualifiedID,
attachment_name,
extract_frontmatter,
extract_frontmatter_title,
extract_qualified_id,
read_qualified_id,
)
Expand Down Expand Up @@ -174,7 +172,7 @@ def _get_or_create_page(
document = f.read()

qualified_id, document = extract_qualified_id(document)
frontmatter, document = extract_frontmatter(document)
frontmatter_title, _ = extract_frontmatter_title(document)

if qualified_id is not None:
confluence_page = self.api.get_page(
Expand All @@ -187,15 +185,8 @@ def _get_or_create_page(
)

# assign title from frontmatter if present
if title is None and frontmatter is not None:
properties = yaml.safe_load(frontmatter)
if isinstance(properties, dict):
property_title = properties.get("title")
if isinstance(property_title, str):
title = property_title

confluence_page = self._create_page(
absolute_path, document, title, parent_id
absolute_path, document, title or frontmatter_title, parent_id
)

return ConfluencePageMetadata(
Expand Down Expand Up @@ -230,7 +221,13 @@ def _create_page(
)
return confluence_page

def _update_document(self, document: ConfluenceDocument, base_path: Path) -> None:
def _update_document(
self,
document: ConfluenceDocument,
base_path: Path,
*,
title: Optional[str] = None,
) -> None:
"Saves a new version of a Confluence document."

for image in document.images:
Expand All @@ -249,7 +246,7 @@ def _update_document(self, document: ConfluenceDocument, base_path: Path) -> Non

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

def _update_markdown(
self,
Expand Down
15 changes: 15 additions & 0 deletions md2conf/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import lxml.etree as ET
import markdown
import yaml
from lxml.builder import ElementMaker

from . import mermaid
Expand Down Expand Up @@ -907,6 +908,20 @@ def extract_frontmatter(text: str) -> Tuple[Optional[str], str]:
return extract_value(r"(?ms)\A---$(.+?)^---$", text)


def extract_frontmatter_title(text: str) -> Tuple[Optional[str], str]:
frontmatter, text = extract_frontmatter(text)

title: Optional[str] = None
if frontmatter is not None:
properties = yaml.safe_load(frontmatter)
if isinstance(properties, dict):
property_title = properties.get("title")
if isinstance(property_title, str):
title = property_title

return title, text


def read_qualified_id(absolute_path: Path) -> Optional[ConfluenceQualifiedID]:
"Reads the Confluence page ID and space key from a Markdown document."

Expand Down

0 comments on commit 19325a1

Please sign in to comment.