dapengde 你的这个假设条件不太严谨,但基本上够用了。写了个函数修正双引号和单引号(以及任何成对出现的字符):
fix_quotes = function(file, quotes = c('“', '”')) {
x = xfun::read_utf8(file)
r = paste(c('[', quotes, ']'), collapse = '')
k = gregexpr(r, x)
m = regmatches(x, k)
for (i in seq_along(m)) {
n = length(m[[i]])
if (n %% 2 != 0) {
warning('引号与回引号不匹配:\n', '文件:', file, '(行:', i, ')\n文本:', x[i], '\n')
next
}
m[[i]] = rep(quotes, length.out = n)
}
x2 = x
regmatches(x, k) = m
if (!identical(x, x2)) xfun::write_utf8(x, file)
}
files = list.files('.', '[.]md$', recursive = TRUE, full.names = TRUE)
for (f in files) {
fix_quotes(f)
fix_quotes(f, quotes = c('‘', '’'))
}
跑一遍代码把所有文件都处理一遍,因为有版本控制在,所以容易看出哪些是正确的修正,哪些是误处理。只发现一个特例,文本形如 这里有“引号^[里面有个“脚注”]”
,这个不能按奇偶位置修正。除此之外,其它修正都是对的(还发现了若干年前的一些引号问题)。