Convert ADF to markdown and back through adf-codec's lossless pair
CI / tests (pull_request) Successful in 1m47s
CI / tests (pull_request) Successful in 1m47s
This commit is contained in:
+42
-10
@@ -1,6 +1,10 @@
|
||||
"""Converts markdown to ADF (Atlassian Document Format, the rich text of Jira Cloud and Confluence) and back.
|
||||
|
||||
It runs @larvit/adf-codec, a Node package, in a throwaway container that holds node_shim/shim.mjs; the shim's header says what its exit status means.
|
||||
|
||||
adf-codec has two pairs of calls, and each function here names the one it runs:
|
||||
- The lossless pair, `*_lossless`, for a document that already exists in Jira or Confluence and is edited and saved back. It keeps each block's localId and the empty paragraphs Jira uses for spacing, so a document read and saved unedited comes back identical.
|
||||
- The portable pair, with no suffix, for new text written from scratch, and for markdown shown to a person. It drops both, so saving a document that exists changes it.
|
||||
"""
|
||||
import contextlib
|
||||
import dataclasses
|
||||
@@ -31,7 +35,7 @@ class Ran:
|
||||
err: str
|
||||
|
||||
|
||||
# Runs the shim in the given mode (`to-adf` or `to-markdown`) on a text, or fails to run it.
|
||||
# Runs the shim in the given mode (`to-adf`, `to-markdown` or either with `-lossless` after it) on a text, or fails to run it.
|
||||
Run = typing.Callable[[str, str], Ran | Failure]
|
||||
# Runs a docker command line on a text, in at most the given seconds, or fails to run it.
|
||||
Docker = typing.Callable[[list[str], str, int], Ran | Failure]
|
||||
@@ -149,9 +153,8 @@ def refusal(out: str) -> Refused | Failure:
|
||||
return Failure(f"the adf-codec shim refused with output that is not an error: {out.strip()}")
|
||||
|
||||
|
||||
def markdown_to_adf(markdown: str, run: Run = container_run) -> dict[str, typing.Any] | Failure:
|
||||
"""The ADF document for the markdown."""
|
||||
out = answer("to-adf", markdown, run)
|
||||
def to_adf(mode: str, markdown: str, run: Run) -> dict[str, typing.Any] | Failure:
|
||||
out = answer(mode, markdown, run)
|
||||
if isinstance(out, Refused):
|
||||
return out.failure
|
||||
if isinstance(out, Failure):
|
||||
@@ -163,11 +166,10 @@ def markdown_to_adf(markdown: str, run: Run = container_run) -> dict[str, typing
|
||||
return document
|
||||
|
||||
|
||||
def adfs_to_markdown(documents: list[dict[str, typing.Any]], run: Run = container_run) -> list[str] | Refused | Failure:
|
||||
"""The markdown for each ADF document, in their order, from one container run, so a caller with many documents pays one container start."""
|
||||
def to_markdowns(mode: str, documents: list[dict[str, typing.Any]], run: Run) -> list[str] | Refused | Failure:
|
||||
if not documents:
|
||||
return []
|
||||
out = answer("to-markdown", json.dumps(documents), run)
|
||||
out = answer(mode, json.dumps(documents), run)
|
||||
if isinstance(out, Refused) and not 0 <= out.index < len(documents):
|
||||
return Failure(f"the adf-codec shim refused a document at index {out.index}, past the {len(documents)} it was handed")
|
||||
if isinstance(out, Refused | Failure):
|
||||
@@ -181,9 +183,39 @@ def adfs_to_markdown(documents: list[dict[str, typing.Any]], run: Run = containe
|
||||
return texts
|
||||
|
||||
|
||||
def adf_to_markdown(document: dict[str, typing.Any], run: Run = container_run) -> str | Failure:
|
||||
"""The markdown for the ADF document."""
|
||||
texts = adfs_to_markdown([document], run)
|
||||
def one_markdown(texts: list[str] | Refused | Failure) -> str | Failure:
|
||||
if isinstance(texts, Refused):
|
||||
return texts.failure
|
||||
return texts if isinstance(texts, Failure) else texts[0]
|
||||
|
||||
|
||||
def markdown_to_adf(markdown: str, run: Run = container_run) -> dict[str, typing.Any] | Failure:
|
||||
"""The ADF document for new markdown, by the portable pair: for text written from scratch. It can't carry a document that exists back unchanged; use markdown_to_adf_lossless."""
|
||||
return to_adf("to-adf", markdown, run)
|
||||
|
||||
|
||||
def markdown_to_adf_lossless(markdown: str, run: Run = container_run) -> dict[str, typing.Any] | Failure:
|
||||
"""The ADF document for markdown that adf_to_markdown_lossless or adfs_to_markdown_lossless wrote, by the lossless pair: for saving an existing Jira or Confluence document back. Unedited, it gives back the document that was read."""
|
||||
return to_adf("to-adf-lossless", markdown, run)
|
||||
|
||||
|
||||
def adfs_to_markdown(documents: list[dict[str, typing.Any]], run: Run = container_run) -> list[str] | Refused | Failure:
|
||||
"""The markdown for each ADF document, in their order, by the portable pair, from one container run, so a caller with many documents pays one container start.
|
||||
|
||||
For markdown shown to a person; the text can't be saved back without changing the document, so use adfs_to_markdown_lossless to edit one."""
|
||||
return to_markdowns("to-markdown", documents, run)
|
||||
|
||||
|
||||
def adfs_to_markdown_lossless(documents: list[dict[str, typing.Any]], run: Run = container_run) -> list[str] | Refused | Failure:
|
||||
"""The markdown for each ADF document, in their order, by the lossless pair, from one container run: for documents that exist in Jira or Confluence and are edited and saved back with markdown_to_adf_lossless."""
|
||||
return to_markdowns("to-markdown-lossless", documents, run)
|
||||
|
||||
|
||||
def adf_to_markdown(document: dict[str, typing.Any], run: Run = container_run) -> str | Failure:
|
||||
"""The markdown for the ADF document, by the portable pair: for showing it to a person, not for saving it back."""
|
||||
return one_markdown(adfs_to_markdown([document], run))
|
||||
|
||||
|
||||
def adf_to_markdown_lossless(document: dict[str, typing.Any], run: Run = container_run) -> str | Failure:
|
||||
"""The markdown for the ADF document, by the lossless pair: for editing it and saving it back with markdown_to_adf_lossless."""
|
||||
return one_markdown(adfs_to_markdown_lossless([document], run))
|
||||
|
||||
Reference in New Issue
Block a user