Improved markdown rendering

This commit is contained in:
Mark Qvist 2025-02-17 21:55:50 +01:00
parent b4a063a4e7
commit 54000a72c7
3 changed files with 23 additions and 16 deletions

View File

@ -1527,7 +1527,7 @@ class SidebandApp(MDApp):
def md_to_bbcode(self, text):
if not hasattr(self, "mdconv"):
from md2bbcode.main import process_readme as mdconv
from .md2bbcode.main import process_readme as mdconv
self.mdconv = mdconv
converted = self.mdconv(text)
while converted.endswith("\n"):

View File

@ -17,9 +17,9 @@ from mistune.plugins.abbr import abbr
from mistune.plugins.spoiler import spoiler
# local
from md2bbcode.plugins.merge_lists import merge_ordered_lists
from md2bbcode.renderers.bbcode import BBCodeRenderer
from md2bbcode.html2bbcode import process_html
from .plugins.merge_lists import merge_ordered_lists
from .renderers.bbcode import BBCodeRenderer
from .html2bbcode import process_html
def convert_markdown_to_bbcode(markdown_text, domain):
# Create a Markdown parser instance using the custom BBCode renderer
@ -32,11 +32,6 @@ def process_readme(markdown_text, domain=None, debug=False):
# Convert Markdown to BBCode
bbcode_text = convert_markdown_to_bbcode(markdown_text, domain)
# If debug mode, save intermediate BBCode
if debug:
with open('readme.1stpass', 'w', encoding='utf-8') as file:
file.write(bbcode_text)
# Convert BBCode formatted as HTML to final BBCode
final_bbcode = process_html(bbcode_text, debug, 'readme.finalpass')

View File

@ -26,6 +26,7 @@ class BBCodeRenderer(BaseRenderer):
return func(**attrs)
else:
return func()
if attrs:
return func(text, **attrs)
else:
@ -69,7 +70,7 @@ class BBCodeRenderer(BaseRenderer):
return '\n'
def softbreak(self) -> str:
return ''
return '\n'
def inline_html(self, html: str) -> str:
if self._escape:
@ -126,13 +127,24 @@ class BBCodeRenderer(BaseRenderer):
return '[color=red][icode]' + text + '[/icode][/color]\n'
def list(self, text: str, ordered: bool, **attrs) -> str:
# For ordered lists, always use [list=1] to get automatic sequential numbering
# For unordered lists, use [list]
tag = 'list=1' if ordered else 'list'
return '[{}]'.format(tag) + text + '[/list]\n'
depth = 0; sln = ""; tli = ""
if "depth" in attrs: depth = attrs["depth"]
if depth != 0: sln = "\n"
if depth == 0: tli = "\n"
def remove_empty_lines(text):
lines = text.split('\n')
non_empty_lines = [line for line in lines if line.strip() != '']
nli = ""; dlm = "\n"+" "*depth
if depth != 0: nli = dlm
return nli+dlm.join(non_empty_lines)
text = remove_empty_lines(text)
return sln+text+"\n"+tli
# return '[{}]'.format(tag) + text + '[/list]\n'
def list_item(self, text: str) -> str:
return '[*]' + text + '\n'
return '' + text + '\n'
def strikethrough(self, text: str) -> str:
return '[s]' + text + '[/s]'
@ -209,7 +221,7 @@ class BBCodeRenderer(BaseRenderer):
def task_list_item(self, text: str, checked: bool = False) -> str:
# Using emojis to represent the checkbox
checkbox_emoji = '🗹' if checked else ''
checkbox_emoji = '󰱒' if checked else '󰄱'
return checkbox_emoji + ' ' + text + '\n'
def def_list(self, text: str) -> str: