Skip to content

Commit

Permalink
Set title from front matter if present
Browse files Browse the repository at this point in the history
  • Loading branch information
hunyadi committed Oct 10, 2024
1 parent 0fb4509 commit 3ebdd29
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
13 changes: 13 additions & 0 deletions md2conf/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
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_qualified_id,
read_qualified_id,
)
Expand Down Expand Up @@ -137,6 +140,8 @@ def _get_or_create_page(
document = f.read()

qualified_id, document = extract_qualified_id(document)
frontmatter, document = extract_frontmatter(document)

if qualified_id is not None:
confluence_page = self.api.get_page(
qualified_id.page_id, space_key=qualified_id.space_key
Expand All @@ -147,6 +152,14 @@ def _get_or_create_page(
f"expected: parent page ID for Markdown file with no linked Confluence page: {absolute_path}"
)

# 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
)
Expand Down
26 changes: 15 additions & 11 deletions md2conf/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,16 +844,16 @@ class DocumentError(RuntimeError):
pass


def extract_value(pattern: str, string: str) -> Tuple[Optional[str], str]:
def extract_value(pattern: str, text: str) -> Tuple[Optional[str], str]:
values: List[str] = []

def _repl_func(matchobj: re.Match) -> str:
values.append(matchobj.group(1))
return ""

string = re.sub(pattern, _repl_func, string, 1, re.ASCII)
text = re.sub(pattern, _repl_func, text, 1, re.ASCII)
value = values[0] if values else None
return value, string
return value, text


@dataclass
Expand All @@ -866,20 +866,24 @@ def __init__(self, page_id: str, space_key: Optional[str] = None):
self.space_key = space_key


def extract_qualified_id(string: str) -> Tuple[Optional[ConfluenceQualifiedID], str]:
def extract_qualified_id(text: str) -> Tuple[Optional[ConfluenceQualifiedID], str]:
"Extracts the Confluence page ID and space key from a Markdown document."

page_id, string = extract_value(r"<!--\s+confluence-page-id:\s*(\d+)\s+-->", string)
page_id, text = extract_value(r"<!--\s+confluence-page-id:\s*(\d+)\s+-->", text)

if page_id is None:
return None, string
return None, text

# extract Confluence space key
space_key, string = extract_value(
r"<!--\s+confluence-space-key:\s*(\S+)\s+-->", string
)
space_key, text = extract_value(r"<!--\s+confluence-space-key:\s*(\S+)\s+-->", text)

return ConfluenceQualifiedID(page_id, space_key), text


def extract_frontmatter(text: str) -> Tuple[Optional[str], str]:
"Extracts the front matter from a Markdown document."

return ConfluenceQualifiedID(page_id, space_key), string
return extract_value(r"(?ms)\A---$(.+?)^---$", text)


def read_qualified_id(absolute_path: Path) -> Optional[ConfluenceQualifiedID]:
Expand Down Expand Up @@ -956,7 +960,7 @@ def __init__(
)

# extract frontmatter
frontmatter, text = extract_value(r"(?ms)\A---$(.+?)^---$", text)
frontmatter, text = extract_frontmatter(text)

# convert to HTML
html = markdown_to_html(text)
Expand Down
5 changes: 2 additions & 3 deletions md2conf/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ def render(source: str, output_format: Literal["png", "svg"] = "png") -> bytes:
"--outputFormat",
output_format,
]
root = os.path.dirname(__file__)
if is_docker():
cmd.extend(
["-p", os.path.join(os.path.dirname(__file__), "puppeteer-config.json")]
)
cmd.extend(["-p", os.path.join(root, "puppeteer-config.json")])
LOGGER.debug(f"Executing: {' '.join(cmd)}")
try:
proc = subprocess.Popen(
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ types-lxml
markdown
types-markdown
pymdown-extensions
pyyaml
requests
types-requests

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ install_requires =
markdown >= 3.6
types-markdown >= 3.6
pymdown-extensions >= 10.9
pyyaml >= 6.0
requests >= 2.32
types-requests >= 2.32

Expand Down

0 comments on commit 3ebdd29

Please sign in to comment.