Skip to content

Commit 4240c06

Browse files
committed
feat(markdown_to_bbcode): add option to skip table of contents during conversion
function convert_markdown_to_bbcode(markdown_text, repo_name, bbcode_type, relative_path, skip_toc): +added skip_toc parameter to control TOC handling. function parse_arguments(): +added --skip-toc argument for command line interface.
1 parent b915b41 commit 4240c06

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

markdown_to_bbcode.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import subprocess
66
from urllib.parse import urljoin
77

8-
def convert_markdown_to_bbcode(markdown_text, repo_name=None, bbcode_type='egosoft', relative_path=None):
8+
def convert_markdown_to_bbcode(markdown_text, repo_name=None, bbcode_type='egosoft', relative_path=None, skip_toc=False):
99
"""
1010
Converts Markdown formatted text to BBCode formatted text.
1111
@@ -22,6 +22,30 @@ def convert_markdown_to_bbcode(markdown_text, repo_name=None, bbcode_type='egoso
2222

2323
bbcode_text = markdown_text
2424

25+
# Optionally strip table of contents blocks from the document before conversion
26+
if skip_toc:
27+
lines = bbcode_text.splitlines()
28+
processed_lines = []
29+
i = 0
30+
while i < len(lines):
31+
line = lines[i]
32+
if re.match(r'^\s{0,3}#{1,6}\s+table of contents\b', line, flags=re.IGNORECASE):
33+
# Skip the heading and any immediately following list entries
34+
i += 1
35+
while i < len(lines):
36+
next_line = lines[i]
37+
if next_line.strip() == "":
38+
i += 1
39+
continue
40+
if re.match(r'^\s*(?:[-*+]\s+|\d+\.\s+)', next_line):
41+
i += 1
42+
continue
43+
break
44+
continue
45+
processed_lines.append(line)
46+
i += 1
47+
bbcode_text = "\n".join(processed_lines)
48+
2549
# 1. Headers
2650
# Define size mapping based on BBCode type
2751
header_level_mapping = {
@@ -250,6 +274,8 @@ def parse_arguments():
250274
help='Type of BBCode format to use (default: egosoft).')
251275
parser.add_argument('-r', '--repo', help='GitHub repository name (e.g., username/repo) to generate absolute image URLs.', default=None)
252276
parser.add_argument('-o', '--output-folder', help='Path to the output folder. Defaults to the current directory.', default='.')
277+
parser.add_argument('--skip-toc', action='store_true',
278+
help='Leave Markdown table of contents sections unchanged during conversion.')
253279

254280
return parser.parse_args()
255281

@@ -323,7 +349,13 @@ def main():
323349
relative_path = os.path.dirname(input_path)
324350

325351
# Convert Markdown to BBCode
326-
bbcode_result = convert_markdown_to_bbcode(markdown_content, repo_name=repo_name, bbcode_type=bbcode_type, relative_path=relative_path)
352+
bbcode_result = convert_markdown_to_bbcode(
353+
markdown_content,
354+
repo_name=repo_name,
355+
bbcode_type=bbcode_type,
356+
relative_path=relative_path,
357+
skip_toc=args.skip_toc
358+
)
327359

328360
# Generate output file name
329361
output_path = generate_output_filename(input_path, bbcode_type, output_folder)

0 commit comments

Comments
 (0)