Skip to content

Commit 384519c

Browse files
committed
refactor: Rearrange link handling in Markdown to BBCode conversion for improved clarity
1 parent dd4b8e3 commit 384519c

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

markdown_to_bbcode.py

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -111,39 +111,7 @@ def replace_block_code(match):
111111

112112
bbcode_text = re.sub(r'```(\w+)?\n([\s\S]*?)\n(\s*)```', replace_block_code, bbcode_text)
113113

114-
# 4. Links
115-
# Convert [text](url) to [url=url]text[/url]
116-
def replace_links(match):
117-
link_text, link_url = match.groups()
118-
if bbcode_type == 'steam' and 'youtube.com/watch?v=' in link_url:
119-
video_id = re.search(r'v=([^&]+)', link_url).group(1)
120-
return f"{link_text}\n[previewyoutube={video_id};full][/previewyoutube]"
121-
elif bbcode_type == 'nexus' and 'youtube.com/watch?v=' in link_url:
122-
video_id = re.search(r'v=([^&]+)', link_url).group(1)
123-
return f"{link_text}\n[youtube]{video_id}[/youtube]"
124-
elif repo_name and not re.match(r'^https?://', link_url):
125-
absolute_url = f"https://github.com/{repo_name}/raw/main/{relative_path}/{link_url}"
126-
return f"[url={absolute_url}]{link_text}[/url]"
127-
else:
128-
return f"[url={link_url}]{link_text}[/url]"
129-
130-
bbcode_text = re.sub(r'\[(.*?)\]\((.*?)\)', replace_links, bbcode_text)
131-
132-
# 5. Bold
133-
# Convert **text** or __text__ to [b]text[/b]
134-
bbcode_text = re.sub(r'(\*\*|__)(.*?)\1', r'[b]\2[/b]', bbcode_text)
135-
136-
# 6. Italics
137-
# Convert *text* or _text_ to [i]text[/i]
138-
# Only match if the marker is preceded by a space or start of line
139-
# This prevents matching underscores within URLs or words like some_word
140-
bbcode_text = re.sub(r'(^|\s)(\*|_)(?!\2)(.*?)\2', r'\1[i]\3[/i]', bbcode_text)
141-
142-
# 7. Inline Code
143-
# Convert `text` to [b]text[/b]
144-
bbcode_text = re.sub(r'`([^`\n]+)`', r'[b]\1[/b]', bbcode_text)
145-
146-
# 8. Lists
114+
# 4. Lists
147115
# Convert unordered and ordered lists to BBCode
148116
def parse_list_items(lines):
149117
list_stack = []
@@ -226,6 +194,38 @@ def replace_lists(match):
226194
bbcode_text = re.sub(r'(?:^\s*[-*+]\s+.*\n?)+', replace_lists, bbcode_text, flags=re.MULTILINE)
227195
bbcode_text = re.sub(r'(?:^\s*\d+\.\s+.*\n?)+', replace_lists, bbcode_text, flags=re.MULTILINE)
228196

197+
# 5. Links
198+
# Convert [text](url) to [url=url]text[/url]
199+
def replace_links(match):
200+
link_text, link_url = match.groups()
201+
if bbcode_type == 'steam' and 'youtube.com/watch?v=' in link_url:
202+
video_id = re.search(r'v=([^&]+)', link_url).group(1)
203+
return f"{link_text}\n[previewyoutube={video_id};full][/previewyoutube]"
204+
elif bbcode_type == 'nexus' and 'youtube.com/watch?v=' in link_url:
205+
video_id = re.search(r'v=([^&]+)', link_url).group(1)
206+
return f"{link_text}\n[youtube]{video_id}[/youtube]"
207+
elif repo_name and not re.match(r'^https?://', link_url):
208+
absolute_url = f"https://github.com/{repo_name}/raw/main/{relative_path}/{link_url}"
209+
return f"[url={absolute_url}]{link_text}[/url]"
210+
else:
211+
return f"[url={link_url}]{link_text}[/url]"
212+
213+
bbcode_text = re.sub(r'\[([^[]+)\]\((.*?)\)', replace_links, bbcode_text)
214+
215+
# 6. Bold
216+
# Convert **text** or __text__ to [b]text[/b]
217+
bbcode_text = re.sub(r'(\*\*|__)(.*?)\1', r'[b]\2[/b]', bbcode_text)
218+
219+
# 7. Italics
220+
# Convert *text* or _text_ to [i]text[/i]
221+
# Only match if the marker is preceded by a space or start of line
222+
# This prevents matching underscores within URLs or words like some_word
223+
bbcode_text = re.sub(r'(^|\s)(\*|_)(?!\2)(.*?)\2', r'\1[i]\3[/i]', bbcode_text)
224+
225+
# 8. Inline Code
226+
# Convert `text` to [b]text[/b]
227+
bbcode_text = re.sub(r'`([^`\n]+)`', r'[b]\1[/b]', bbcode_text)
228+
229229
# 9. Blockquotes
230230
# Convert > Quote to [quote]Quote[/quote]
231231
def replace_blockquotes(match):

0 commit comments

Comments
 (0)