fix nested quotes and alter linebreak parsing

This commit is contained in:
2025-05-26 02:44:11 +03:00
parent 6eee661b58
commit 94f58fef73
3 changed files with 29 additions and 15 deletions

View File

@ -22,25 +22,27 @@ function babycode.to_html(s, escape_html)
return "\1CODE:"..#code_blocks.."\1"
end
end)
text = text:gsub(" %s?\r?\n", "<br>")
-- normalize newlines
text = text:gsub("\r?\n\r?\n+", "<br>")
local url_tags = {}
-- replace `[url=https://example.com]Example[/url] tags
text = text:gsub("%[url=([^%]]+)%](.-)%[/url%]", function(url, label)
table.insert(url_tags, {url = url, label = label})
return "\1URL:"..#url_tags.."\1"
end)
-- replace `[url]https://example.com[/url] tags
-- text = text:gsub("%[url%]([^%]]+)%[/url%]", function(url)
-- return '<a href="'..url..'">'..url..'</a>'
-- end)
-- bold, italics, strikethrough
text = text:gsub("%[b%](.-)%[/b%]", "<strong>%1</strong>")
text = text:gsub("%[i%](.-)%[/i%]", "<em>%1</em>")
text = text:gsub("%[s%](.-)%[/s%]", "<del>%1</del>")
text = text:gsub("%[quote%](.-)%[/quote%]", "<blockquote>%1</blockquote>")
-- these can be nested, so replace open and closed separately
-- text = text:gsub("%[quote%]", "<blockquote>")
-- text = text:gsub("%[/quote%]", "</blockquote>")
text = text:gsub("%[(/?)quote%]", "<%1blockquote>")
-- replace loose links
text = text:gsub("(https?://[%w-_%.%?%.:/%+=&~%@#%%]+[%w-/])", function(url)
@ -57,9 +59,6 @@ function babycode.to_html(s, escape_html)
-- rule
text = text:gsub("\n+%-%-%-", "<hr>")
-- normalize newlines, replace them with <br>
text = text:gsub("\r?\n\r?\n+", "<br>")--:gsub("\r?\n", "<br>")
-- replace code block placeholders back with their original contents
text = text:gsub("\1CODE:(%d+)\1", function(n)