首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在lua中将段落合并成一行?

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

Stack Overflow用户
提问于 2021-09-08 19:10:01
回答 1查看 167关注 0票数 2

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

  • 删除单行断线
  • 当两个或两个以上连续的时候保持换行。
  • 始终保持标题行中断。

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

我已经找出了这个模式:(\n%a.-)\n(%S)。但它并没有合并所有的换行符。(我使用>_<可以很容易地看到合并的行。)

(注:“过程性”有尾随空间。)

代码语言:javascript
运行
复制
>>> 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

“构造”和其他许多内容没有合并到前面的行中。

更天真的"(.)\n(%S)"有点工作,但它删除了双行中断,我不知道如何确保我的章节标题保持他们的空格。

代码语言:javascript
运行
复制
>>> 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.wordwrap包装行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-09 19:55:38

您可以使用

代码语言:javascript
运行
复制
(%S)[^%S\n]*\n([%a()])

此模式匹配:

  • (%S) -(捕获到组1,%1)任何非空白字符
  • [^%S\n]* -匹配(不捕获)零或多个字符,而不是非空白和换行符(也就是说,它是没有\n%s模式)。
  • \n -一个新行字符
  • ([%a()]) -(捕捉到第2组,%2)任何字母,()字符。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69108487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档