包含文件并缩进每一行

les*_*ana 5 m4

我想包含一个文件并缩进所有行。我希望它成为 Markdown 文档中的代码块。

基本上我想要这样的东西:

text
include(somefile)
text
Run Code Online (Sandbox Code Playgroud)

得到这个输出:

text
    content
    from
    somefile
text
Run Code Online (Sandbox Code Playgroud)

我翻阅了说明书,发现patsubst。我还发现了这个问题和答案:How to indent a block of text in an m4 macro

这适用于不包含逗号的文件:

$ cat textnocomma 
some text.
with sentences.
and no commas.

$ cat patsubstincludenocomma
text
patsubst(include(textnocomma), `^', `    ')
text

$ m4 patsubstincludenocomma
text
    some text.
    with sentences.
    and no commas.

text
Run Code Online (Sandbox Code Playgroud)

但是当我的include文件包含逗号时:

$ cat textcomma 
some text.
with sentences.
and, commas.

$ cat patsubstincludecomma
text
patsubst(include(textcomma), `^', `    ')
text

$ m4 patsubstincludecomma
text
m4:patsubstincludecommanoquote:3: Warning: excess arguments to builtin `patsubst' ignored
some text.
with sentences.
and
text
Run Code Online (Sandbox Code Playgroud)

问题似乎是 m4 进行宏扩展的幼稚方式。包含的文本中的逗号被解释为patsubst宏的语法。解决方案(应该)很简单:引用包含的文本。

但如果我引用,include那么只有第一行缩进:

$ cat patsubstincludecommaquote 
text
patsubst(`include(textcomma)', `^', `    ')
text

$ m4 patsubstincludecommaquote
text
    some text.
with sentences.
and, commas.

text
Run Code Online (Sandbox Code Playgroud)

我尝试了引用和文字换行符的不同组合,而不是正则表达式换行符。但到目前为止我得到的只是excess arguments错误消息或只是缩进的第一行。

如何包含带有逗号或其他 m4 语法的文本,并在 m4 中缩进?

uzs*_*olt 3

为什么不使用外部命令呢?

esyscmd(`sed "s,^,    ," textcomma')
Run Code Online (Sandbox Code Playgroud)