fix nested quotes and alter linebreak parsing

This commit is contained in:
Lera Elvoé 2025-05-26 02:44:11 +03:00
parent 6eee661b58
commit 94f58fef73
Signed by: yagich
SSH Key Fingerprint: SHA256:6xjGb6uA7lAVcULa7byPEN//rQ0wPoG+UzYVMfZnbvc
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)

View File

@ -73,4 +73,22 @@ return {
schema.add_column("users", "signature_original_markup", types.text{default = ""})
schema.add_column("users", "signature_rendered", types.text{default = ""})
end,
[11] = function ()
local render = require("lib.babycode").to_html
local html_escape = require("lapis.html").escape
local phs = db.query("SELECT * from post_history")
local users = db.query("SELECT * from users")
db.query("BEGIN")
for _, post_history in ipairs(phs) do
db.query("UPDATE post_history SET content = ? WHERE id = ?", render(post_history.original_markup, html_escape), post_history.id)
end
for _, user in ipairs(users) do
db.query("UPDATE users SET signature_rendered = ? WHERE id = ?", render(user.signature_original_markup, html_escape), user.id)
end
db.query("COMMIT")
end,
}

View File

@ -64,14 +64,11 @@
</div>
<div class="post-content">
<% if not edit then %>
<div class="post-inner">
<%- post.content %>
</div>
<div class="post-inner"><%- post.content %></div>
<% if render_sig and #post.signature_rendered > 0 then %>
<div class="signature-container">
<hr>
<%- post.signature_rendered %>
</div>
<%- post.signature_rendered %></div>
<% end %>
<% else %>
<% render("views.common.babycode-editor", {