Skip to content

Commit

Permalink
Adding functionality to escape items in the markdown which look like …
Browse files Browse the repository at this point in the history
…XHTML macros.

So ${SOME_VARIABLE} becomes $\{SOME_VARIABLE\}

Without this change the Confluence page creation can fail, in our case because we had
fenced code blocks containing fragments of bash scripts.
  • Loading branch information
clivedavies-cpi committed Nov 30, 2023
1 parent 5c3a11d commit 493335e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions wiki_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# TODO handle links like [link], which happen when the link name is the same as
# the link itself
JIRA_LINK_PATTERN = re.compile(r'\[[^|\n]+\|([^|\n]+)\]')
JIRA_MACRO_PATTERN = re.compile(r'\${[a-zA-Z_-]*}')


def get_files_to_sync(changed_files: str) -> List[str]:
Expand Down Expand Up @@ -122,6 +123,8 @@ def get_formatted_file_content(wiki_client: atlassian.Confluence,
"""
# keys are relative links; values are what they should be replaced with
links_to_replace: Dict[str, str] = {}
# keys are macros in XHTML; values are what they should be replaced with
macros_to_replace: Dict[str, str] = {}

absolute_file_path = os.path.join(repo_root, file_path)
formated_file_contents = pypandoc.convert_file(absolute_file_path, 'jira')
Expand Down Expand Up @@ -156,6 +159,16 @@ def get_formatted_file_content(wiki_client: atlassian.Confluence,
formated_file_contents = formated_file_contents.replace(
f'|{relative_link}]', f'|{new_link}]')

# find macros and escape the curly braces
for macro in re.findall(JIRA_MACRO_PATTERN, formated_file_contents):
macros_to_replace[macro] = macro.replace(
'{',
r'\{').replace('}', r'\}')

for macro, escaped_macro in macros_to_replace.items():
formated_file_contents = formated_file_contents.replace(
f'{macro}', f'{escaped_macro}')

return formated_file_contents


Expand Down

0 comments on commit 493335e

Please sign in to comment.