marklas 0.7.0__tar.gz → 0.7.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: marklas
3
- Version: 0.7.0
3
+ Version: 0.7.2
4
4
  Summary: Bidirectional converter between GitHub Flavored Markdown and Atlassian Document Format
5
5
  Keywords: markdown,adf,atlassian,jira,confluence,converter,atlassian-document-format
6
6
  Author: byExist
@@ -94,14 +94,15 @@ restored_adf = to_adf(markdown) # push back — structure preserved
94
94
 
95
95
  ## Advanced Usage
96
96
 
97
- For pipelines that need to modify the AST between parsing and rendering — such as uploading local images as Confluence attachments — use `Transformer`:
97
+ For pipelines that need to modify the AST between parsing and rendering, use `Transformer`:
98
98
 
99
99
  ```python
100
100
  from marklas import Transformer, parse_md, render_adf
101
- from marklas.ast import Media
101
+ from marklas.ast import CodeBlock, Expand, Extension, Media, Node
102
102
 
103
103
  t = Transformer()
104
104
 
105
+ # Replace: return a Node to substitute the original
105
106
  @t.register(Media)
106
107
  def _(node: Media) -> Media | None:
107
108
  if node.type == "external":
@@ -109,11 +110,35 @@ def _(node: Media) -> Media | None:
109
110
  return Media(type="file", id=uploaded.media_id, collection=uploaded.collection)
110
111
  return None
111
112
 
113
+ # Splice: return a list[Node] to expand one node into many
114
+ @t.register(CodeBlock)
115
+ def _(node: CodeBlock) -> list[Node] | None:
116
+ if node.language == "mermaid":
117
+ return [
118
+ Expand(title="mermaid source", content=[node]),
119
+ Extension(
120
+ extension_key="mermaid-macro",
121
+ extension_type="com.example.mermaid",
122
+ parameters={"code": "".join(c.text for c in node.content)},
123
+ ),
124
+ ]
125
+ return None
126
+
112
127
  doc = parse_md(markdown)
113
128
  new_doc = t(doc)
114
129
  adf = render_adf(new_doc)
115
130
  ```
116
131
 
132
+ A handler returns one of three values:
133
+
134
+ | Return | Effect |
135
+ | --- | --- |
136
+ | `None` | Skip — pass to the next handler, or leave unchanged |
137
+ | `Node` | Replace the original node |
138
+ | `list[Node]` | Splice multiple nodes in place of the original |
139
+
140
+ Multiple handlers can be registered for the same type; they run in registration order and the first non-`None` result wins. The tree is traversed bottom-up, and nodes returned by a handler are not revisited.
141
+
117
142
  | Function | Description |
118
143
  | --- | --- |
119
144
  | `parse_md(md)` | Markdown → AST |
@@ -67,14 +67,15 @@ restored_adf = to_adf(markdown) # push back — structure preserved
67
67
 
68
68
  ## Advanced Usage
69
69
 
70
- For pipelines that need to modify the AST between parsing and rendering — such as uploading local images as Confluence attachments — use `Transformer`:
70
+ For pipelines that need to modify the AST between parsing and rendering, use `Transformer`:
71
71
 
72
72
  ```python
73
73
  from marklas import Transformer, parse_md, render_adf
74
- from marklas.ast import Media
74
+ from marklas.ast import CodeBlock, Expand, Extension, Media, Node
75
75
 
76
76
  t = Transformer()
77
77
 
78
+ # Replace: return a Node to substitute the original
78
79
  @t.register(Media)
79
80
  def _(node: Media) -> Media | None:
80
81
  if node.type == "external":
@@ -82,11 +83,35 @@ def _(node: Media) -> Media | None:
82
83
  return Media(type="file", id=uploaded.media_id, collection=uploaded.collection)
83
84
  return None
84
85
 
86
+ # Splice: return a list[Node] to expand one node into many
87
+ @t.register(CodeBlock)
88
+ def _(node: CodeBlock) -> list[Node] | None:
89
+ if node.language == "mermaid":
90
+ return [
91
+ Expand(title="mermaid source", content=[node]),
92
+ Extension(
93
+ extension_key="mermaid-macro",
94
+ extension_type="com.example.mermaid",
95
+ parameters={"code": "".join(c.text for c in node.content)},
96
+ ),
97
+ ]
98
+ return None
99
+
85
100
  doc = parse_md(markdown)
86
101
  new_doc = t(doc)
87
102
  adf = render_adf(new_doc)
88
103
  ```
