greatly improved newline handling

This commit is contained in:
Izaya 2018-06-21 00:02:42 +10:00
parent 711b6f2157
commit bb2adacba3

View File

@ -10,7 +10,9 @@ function md.parse(md)
end end
newpart() newpart()
local lm = false local lm = false
local cc = 0
for c in md:gmatch(".") do for c in md:gmatch(".") do
cc = cc + 1
if c == "[" then if c == "[" then
newpart() newpart()
elseif c == "(" and lc == "]" then elseif c == "(" and lc == "]" then
@ -23,9 +25,28 @@ function md.parse(md)
it[#it].addrid = #it.l it[#it].addrid = #it.l
newpart() newpart()
elseif c == "\n" and lc == "\n" then elseif c == "\n" and lc == "\n" then
if it[#it-1].content == "\n" then
table.remove(it,#it-1)
end
newpart() newpart()
it[#it].content = "\n\n" it[#it].content = "\n\n"
newpart() newpart()
elseif c == "\n" then
local line = md:sub(1,cc):match(".+\n(.+)") or it[#it].content
print(line:sub(1,1),line)
if line:sub(1,1) == "-" then
print("entry")
newpart()
it[#it].content = "\n"
newpart()
elseif line:sub(1,1) == "#" then
print("title")
newpart()
it[#it].content = "\n"
newpart()
else
it[#it].content = it[#it].content .. c
end
else else
if not lm then if not lm then
it[#it].content = it[#it].content .. c it[#it].content = it[#it].content .. c
@ -42,8 +63,8 @@ end
function md.reflow(text,len) function md.reflow(text,len)
local words, lines, links = {}, {""}, {} local words, lines, links = {}, {""}, {}
for k,v in ipairs(md.parse(text)) do for k,v in ipairs(md.parse(text)) do
if v.content == "\n\n" then if v.content == "\n\n" or v.content == "\n" then
words[#words+1] = {"\n\n"} words[#words+1] = {v.content}
elseif not v.address then elseif not v.address then
for word in v.content:gmatch("%S+") do for word in v.content:gmatch("%S+") do
words[#words+1] = {word} words[#words+1] = {word}
@ -59,6 +80,8 @@ function md.reflow(text,len)
end end
links[#links+1] = {#lines, lines[#lines]:len()+1, v[1], v[2]} links[#links+1] = {#lines, lines[#lines]:len()+1, v[1], v[2]}
lines[#lines] = lines[#lines] .. "<"..v[1].."> " lines[#lines] = lines[#lines] .. "<"..v[1].."> "
elseif v[1] == "\n" then
lines[#lines+1] = ""
elseif v[1] == "\n\n" then elseif v[1] == "\n\n" then
lines[#lines+1] = "" lines[#lines+1] = ""
lines[#lines+1] = "" lines[#lines+1] = ""