将以某种语法编写的文本转换为另一种指定语法
Convert text written in some syntax to another specified syntax
我想将用 Markdown 扩展 Fountain 编写的剧本转换为 LaTeX(更具体地说,我自己的 LaTeX 剧本模板)。为此,我需要转换以下列格式给出的文本
Some stage directions.
CHARACTER A:
Text the character is saying.
CHARACTER B:
Text the other character is saying.
Some other stage direction.
CHARACTER B:
Some more text the other character is saying.
至
\textit{Some stage directions.}
\dialog{Character A}{Text the character is saying.}
\dialog{Character B}{Text the other character is saying.}
\textit{Some other stage direction.}
\dialog{Character B}{Some more text the other character is saying.}
我想避免从头开始编写这样的程序。是否有工具或包(例如 Python)允许进行这种相当基本的重新格式化?问题可能在于,舞台方向在文本中分布不均匀,即在一个角色说了些什么之后,可能会有也可能不会有舞台指导。
假设块由双换行符分隔,这很容易使用正则表达式实现:
输入:
t='''Some stage directions.
CHARACTER A:
Text the character is saying.
CHARACTER B:
Text the other character is saying.
Some other stage direction.
CHARACTER B:
Some more text the other character is saying.'''
代码:
import re
out = '\n\n'.join(fr'\dialog{{{m.group(1)}}}{{{m.group(2)}}}'
if (m:=re.match('([^\n]+):\n(.*)', s))
else fr'\textit{{{s}}}'
for s in re.split('\n\n', t))
print(out)
输出:
\textit{Some stage directions.}
\dialog{CHARACTER A}{Text the character is saying.}
\dialog{CHARACTER B}{Text the other character is saying.}
\textit{Some other stage direction.}
\dialog{CHARACTER B}{Some more text the other character is saying.}
我想将用 Markdown 扩展 Fountain 编写的剧本转换为 LaTeX(更具体地说,我自己的 LaTeX 剧本模板)。为此,我需要转换以下列格式给出的文本
Some stage directions.
CHARACTER A:
Text the character is saying.
CHARACTER B:
Text the other character is saying.
Some other stage direction.
CHARACTER B:
Some more text the other character is saying.
至
\textit{Some stage directions.}
\dialog{Character A}{Text the character is saying.}
\dialog{Character B}{Text the other character is saying.}
\textit{Some other stage direction.}
\dialog{Character B}{Some more text the other character is saying.}
我想避免从头开始编写这样的程序。是否有工具或包(例如 Python)允许进行这种相当基本的重新格式化?问题可能在于,舞台方向在文本中分布不均匀,即在一个角色说了些什么之后,可能会有也可能不会有舞台指导。
假设块由双换行符分隔,这很容易使用正则表达式实现:
输入:
t='''Some stage directions.
CHARACTER A:
Text the character is saying.
CHARACTER B:
Text the other character is saying.
Some other stage direction.
CHARACTER B:
Some more text the other character is saying.'''
代码:
import re
out = '\n\n'.join(fr'\dialog{{{m.group(1)}}}{{{m.group(2)}}}'
if (m:=re.match('([^\n]+):\n(.*)', s))
else fr'\textit{{{s}}}'
for s in re.split('\n\n', t))
print(out)
输出:
\textit{Some stage directions.}
\dialog{CHARACTER A}{Text the character is saying.}
\dialog{CHARACTER B}{Text the other character is saying.}
\textit{Some other stage direction.}
\dialog{CHARACTER B}{Some more text the other character is saying.}