89
104
 
105
+ A handler returns one of three values:
106
+
107
+ | Return | Effect |
108
+ | --- | --- |
109
+ | `None` | Skip — pass to the next handler, or leave unchanged |
110
+ | `Node` | Replace the original node |
111
+ | `list[Node]` | Splice multiple nodes in place of the original |
112
+
113
+ Multiple handlers can be registered for the same type; they run in registration order and the first non-`None` result wins. The tree is traversed bottom-up, and nodes returned by a handler are not revisited.
114
+
90
115
  | Function | Description |
91
116
  | --- | --- |
92
117
  | `parse_md(md)` | Markdown → AST |
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "marklas"
3
- version = "0.7.0"
3
+ version = "0.7.2"
4
4
  description = "Bidirectional converter between GitHub Flavored Markdown and Atlassian Document Format"
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -44,8 +44,18 @@ def _omit_none(d: dict[str, Any]) -> dict[str, Any]:
44
44
  return {k: v for k, v in d.items() if v is not None}
45
45
 
46
46
 
47
+ _CODE_COMPATIBLE_MARKS = (ast.CodeMark, ast.LinkMark, ast.AnnotationMark)
48
+
49
+
50
+ def _sanitize_marks(marks: Sequence[ast.Mark]) -> Sequence[ast.Mark]:
51
+ if any(isinstance(m, ast.CodeMark) for m in marks):
52
+ return [m for m in marks if isinstance(m, _CODE_COMPATIBLE_MARKS)]
53
+ return marks
54
+
55
+
47
56
  def _with_marks(result: dict[str, Any], marks: Sequence[ast.Mark]) -> dict[str, Any]:
48
57
  """Attach marks to a node dict if any exist."""
58
+ marks = _sanitize_marks(marks)
49
59
  if marks:
50
60
  result["marks"] = _render_marks(marks)
51
61
  return result
