function html.picofmt(post_tbl)
local email = post_tbl.Email
if email and (email == "nofo" or email:match("^nofo ") or email:match(" nofo$") or email:match(" nofo ")) then
local s = html.striphtml(post_tbl.Comment)
:gsub("[\1-\8\11-\31\127]", "")
:gsub("^\n+", "")
:gsub("%s+$", "")
:gsub("\n", "
")
return s
end
local function handle_refs(number, append)
number = tonumber(number)
local ref_post_tbl = pico.post.tbl(post_tbl.Board, number, true)
if ref_post_tbl then
return ("\2\2%d%s"):format(
ref_post_tbl.Board, ref_post_tbl.Parent or number, number, number, append)
else
return ("\2\2%d%s"):format(number, append)
end
end
local function handle_xbrefs(board, number, append)
if number == "" then
return ("\2\2\2/%s/%s"):format(board, board, append)
else
number = tonumber(number)
end
local ref_post_tbl = pico.post.tbl(board, number, true)
if ref_post_tbl then
return ("\2\2\2/%s/%d%s"):format(
board, ref_post_tbl.Parent or number, number, board, number, append)
else
return ("\2\2\2/%s/%d%s"):format(board, number, append)
end
end
local function handle_url(prev, url)
local balance_tbl = {
["("] = ")",
["\1"] = "\2",
["{"] = "}",
["["] = "]",
["\3"] = "\3",
["\4"] = "\4",
}
local balance = balance_tbl[prev]
local append = ""
if balance then
local first, second = (prev .. url):match("^(%b" .. prev .. balance .. ")(.-)$")
if first then
url = first:sub(2, -2)
append = balance .. second
end
else
local last = url:match("[!,%.:;%?]$")
if last then
url = url:sub(1, -2)
append = last
end
end
return ("%s%s%s"):format(prev, url, url, append)
end
local blocks = {}
local iblocks = {}
local function handle_code(b, c, t, e)
return function(block)
b[#b + 1] = t .. block .. e
return c
end
end
local function insert_escaped(t)
return function()
if #t > 0 then
return table.remove(t, 1)
end
return ""
end
end
local punct = "!\4#%$%%&\3%(%)%*%+,%-%./:;\1=\2%?@%[\\%]%^_`{|}~"
local s = ("\n" .. post_tbl.Comment .. "\n")
:gsub("[\1-\8\11-\31\127]", "")
:gsub("&", "&")
:gsub("<", "\1")
:gsub(">", "\2")
:gsub("'", "\3")
:gsub("\"", "\4")
:gsub("```\n*(.-)\n*```", handle_code(blocks, "\5", "", "
"))
:gsub("([^\n])\5", "%1\n\5")
:gsub("\5([^\n])", "\5\n%1")
:gsub("`([^\n]-)`", handle_code(iblocks, "\6", "", ""))
:gsub("\2\2\2/([%l%d]+)/(%d-)([%s" .. punct .. "])", handle_xbrefs)
:gsub("\2\2(%d+)([%s" .. punct .. "])", handle_refs)
:gsub("\3\3\3([^\n]-)\3\3\3", "%1")
:gsub("\3\3([^\n]-)\3\3", "%1")
:gsub("~~([^\n]-)~~", "%1")
:gsub("__([^\n]-)__", "%1")
:gsub("==([^\n]-)==", "%1")
:gsub("%*%*([^\n]-)%*%*", "%1")
:gsub("%(%(%([^\n]-%)%)%)", "%1")
:gsub("\n(\2[^\n]*)", "\n%1")
:gsub("\n(\1[^\n]*)", "\n%1")
:gsub("(.)(https?://[%w" .. punct .. "]+)", handle_url)
:gsub("\6", insert_escaped(iblocks))
:gsub("\5", insert_escaped(blocks))
:gsub("\4", """)
:gsub("\3", "'")
:gsub("\2", ">")
:gsub("\1", "<")
:gsub("^\n+", "")
:gsub("%s+$", "")
:gsub("\n", "
")
return s
end