如何在 lua 中将段落合并为一行?

How to merge paragraphs into a single line in lua?

如何将 lua 中的一段文本合并成一行,类似于 html 或 markdown 忽略换行符的方式:

我的 header 很容易识别:他们的行从不以字母字符开头。

我发现了这个模式:(\n%a.-)\n(%S)。但它不会合并所有换行符。 (我使用 >_< 以便于查看合并的行。)

(注意:“程序”有尾随 space。)

>>> t = [[
                                                               *lrv-section-1*
1 -- Introduction~

Lua is a powerful, efficient, lightweight, embeddable scripting language.
It supports procedural programming,
object-oriented programming, functional programming,
data-driven programming, and data description.

Lua combines simple procedural 
syntax with powerful data description
constructs based on associative arrays and extensible semantics.
Lua is dynamically typed,
runs by interpreting bytecode with a register-based
virtual machine,
and has automatic memory management with
incremental garbage collection,
making it ideal for configuration, scripting,
and rapid prototyping.

]]

>>> print(t:gsub("(\n%a.-)\n(%S)", "%1>_<%2"))
[[
                                                               *lrv-section-1*
1 -- Introduction~

Lua is a powerful, efficient, lightweight, embeddable scripting language.>_<It supports procedural programming,
object-oriented programming, functional programming,>_<data-driven programming, and data description.

Lua combines simple procedural >_<syntax with powerful data description
constructs based on associative arrays and extensible semantics.>_<Lua is dynamically typed,
runs by interpreting bytecode with a register-based>_<virtual machine,
and has automatic memory management with>_<incremental garbage collection,
making it ideal for configuration, scripting,>_<and rapid prototyping.

 7

“constructs”和许多其他未合并到上一行。

更天真的 "(.)\n(%S)" 有点管用,但它删除了双换行符,我不确定如何确保我的 章节标题保持白色space.

>>> print(t:gsub("(.)\n(%S)", "%1>_<%2"))
                                                               *lrv-section-1*>_<1 -- Introduction~
>_<Lua is a powerful, efficient, lightweight, embeddable scripting language.>_<It supports procedural programming,>_<object-oriented programming, functional programming,>_<data-driven programming, and data description.
>_<Lua combines simple procedural >_<syntax with powerful data description>_<constructs based on associative arrays and extensible semantics.>_<Lua is dynamically typed,>_<runs by interpreting bytecode with a register-based>_<virtual machine,>_<and has automatic memory management with>_<incremental garbage collection,>_<making it ideal for configuration, scripting,>_<and rapid prototyping.

我正在尝试调整 lua 的 2html document processor script to output a vim help file. I plan to use lume.wordwrap 以在合并后换行。

您可以使用

(%S)[^%S\n]*\n([%a()])

此模式匹配:

  • (%S) -(捕获到第 1 组,%1)任何非空白字符
  • [^%S\n]* - 匹配(不捕获)除非空白和换行符以外的零个或多个字符(即,它是没有 \n%s 模式)
  • \n - 换行符
  • ([%a()]) -(捕获到第 2 组,%2)任何字母,() 个字符。