如何将段落合并为lua中的单行?



如何在lua中获取文本块并将其合并为一行,类似于html或markdown忽略换行符的方式:

  • 删除单行换行符
  • 当两个或多个连续
  • 时保持换行
  • 始终保持标题换行

我的标题很容易识别:它们的行从不以字母字符开头。

我已经找出了这个模式:(n%a.-)n(%S)。但是它不会合并所有的换行符。(我使用>_<是为了方便看到合并的行。)

(注:"procedural"有尾随空格)

>>> 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)"有点工作,但它删除了双换行符,我不知道如何确保我的段落标题保留空白。

>>> 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文档处理器脚本输出vim帮助文件。我打算用lume。换行:在合并后换行。

可以使用

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

这个模式匹配:

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

相关内容

  • 没有找到相关文章

最新更新