From 3ebdd2960716b0af7d3daf40d880b3f786e3afe2 Mon Sep 17 00:00:00 2001 From: Levente Hunyadi Date: Fri, 11 Oct 2024 01:16:10 +0200 Subject: [PATCH] Set title from front matter if present --- md2conf/application.py | 13 +++++++++++++ md2conf/converter.py | 26 +++++++++++++++----------- md2conf/mermaid.py | 5 ++--- requirements.txt | 1 + setup.cfg | 1 + 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/md2conf/application.py b/md2conf/application.py index c91ed4e..dbf04fd 100644 --- a/md2conf/application.py +++ b/md2conf/application.py @@ -3,6 +3,8 @@ from pathlib import Path from typing import Dict, List, Optional +import yaml + from .api import ConfluencePage, ConfluenceSession from .converter import ( ConfluenceDocument, @@ -10,6 +12,7 @@ ConfluencePageMetadata, ConfluenceQualifiedID, attachment_name, + extract_frontmatter, extract_qualified_id, read_qualified_id, ) @@ -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 @@ -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 ) diff --git a/md2conf/converter.py b/md2conf/converter.py index 5946a6a..f02675e 100644 --- a/md2conf/converter.py +++ b/md2conf/converter.py @@ -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 @@ -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"", string) + page_id, text = extract_value(r"", text) if page_id is None: - return None, string + return None, text # extract Confluence space key - space_key, string = extract_value( - r"", string - ) + space_key, text = extract_value(r"", 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]: @@ -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) diff --git a/md2conf/mermaid.py b/md2conf/mermaid.py index 63c447f..fc83da0 100644 --- a/md2conf/mermaid.py +++ b/md2conf/mermaid.py @@ -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( diff --git a/requirements.txt b/requirements.txt index fee4195..910a539 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,7 @@ types-lxml markdown types-markdown pymdown-extensions +pyyaml requests types-requests diff --git a/setup.cfg b/setup.cfg index da2f566..65da147 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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