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): def md_to_bbcode(self, text):
if not hasattr(self, "mdconv"): if not hasattr(self, "mdconv"):
from md2bbcode.main import process_readme as mdconv from .md2bbcode.main import process_readme as mdconv
self.mdconv = mdconv self.mdconv = mdconv
converted = self.mdconv(text) converted = self.mdconv(text)
while converted.endswith("\n"): while converted.endswith("\n"):

View File

@ -17,9 +17,9 @@ from mistune.plugins.abbr import abbr
from mistune.plugins.spoiler import spoiler from mistune.plugins.spoiler import spoiler
# local # local
from md2bbcode.plugins.merge_lists import merge_ordered_lists from .plugins.merge_lists import merge_ordered_lists
from md2bbcode.renderers.bbcode import BBCodeRenderer from .renderers.bbcode import BBCodeRenderer
from md2bbcode.html2bbcode import process_html from .html2bbcode import process_html
def convert_markdown_to_bbcode(markdown_text, domain): def convert_markdown_to_bbcode(markdown_text, domain):
# Create a Markdown parser instance using the custom BBCode renderer # 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 # Convert Markdown to BBCode
bbcode_text = convert_markdown_to_bbcode(markdown_text, domain) 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 # Convert BBCode formatted as HTML to final BBCode
final_bbcode = process_html(bbcode_text, debug, 'readme.finalpass') final_bbcode = process_html(bbcode_text, debug, 'readme.finalpass')

View File

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