-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown_to_bbcode.py
More file actions
374 lines (327 loc) · 16.1 KB
/
markdown_to_bbcode.py
File metadata and controls
374 lines (327 loc) · 16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import re
import argparse
import sys
import os
import subprocess
from urllib.parse import urljoin
def convert_markdown_to_bbcode(markdown_text, repo_name=None, bbcode_type='egosoft', relative_path=None, skip_toc=False):
"""
Converts Markdown formatted text to BBCode formatted text.
Args:
markdown_text (str): The Markdown text to be converted.
repo_name (str, optional): GitHub repository name in the format 'username/repo'.
Used to generate absolute URLs for images and links.
bbcode_type (str): Type of BBCode format ('egosoft', 'nexus', or 'steam').
relative_path (str, optional): Relative path of the input file.
Returns:
str: The converted BBCode text.
"""
bbcode_text = markdown_text
# Optionally strip table of contents blocks from the document before conversion
if skip_toc:
lines = bbcode_text.splitlines()
processed_lines = []
i = 0
while i < len(lines):
line = lines[i]
if re.match(r'^\s{0,3}#{1,6}\s+table of contents\b', line, flags=re.IGNORECASE):
# Skip the heading and any immediately following list entries
i += 1
while i < len(lines):
next_line = lines[i]
if next_line.strip() == "":
i += 1
continue
if re.match(r'^\s*(?:[-*+]\s+|\d+\.\s+)', next_line):
i += 1
continue
break
continue
processed_lines.append(line)
i += 1
bbcode_text = "\n".join(processed_lines)
# 1. Headers
# Define size mapping based on BBCode type
header_level_mapping = {
'egosoft': {
1: {'size': 140, 'underline': True, 'italic': False, 'bold': False},
2: {'size': 130, 'underline': True, 'italic': False, 'bold': False},
3: {'size': 125, 'underline': True, 'italic': False, 'bold': False},
4: {'size': 120, 'underline': True, 'italic': False, 'bold': False},
5: {'size': 115, 'underline': True, 'italic': False, 'bold': False},
6: {'size': 110, 'underline': True, 'italic': False, 'bold': False}
},
'nexus': {
1: {'size': 4, 'underline': True, 'italic': False, 'bold': False},
2: {'size': 3, 'underline': True, 'italic': False, 'bold': True},
3: {'size': 3, 'underline': True, 'italic': True, 'bold': False},
4: {'size': 3, 'underline': True, 'italic': False, 'bold': False},
5: {'size': 2, 'underline': True, 'italic': True, 'bold': False},
6: {'size': 2, 'underline': True, 'italic': False, 'bold': False}
},
'steam': {
1: {'h': 1, 'underline': False, 'italic': False, 'bold': False},
2: {'h': 2, 'underline': False, 'italic': False, 'bold': True},
3: {'h': 2, 'underline': False, 'italic': False, 'bold': False},
4: {'h': 3, 'underline': False, 'italic': False, 'bold': True},
5: {'h': 3, 'underline': True, 'italic': False, 'bold': False},
6: {'h': 3, 'underline': False, 'italic': False, 'bold': False}
}
}
current_header_level_mapping = header_level_mapping.get(bbcode_type, header_level_mapping['egosoft'])
# Convert Markdown headers to BBCode with size, bold, underline, and italic
def replace_headers(match):
hashes, header_text = match.groups()
level = len(hashes)
header_attrs = current_header_level_mapping.get(level, {'size': 100, 'underline': True, 'italic': False, 'bold': True})
if bbcode_type == 'steam':
h = header_attrs['h']
underline = '[u]' if header_attrs['underline'] else ''
italic = '[i]' if header_attrs['italic'] else ''
bold = '[b]' if header_attrs['bold'] else ''
underline_close = '[/u]' if header_attrs['underline'] else ''
italic_close = '[/i]' if header_attrs['italic'] else ''
bold_close = '[/b]' if header_attrs['bold'] else ''
return f"[h{h}]{underline}{italic}{bold}{header_text.strip()}{bold_close}{italic_close}{underline_close}[/h{h}]"
else:
size = header_attrs['size']
underline = '[u]' if header_attrs['underline'] else ''
italic = '[i]' if header_attrs['italic'] else ''
bold = '[b]' if header_attrs['bold'] else ''
underline_close = '[/u]' if header_attrs['underline'] else ''
italic_close = '[/i]' if header_attrs['italic'] else ''
bold_close = '[/b]' if header_attrs['bold'] else ''
return f"[size={size}]{underline}{italic}{bold}{header_text.strip()}{bold_close}{italic_close}{underline_close}[/size]"
bbcode_text = re.sub(r'^(#{1,6})\s+(.*)', replace_headers, bbcode_text, flags=re.MULTILINE)
# 2. Images
def replace_images(match):
image_url = match.group(1)
if repo_name and not re.match(r'^https?://', image_url):
absolute_url = f"raw.githubusercontent.com/{repo_name}/refs/heads/main/{relative_path}/{image_url}"
absolute_url = absolute_url.replace('//', '/')
absolute_url = f"https://{absolute_url}"
if bbcode_type == 'egosoft':
return f"[spoiler][img]{absolute_url}[/img][/spoiler]"
else:
return f"[img]{absolute_url}[/img]"
else:
if bbcode_type == 'egosoft':
return f"[spoiler][img]{image_url}[/img][/spoiler]"
else:
return f"[img]{image_url}[/img]"
bbcode_text = re.sub(r'!\[.*?\]\((.*?)\)', replace_images, bbcode_text)
# 3. Block Code
# Convert ```language\ncode\n``` to [code=language]code[/code] for 'egosoft'
# and to [code]code[/code] for 'nexus' and 'steam'
def replace_block_code(match):
lang = match.group(1) if match.group(1) else ''
code = match.group(2)
indent = match.group(3)
if bbcode_type == 'egosoft' and lang:
return f"[code={lang}]\n{code}\n{indent}[/code]"
else:
return f"[code]\n{code}\n{indent}[/code]"
bbcode_text = re.sub(r'```(\w+)?\n([\s\S]*?)\n(\s*)```', replace_block_code, bbcode_text)
# 4. Lists
# Convert unordered and ordered lists to BBCode
def parse_list_items(lines):
list_stack = []
current_list = []
current_indent = 0
list_type = 'unordered'
for line in lines:
stripped_line = line.lstrip()
indent = len(line) - len(stripped_line)
if stripped_line.startswith(('-', '*', '+')):
item = stripped_line[1:].strip()
if indent > current_indent:
list_stack.append((current_list, current_indent, list_type))
current_list = []
current_indent = indent
elif indent < current_indent:
while list_stack and indent < current_indent:
parent_list, parent_indent, parent_type = list_stack.pop()
if parent_type == 'ordered':
parent_list.append(f"[list=1]\n" + "\n".join(current_list) + "\n[/list]")
else:
parent_list.append(f"[list]\n" + "\n".join(current_list) + "\n[/list]")
current_list = parent_list
current_indent = parent_indent
list_type = parent_type
current_list.append(f"[*] {item}")
list_type = 'unordered'
elif re.match(r'^\s*\d+\.\s+', stripped_line):
item = re.sub(r'^\s*\d+\.\s+', '', stripped_line)
if indent > current_indent:
list_stack.append((current_list, current_indent, list_type))
current_list = []
current_indent = indent
elif indent < current_indent:
while list_stack and indent < current_indent:
parent_list, parent_indent, parent_type = list_stack.pop()
if parent_type == 'ordered':
parent_list.append(f"[list=1]\n" + "\n".join(current_list) + "\n[/list]")
else:
parent_list.append(f"[list]\n" + "\n".join(current_list) + "\n[/list]")
current_list = parent_list
current_indent = parent_indent
list_type = parent_type
current_list.append(f"[*] {item}")
list_type = 'ordered'
else:
current_list.append(line)
while list_stack:
parent_list, parent_indent, parent_type = list_stack.pop()
if parent_type == 'ordered':
parent_list.append(f"[list=1]\n" + "\n".join(current_list) + "\n[/list]")
else:
parent_list.append(f"[list]\n" + "\n".join(current_list) + "\n[/list]")
current_list = parent_list
if list_type == 'ordered':
return "[list=1]" + "\n".join(current_list) + "\n[/list]"
else:
return "[list]" + "\n".join(current_list) + "\n[/list]"
def replace_lists(match):
list_content = match.group(0)
lines = list_content.split('\n')
return parse_list_items(lines).replace('\n\n', '\n')
bbcode_text = re.sub(r'(?:^\s*[-*+]\s+.*\n?)+', replace_lists, bbcode_text, flags=re.MULTILINE)
bbcode_text = re.sub(r'(?:^\s*\d+\.\s+.*\n?)+', replace_lists, bbcode_text, flags=re.MULTILINE)
# 5. Links
# Convert [text](url) to [url=url]text[/url]
def replace_links(match):
link_text, link_url = match.groups()
if bbcode_type == 'steam' and 'youtube.com/watch?v=' in link_url:
video_id = re.search(r'v=([^&]+)', link_url).group(1)
return f"{link_text}\n[previewyoutube={video_id};full][/previewyoutube]"
elif bbcode_type == 'nexus' and 'youtube.com/watch?v=' in link_url:
video_id = re.search(r'v=([^&]+)', link_url).group(1)
return f"{link_text}\n[youtube]{video_id}[/youtube]"
elif repo_name and not re.match(r'^https?://', link_url):
absolute_url = f"https://github.com/{repo_name}/raw/main/{relative_path}/{link_url}"
absolute_url = urljoin(urljoin(f"https://github.com/{repo_name}/raw/main/", relative_path.strip('/')), link_url.strip('/'))
return f"[url={absolute_url}]{link_text}[/url]"
else:
return f"[url={link_url}]{link_text}[/url]"
bbcode_text = re.sub(r'\[([^[]+)\]\((.*?)\)', replace_links, bbcode_text)
# 6. Bold
# Convert **text** or __text__ to [b]text[/b]
bbcode_text = re.sub(r'(\*\*|__)(.*?)\1', r'[b]\2[/b]', bbcode_text)
# 7. Italics
# Convert *text* or _text_ to [i]text[/i]
# Only match if the marker is preceded by a space or start of line
# This prevents matching underscores within URLs or words like some_word
bbcode_text = re.sub(r'(^|\s)(\*|_)(?!\2)(.*?)\2', r'\1[i]\3[/i]', bbcode_text)
# 8. Inline Code
# Convert `text` to [b]text[/b]
bbcode_text = re.sub(r'`([^`\n]+)`', r'[b]\1[/b]', bbcode_text)
# 9. Blockquotes
# Convert > Quote to [quote]Quote[/quote]
def replace_blockquotes(match):
quote = match.group(1)
return f"[quote]{quote.strip()}[/quote]"
bbcode_text = re.sub(r'^>\s?(.*)', replace_blockquotes, bbcode_text, flags=re.MULTILINE)
# 10. Horizontal Rules
# Convert --- or *** or ___ to [hr]
bbcode_text = re.sub(r'^(\*\*\*|---|___)$', r'[hr]', bbcode_text, flags=re.MULTILINE)
# 11. Line Breaks
# Convert two or more spaces at the end of a line to [br]
bbcode_text = re.sub(r' {2,}\n', r'[br]\n', bbcode_text)
return bbcode_text
def parse_arguments():
"""
Parses command-line arguments.
Returns:
argparse.Namespace: Parsed arguments containing input file path, BBCode type, repo name, and output folder.
"""
parser = argparse.ArgumentParser(
description='Convert a Markdown file to BBCode format.',
epilog='Example usage: python markdown_to_bbcode.py input.md --type nexus --repo username/repo --output-folder ./output'
)
parser.add_argument('input_file', help='Path to the input Markdown file.')
parser.add_argument('-t', '--type', choices=['egosoft', 'nexus', 'steam'], default='egosoft',
help='Type of BBCode format to use (default: egosoft).')
parser.add_argument('-r', '--repo', help='GitHub repository name (e.g., username/repo) to generate absolute image URLs.', default=None)
parser.add_argument('-o', '--output-folder', help='Path to the output folder. Defaults to the current directory.', default='.')
parser.add_argument('--skip-toc', action='store_true',
help='Leave Markdown table of contents sections unchanged during conversion.')
return parser.parse_args()
def generate_output_filename(input_file, bbcode_type, output_folder):
"""
Generates the output file name based on the input file, BBCode type, and output folder.
Args:
input_file (str): Path to the input Markdown file.
bbcode_type (str): Type of BBCode format ('egosoft' or 'nexus').
output_folder (str): Path to the output folder.
Returns:
str: Generated output file path.
"""
folder = os.path.dirname(input_file)
base = os.path.splitext(os.path.basename(input_file))[0]
output_filename = f"{base}.{bbcode_type}"
return os.path.join(folder, output_folder, output_filename)
def get_repo_name():
"""
Retrieves the GitHub repository name from the Git configuration.
Returns:
str: The repository name in the format 'username/repo'.
"""
try:
repo_url = subprocess.check_output(['git', 'config', '--get', 'remote.origin.url'], encoding='utf-8').strip()
pattern = re.compile(r"([^/:]+)/([^/]+?)(?:\.git)?$")
match = pattern.search(repo_url)
if match:
return f"{match.group(1)}/{match.group(2)}"
else:
print(f"Error: Unsupported repository URL format: {repo_url}")
sys.exit(1)
except subprocess.CalledProcessError as e:
print(f"Error: Unable to retrieve repository URL: {e}")
sys.exit(1)
def main():
args = parse_arguments()
input_path = args.input_file
bbcode_type = args.type
repo_name = args.repo or get_repo_name()
output_folder = args.output_folder
# Check if input file exists
if not os.path.isfile(input_path):
print(f"Error: The input file '{input_path}' does not exist.")
sys.exit(1)
# Create output folder if it doesn't exist
if not os.path.isdir(output_folder):
try:
os.makedirs(output_folder)
print(f"Created output directory: '{output_folder}'")
except Exception as e:
print(f"Error creating output directory '{output_folder}': {e}")
sys.exit(1)
try:
with open(input_path, 'r', encoding='utf-8') as infile:
markdown_content = infile.read()
except Exception as e:
print(f"Error reading the input file: {e}")
sys.exit(1)
# Extract relative path part from input_path
relative_path = os.path.dirname(input_path)
# Convert Markdown to BBCode
bbcode_result = convert_markdown_to_bbcode(
markdown_content,
repo_name=repo_name,
bbcode_type=bbcode_type,
relative_path=relative_path,
skip_toc=args.skip_toc
)
# Generate output file name
output_path = generate_output_filename(input_path, bbcode_type, output_folder)
try:
if not os.path.exists(os.path.dirname(output_path)):
os.makedirs(os.path.dirname(output_path), exist_ok=True)
with open(output_path, 'w', encoding='utf-8') as outfile:
outfile.write(bbcode_result)
print(f"Successfully converted '{input_path}' to '{output_path}'.")
except Exception as e:
print(f"Error writing to the output file: {e}")
sys.exit(1)
if __name__ == "__main__":
main()