我一直在研究一种格式化程序,该格式化程序将使用一个长字符串并将其格式化为在特定字符限制内的单词处折断的一系列行。例如,他吃了每8个字符就折断的面包,将返回以下内容:
He eats
the
bread
这是因为“他吃”包含7个字符,“面包”包含9个字符,因此它必须在“ the”处断开并继续“ bread”。
由于之前其他成员的帮助,脚本本身一直运行良好。但是,现在我面临新的挑战。
我正在利用自由输入框输入一系列行。如果有人熟悉它,它适用于一个名为MUSHclient的程序。该utils.editbox打开一个编辑的文本框,并允许自由形式,并返回结果作为一个字符串。例如:
result = utils.editbox("What are you typing?")
将类型化的响应保存result
为string
类型。我想适当地插入换行符,但是我不知道该怎么做。如果I print(result)
,则返回类似以下内容:
This is the result of three separate paragraphs.
As you can see, there is a line break between each paragraph.
So the result contains line breaks, or control characters.
现在,我使用以下代码设法推断了单独的行,但没有推断它们之间的换行符:
for line in result:gmatch("[^%c]+%c?") do
Send(note_wrap(line))
end
如何推断换行符和文本内容?
编辑以供参考,note_wrap
功能如下:
function note_wrap(str, limit, indent, indent1)
indent = indent or ""
indent1 = indent1 or indent
limit = limit or 79
local here = 1-#indent1
local last_color = ''
return indent1..str:gsub("(%s+)()(%S+)()",
function(sp, st, word, fi)
local delta = 0
local color_before_current_word = last_color
word:gsub('()@([@%a])',
function(pos, c)
if c == '@' then
delta = delta + 1
elseif c == 'x' then
delta = delta + 5
last_color = word:sub(pos, pos+4)
else
delta = delta + 2
last_color = word:sub(pos, pos+1)
end
end)
here = here + delta
if fi-here > limit then
here = st - #indent + delta
return "\n"..indent..color_before_current_word..word
end
end)
end
编写此代码的原因是能够吃@rHe吃@ x123the面包,忽略颜色代码(由@ <letter>或@x <digits>表示)并返回上述结果:
He eats
the
bread
但插入颜色代码,然后返回:
@rHe eats
@x123the
bread
如果可以修改它以识别新行并放入实际的新行,那就太好了。
编辑2尝试过Paul的解决方案,但它并没有满足我的需要。我可能还不够清楚,所以我将在此编辑中尝试将其清除。
当我在自由格式框中键入内容时,我希望信息按原样显示,但是要正确设置格式才能正确显示并保持输入的换行符。例如,如果我在自由格式框中输入以下内容:
Mary had a little lamb, little lamb, little lamb
Mary had a little lamb
It's fleece was white as snow
使用保罗的代码,并将字符串设置为79,我得到:
Mary had a little lamb, little lamb, little lamb
Mary had a little lamb
It's fleece was white as snow
那不是我想要的。我希望它按原样返回,并在必要时换行。因此,如果我限制为20个字符,它将返回:
Mary had a little
lamb, little lamb,
little lamb
Mary had a little
lamb
It's fleece was
white as snow
如果我添加了手动换行符,我希望它尊重这些换行符,因此,如果我在第一行之后两次按回车键,那么在第一行下也会有一个换行符。例如,如果我在自由格式框中写这篇文章,我希望它尊重每个新段落以及每个适当的换行符。我希望这可以清除一切。
我在代码中使用了以下内容(假设这\n
是行分隔符):
local function formatUpToX(s, x, indent)
x = x or 79
indent = indent or ""
local t = {""}
local function cleanse(s) return s:gsub("@x%d%d%d",""):gsub("@r","") end
for prefix, word, suffix, newline in s:gmatch("([ \t]*)(%S*)([ \t]*)(\n?)") do
if #(cleanse(t[#t])) + #prefix + #cleanse(word) > x and #t > 0 then
table.insert(t, word..suffix) -- add new element
else -- add to the last element
t[#t] = t[#t]..prefix..word..suffix
end
if #newline > 0 then table.insert(t, "") end
end
return indent..table.concat(t, "\n"..indent)
end
print(formatUpToX(result, 20))
print(formatUpToX("@rHe eats @x123the bread", 8, " "))
该cleanse
函数删除了所有需要包含在字符串中的标记,但不需要计入限制。
对于您的示例,我得到以下输出(20
用作第一个片段和8
第二个片段的限制):
This is the result
of three separate
paragraphs.
As you can see,
there is a line
break between each
paragraph.
So the result
contains line
breaks, or control
characters.
@rHe eats
@x123the
bread
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句