From 11dbec079355efb7de9a635f85f48cc844b452cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= Date: Sat, 24 May 2025 15:47:31 +0300 Subject: [PATCH] better babycode parsing, add horizontal rule --- lib/babycode.lua | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/babycode.lua b/lib/babycode.lua index d5d5647..2c441c6 100644 --- a/lib/babycode.lua +++ b/lib/babycode.lua @@ -8,22 +8,22 @@ function babycode.to_html(s, escape_html) -- extract code blocks first and store them as placeholders -- don't want to process bbcode embedded into a code block local code_blocks = {} - local code_count = 0 + s = escape_html(s) local text = s:gsub("%[code%](.-)%[/code%]", function(code) - code_count = code_count + 1 -- strip leading and trailing newlines, preserve others - code_blocks[code_count] = code:gsub("^%s*(.-)%s*$", "%1") - return "\1CODE:"..code_count.."\1" + local m, _ = code:gsub("^%s*(.-)%s*$", "%1") + table.insert(code_blocks, m) + return "\1CODE:"..#code_blocks.."\1" end) -- replace `[url=https://example.com]Example[/url] tags text = text:gsub("%[url=([^%]]+)%](.-)%[/url%]", function(url, label) - return ''..escape_html(label)..'' + return ''..label..'' end) -- replace `[url]https://example.com[/url] tags text = text:gsub("%[url%]([^%]]+)%[/url%]", function(url) - return ''..escape_html(url)..'' + return ''..url..'' end) -- bold, italics, strikethrough @@ -34,13 +34,16 @@ function babycode.to_html(s, escape_html) -- replace loose links text = text:gsub("(https?://[%w-_%.%?%.:/%+=&~%@#%%]+[%w-/])", function(url) if not text:find(']*>'..url..'') then - return ''..escape_html(url)..'' + return ''..url..'' end return url end) + -- rule + text = text:gsub("\n+%-%-%-", "
") + -- normalize newlines, replace them with
- text = text:gsub("\r?\n\r?\n+", "
"):gsub("\r?\n", "
") + text = text:gsub("\r?\n\r?\n+", "
")--:gsub("\r?\n", "
") -- replace code block placeholders back with their original contents text = text:gsub("\1CODE:(%d+)\1", function(n)