@@ -835,92 +835,91 @@ def _find_inline_close(
835
835
 
836
836
 
837
837
  def _normalize_inlines(tokens: list[dict[str, Any]]) -> list[dict[str, Any]]:
838
- """Normalize inline token stream.
838
+ """Recursively pair inline_html tokens at every nesting level.
839
839
 
840
840
  - inline_html pairs → {"_kind": "inline_html", "tag", "adf_type", "attrs", "inner"}
841
- - void inline_html → same with "inner": []
842
841
  - adf-less HTML → removed
842
+ - tokens with children (strong, emphasis, link, etc.) → children normalized
843
843
  - other tokens → pass through
844
+
845
+ After this pass, no raw inline_html open/close tokens remain anywhere in the
846
+ tree; `_flatten_inlines` can assume the tree is fully normalized.
844
847
  """
845
848
  result: list[dict[str, Any]] = []
846
849
  i = 0
847
850
  while i < len(tokens):
848
851
  token = tokens[i]
849
852
 
850
- if token.get("type") != "inline_html":
851
- result.append(token)
852
- i += 1
853
- continue
854
-
855
- raw = token.get("raw", "")
856
- tag, attrs, closing = _parse_tag(raw)
853
+ if token.get("type") == "inline_html":
854
+ raw = token.get("raw", "")
855
+ tag, attrs, closing = _parse_tag(raw)
857
856
 
858
- if closing or not _has_adf(attrs):
859
- i += 1
860
- continue
857
+ if closing or not _has_adf(attrs):
858
+ i += 1
859
+ continue
861
860
 
862
- adf_type = attrs.get("adf", "")
861
+ adf_type = attrs.get("adf", "")
862
+ close_idx = _find_inline_close(tokens, i + 1, tag)
863
+ if close_idx is not None:
864
+ result.append(
865
+ {
866
+ "_kind": "inline_html",
867
+ "tag": tag,
868
+ "adf_type": adf_type,
869
+ "attrs": attrs,
870
+ "inner": _normalize_inlines(tokens[i + 1 : close_idx]),
871
+ }
872
+ )
873
+ i = close_idx + 1
874
+ continue
863
875
 
864
- close_idx = _find_inline_close(tokens, i + 1, tag)
865
- if close_idx is not None:
866
- result.append(
867
- {
868
- "_kind": "inline_html",
869
- "tag": tag,
870
- "adf_type": adf_type,
871
- "attrs": attrs,
872
- "inner": tokens[i + 1 : close_idx],
873
- }
874
- )
875
- i = close_idx + 1
876
+ i += 1
876
877
  continue
877
878
 
879
+ if "children" in token:
880
+ result.append({**token, "children": _normalize_inlines(token["children"])})
881
+ else:
882
+ result.append(token)
878
883
  i += 1
879
884
 
880
885
  return result
881
886
 
882
887
 
883
888
  def _parse_inlines(tokens: list[dict[str, Any]]) -> list[ast.Inline]:
889
+ return _flatten_inlines(_normalize_inlines(tokens), [])
890
+
891
+
892
+ def _flatten_inlines(
893
+ tokens: list[dict[str, Any]], parent_marks: list[ast.Mark]
894
+ ) -> list[ast.Inline]:
895
+ """Walk normalized tokens, accumulating marks. Assumes input is normalized."""
884
896
  result: list[ast.Inline] = []
885
- for token in _normalize_inlines(tokens):
897
+ for token in tokens:
886
898
  if token.get("_kind") == "inline_html":
887
- result.append(
888
- _parse_inline_html(
889
- token["tag"], token["adf_type"], token["attrs"], token["inner"]
890
- )
891
- )
899
+ result.extend(_parse_inline_html(token, parent_marks))
892
900
  else:
893
- result.extend(_flatten_marks(token, []))
901
+ result.extend(_flatten_token(token, parent_marks))
894
902
  return result
895
903
 
896
904
 
897
905
  # ── Mark flattening ───────────────────────────────────────────────────────────
898
906
 
899
907
 
900
- def _flatten_children(
901
- children: list[dict[str, Any]], marks: list[ast.Mark]
902
- ) -> list[ast.Inline]:
903
- result: list[ast.Inline] = []
904
- for child in children:
905
- result.extend(_flatten_marks(child, marks))
906
- return result
907
-
908
-
909
- def _flatten_marks(
908
+ def _flatten_token(
910
909
  token: dict[str, Any], parent_marks: list[ast.Mark]
911
910
  ) -> list[ast.Inline]:
912
911
  t = token.get("type", "")
913
912
  match t:
914
913
  case "strong":
915
- return _flatten_children(
914
+ return _flatten_inlines(
916
915
  token.get("children", []), [*parent_marks, ast.StrongMark()]
917
916
  )
918
917
  case "emphasis":
919
- return _flatten_children(
918
+ return _flatten_inlines(
920
919
  token.get("children", []), [*parent_marks, ast.EmMark()]
921
920
  )
922
921
  case "strikethrough":
923
- return _flatten_children(
922
+ return _flatten_inlines(
924
923
  token.get("children", []), [*parent_marks, ast.StrikeMark()]
925
924
  )
926
925
  case "codespan":
@@ -934,20 +933,22 @@ def _flatten_marks(
934
933
  *parent_marks,
935
934
  ast.LinkMark(href=href, title=title),
936
935
  ]
937
- return _flatten_children(token.get("children", []), link_marks)
936
+ return _flatten_inlines(token.get("children", []), link_marks)
938
937
  case "text":
939
938
  text = token.get("raw", "")
940
939
  if parent_marks:
941
- return [ast.Text(text=text, marks=parent_marks)]
940
+ return [ast.Text(text=text, marks=list(parent_marks))]
942
941
  return [ast.Text(text=text)]
943
942
  case "softbreak":
943
+ if parent_marks:
944
+ return [ast.Text(text=" ", marks=list(parent_marks))]
944
945
  return [ast.Text(text=" ")]
945
946
  case "linebreak":
946
947
  return [ast.HardBreak()]
947
948
  case _:
948
949
  children = token.get("children", [])
949
950
  if children:
950
- return _flatten_children(children, parent_marks)
951
+ return _flatten_inlines(children, parent_marks)
951
952
  return []
952
953
 
953
954
 
@@ -959,40 +960,46 @@ def _inline_content_text(tokens: list[dict[str, Any]]) -> str:
959
960
 
960
961
 
961
962
  def _parse_inline_html(
962
- tag: str, adf_type: str, attrs: dict[str, str], content_tokens: list[dict[str, Any]]
963
- ) -> ast.Inline:
964
- content = _inline_content_text(content_tokens)
963
+ token: dict[str, Any], parent_marks: list[ast.Mark]
964
+ ) -> list[ast.Inline]:
965
+ tag = token["tag"]
966
+ adf_type = token["adf_type"]
967
+ attrs = token["attrs"]
968
+ inner = token["inner"]
969
+ content = _inline_content_text(inner)
965
970
  match adf_type:
966
971
  case "mention":
967
- return _parse_mention(attrs, content)
972
+ return [_parse_mention(attrs, content)]
968
973
  case "emoji":
969
- return _parse_emoji(attrs, content)
974
+ return [_parse_emoji(attrs, content)]
970
975
  case "date":
971
- return _parse_date(attrs)
976
+ return [_parse_date(attrs)]
972
977
  case "status":
973
- return _parse_status(attrs, content)
978
+ return [_parse_status(attrs, content)]
974
979
  case "inlineCard":
975
- return _parse_inline_card(attrs)
980
+ return [_parse_inline_card(attrs)]
976
981
  case "placeholder":
977
- return _parse_placeholder(content)
982
+ return [_parse_placeholder(content)]
978
983
  case "mediaInline":
979
- return _parse_media_inline(attrs)
984
+ return [_parse_media_inline(attrs)]
980
985
  case "inlineExtension":
981
- return _parse_inline_data(attrs)
986
+ return [_parse_inline_data(attrs)]
982
987
  case "underline" | "textColor" | "bgColor" | "subSup" | "annotation":
983
- return _parse_html_mark(tag, adf_type, attrs, content_tokens, content)
988
+ return _parse_html_mark(tag, adf_type, attrs, inner, parent_marks)
984
989
  case _:
985
- return ast.Text(text=content)
990
+ text = ast.Text(text=content)
991
+ if parent_marks:
992
+ text.marks = list(parent_marks)
993
+ return [text]
986
994
 
987
995
 
988
996
  def _parse_html_mark(
989
997
  tag: str,
990
998
  adf_type: str,
991
999
  attrs: dict[str, str],
992
- content_tokens: list[dict[str, Any]],
993
- content: str,
994
- ) -> ast.Inline:
995
- """Parse HTML mark element → Text with mark attached."""
1000
+ inner: list[dict[str, Any]],
1001
+ parent_marks: list[ast.Mark],
1002
+ ) -> list[ast.Inline]:
996
1003
  mark: ast.Mark
997
1004
  match adf_type:
998
1005
  case "underline":
@@ -1010,13 +1017,17 @@ def _parse_html_mark(
1010
1017
  annotation_type=p.get("annotationType", "inlineComment"),
1011
1018
  )
1012
1019
  case _:
1013
- return ast.Text(text=content)
1020
+ content = _inline_content_text(inner)
1021
+ text = ast.Text(text=content)
1022
+ if parent_marks:
1023
+ text.marks = list(parent_marks)
1024
+ return [text]
1014
1025
 
1015
- inlines = _parse_inlines(content_tokens)
1026
+ inlines = _flatten_inlines(inner, parent_marks)
1016
1027
  for node in inlines:
1017
1028
  if isinstance(node, ast.Text):
1018
1029
  node.marks = [*node.marks, mark]
1019
- return inlines[0] if inlines else ast.Text(text=content)
1030
+ return inlines
1020
1031
 
1021
1032
 
1022
1033
  def _parse_mention(attrs: dict[str, str], content: str) -> ast.Mention:
File without changes
File without changes
File without changes