|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Convert Jira wiki markup (template subset) to Atlassian Document Format JSON. |
| 3 | +
|
| 4 | +Handles: headings (hN.), paragraphs, bullet lists (* item), ordered lists (# item), |
| 5 | + task items ((?) TODO, (/) DONE), bold (*text*), italic (_text_), |
| 6 | + monospace ({{text}} and `backtick`). |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python scripts/jira-wiki-to-adf.py <input.txt> # stdout |
| 10 | + python scripts/jira-wiki-to-adf.py <input.txt> <output.json> # file |
| 11 | +""" |
| 12 | + |
| 13 | +import json |
| 14 | +import re |
| 15 | +import sys |
| 16 | + |
| 17 | + |
| 18 | +def parse_inline(text): |
| 19 | + """Parse Jira wiki inline marks into ADF text nodes. |
| 20 | +
|
| 21 | + Order matters: bold (*text*) is matched before bare asterisks, |
| 22 | + monospace before backticks. |
| 23 | + """ |
| 24 | + nodes = [] |
| 25 | + pattern = re.compile(r'\*([^*\n]+)\*|_([^_\n]+)_|\{\{([^}]+)\}\}|`([^`\n]+)`') |
| 26 | + last = 0 |
| 27 | + for m in pattern.finditer(text): |
| 28 | + if m.start() > last: |
| 29 | + nodes.append({"type": "text", "text": text[last:m.start()]}) |
| 30 | + if m.group(1) is not None: # *bold* |
| 31 | + nodes.append({"type": "text", "text": m.group(1), "marks": [{"type": "strong"}]}) |
| 32 | + elif m.group(2) is not None: # _italic_ |
| 33 | + nodes.append({"type": "text", "text": m.group(2), "marks": [{"type": "em"}]}) |
| 34 | + elif m.group(3) is not None: # {{monospace}} |
| 35 | + nodes.append({"type": "text", "text": m.group(3), "marks": [{"type": "code"}]}) |
| 36 | + else: # `backtick` |
| 37 | + nodes.append({"type": "text", "text": m.group(4), "marks": [{"type": "code"}]}) |
| 38 | + last = m.end() |
| 39 | + if last < len(text): |
| 40 | + nodes.append({"type": "text", "text": text[last:]}) |
| 41 | + return nodes or [{"type": "text", "text": ""}] |
| 42 | + |
| 43 | + |
| 44 | +def _para(text): |
| 45 | + return {"type": "paragraph", "content": parse_inline(text)} |
| 46 | + |
| 47 | + |
| 48 | +def _heading(level, text): |
| 49 | + return {"type": "heading", "attrs": {"level": level}, "content": parse_inline(text)} |
| 50 | + |
| 51 | + |
| 52 | +def _task_list(items, idx): |
| 53 | + return { |
| 54 | + "type": "taskList", |
| 55 | + "attrs": {"localId": f"tl-{idx}"}, |
| 56 | + "content": [ |
| 57 | + { |
| 58 | + "type": "taskItem", |
| 59 | + "attrs": {"localId": f"ti-{idx}-{i}", "state": "DONE" if checked else "TODO"}, |
| 60 | + "content": parse_inline(text), |
| 61 | + } |
| 62 | + for i, (checked, text) in enumerate(items) |
| 63 | + ], |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | +def _bullet_list(items): |
| 68 | + return { |
| 69 | + "type": "bulletList", |
| 70 | + "content": [ |
| 71 | + {"type": "listItem", "content": [_para(item)]} |
| 72 | + for item in items |
| 73 | + ], |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | +def _ordered_list(items): |
| 78 | + return { |
| 79 | + "type": "orderedList", |
| 80 | + "content": [ |
| 81 | + {"type": "listItem", "content": [_para(item)]} |
| 82 | + for item in items |
| 83 | + ], |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | +# hN. text (heading) |
| 88 | +HEADING_RE = re.compile(r'^h([1-6])\.\s+(.*)') |
| 89 | +# (?) text (task item, unchecked) |
| 90 | +TASK_TODO_RE = re.compile(r'^\(\?\)\s+(.*)') |
| 91 | +# (/) text (task item, checked) |
| 92 | +TASK_DONE_RE = re.compile(r'^\(/\)\s+(.*)') |
| 93 | +# * text (bullet — asterisk + whitespace; does NOT match *bold*) |
| 94 | +BULLET_RE = re.compile(r'^\*\s+(.*)') |
| 95 | +# # text or # text (ordered list — optional leading whitespace) |
| 96 | +ORDERED_RE = re.compile(r'^\s*#\s+(.*)') |
| 97 | + |
| 98 | + |
| 99 | +def convert(wiki): |
| 100 | + lines = wiki.splitlines() |
| 101 | + content = [] |
| 102 | + tl_idx = 0 |
| 103 | + i = 0 |
| 104 | + |
| 105 | + while i < len(lines): |
| 106 | + line = lines[i] |
| 107 | + s = line.strip() |
| 108 | + |
| 109 | + if not s: |
| 110 | + i += 1 |
| 111 | + continue |
| 112 | + |
| 113 | + # Heading |
| 114 | + m = HEADING_RE.match(s) |
| 115 | + if m: |
| 116 | + content.append(_heading(int(m.group(1)), m.group(2).strip())) |
| 117 | + i += 1 |
| 118 | + continue |
| 119 | + |
| 120 | + # Task items — collect consecutive (?) and (/) lines into one taskList |
| 121 | + if TASK_TODO_RE.match(s) or TASK_DONE_RE.match(s): |
| 122 | + items = [] |
| 123 | + while i < len(lines): |
| 124 | + s2 = lines[i].strip() |
| 125 | + mt = TASK_TODO_RE.match(s2) |
| 126 | + md = TASK_DONE_RE.match(s2) |
| 127 | + if mt: |
| 128 | + items.append((False, mt.group(1))) |
| 129 | + i += 1 |
| 130 | + elif md: |
| 131 | + items.append((True, md.group(1))) |
| 132 | + i += 1 |
| 133 | + else: |
| 134 | + break |
| 135 | + content.append(_task_list(items, tl_idx)) |
| 136 | + tl_idx += 1 |
| 137 | + continue |
| 138 | + |
| 139 | + # Bullet list (* item — requires space after *, so *bold* is not matched) |
| 140 | + m = BULLET_RE.match(s) |
| 141 | + if m: |
| 142 | + items = [] |
| 143 | + while i < len(lines): |
| 144 | + bm = BULLET_RE.match(lines[i].strip()) |
| 145 | + if bm: |
| 146 | + items.append(bm.group(1)) |
| 147 | + i += 1 |
| 148 | + else: |
| 149 | + break |
| 150 | + if items: |
| 151 | + content.append(_bullet_list(items)) |
| 152 | + continue |
| 153 | + |
| 154 | + # Ordered list (# item or # item with leading whitespace) |
| 155 | + m = ORDERED_RE.match(line) |
| 156 | + if m: |
| 157 | + items = [] |
| 158 | + while i < len(lines): |
| 159 | + om = ORDERED_RE.match(lines[i]) |
| 160 | + if om: |
| 161 | + items.append(om.group(1)) |
| 162 | + i += 1 |
| 163 | + else: |
| 164 | + break |
| 165 | + if items: |
| 166 | + content.append(_ordered_list(items)) |
| 167 | + continue |
| 168 | + |
| 169 | + # Paragraph — collect consecutive non-special lines |
| 170 | + para_lines = [] |
| 171 | + while i < len(lines): |
| 172 | + s2 = lines[i].strip() |
| 173 | + raw = lines[i] |
| 174 | + if not s2: |
| 175 | + break |
| 176 | + if HEADING_RE.match(s2): |
| 177 | + break |
| 178 | + if TASK_TODO_RE.match(s2) or TASK_DONE_RE.match(s2): |
| 179 | + break |
| 180 | + if BULLET_RE.match(s2): |
| 181 | + break |
| 182 | + if ORDERED_RE.match(raw): |
| 183 | + break |
| 184 | + para_lines.append(s2) |
| 185 | + i += 1 |
| 186 | + if para_lines: |
| 187 | + content.append(_para(' '.join(para_lines))) |
| 188 | + |
| 189 | + return {"version": 1, "type": "doc", "content": content} |
| 190 | + |
| 191 | + |
| 192 | +if __name__ == '__main__': |
| 193 | + if len(sys.argv) < 2: |
| 194 | + print("Usage: jira-wiki-to-adf.py <input.txt> [output.json]", file=sys.stderr) |
| 195 | + sys.exit(1) |
| 196 | + |
| 197 | + with open(sys.argv[1]) as f: |
| 198 | + wiki = f.read() |
| 199 | + |
| 200 | + output = json.dumps(convert(wiki), ensure_ascii=False) |
| 201 | + |
| 202 | + if len(sys.argv) >= 3: |
| 203 | + with open(sys.argv[2], 'w') as f: |
| 204 | + f.write(output) |
| 205 | + else: |
| 206 | + print(output) |
0 commit comments