From b25fee071fa554ff0320bfc1280c14140b16ca13 Mon Sep 17 00:00:00 2001 From: rxi Date: Tue, 5 May 2015 19:12:41 +0100 Subject: [PATCH] Changed lume.wordwrap() to better retain existing whitespace --- lume.lua | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/lume.lua b/lume.lua index 5d8e45c..cfc4d57 100644 --- a/lume.lua +++ b/lume.lua @@ -565,20 +565,26 @@ function lume.wordwrap(str, limit) check = limit end local rtn = {} - for j, line in ipairs(lume.split(str, "\n")) do - local str - for i, word in ipairs(lume.split(line)) do - local s = (str and (str .. " ") or "") .. word - if check(s) then - rtn[#rtn + 1] = str - str = word + local line = "" + for word, spaces in str:gmatch("(%S+)(%s*)") do + local str = line .. word + if check(str) then + table.insert(rtn, line .. "\n") + line = word + else + line = str + end + for c in spaces:gmatch(".") do + if c == "\n" then + table.insert(rtn, line .. "\n") + line = "" else - str = s + line = line .. c end end - rtn[#rtn + 1] = str end - return table.concat(rtn, "\n") + table.insert(rtn, line) + return table.concat(rtn) end