首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用Pandoc过滤器代替中文引文中的特定英语单词

用Pandoc过滤器代替中文引文中的特定英语单词
EN

Stack Overflow用户
提问于 2022-11-18 12:35:00
回答 1查看 68关注 0票数 1

在写中文论文时,可以引用中文和英文的论文。然而,样式略有不同。例子如下:

代码语言:javascript
复制
Cite an English article (Smith et al. 2022), and cite a Chinese article (张三 等 2018).

换句话说,对于多位作者的论文,et al.用于英语论文,而用于中文论文。考虑到引文风格语言不能处理多种语言,我会向Lua求助。

一个名为test.md的Markdown文件作为一个示例:

代码语言:javascript
复制
Cite an English article [@makarchev2022], and cite a Chinese article [@luohongyun2018].

然后运行以下命令:

代码语言:javascript
复制
pandoc -C -t native test.md

以及主体的产出:

代码语言:javascript
复制
[ Para
    [ Str "Cite"
    , Space
    , Str "an"
    , Space
    , Str "English"
    , Space
    , Str "article"
    , Space
    , Cite
        [ Citation
            { citationId = "makarchev2022"
            , citationPrefix = []
            , citationSuffix = []
            , citationMode = NormalCitation
            , citationNoteNum = 1
            , citationHash = 0
            }
        ]
        [ Str "(Makarchev"
        , Space
        , Str "et"
        , Space
        , Str "al."
        , Space
        , Str "2022)"
        ]
    , Str ","
    , Space
    , Str "and"
    , Space
    , Str "cite"
    , Space
    , Str "a"
    , Space
    , Str "Chinese"
    , Space
    , Str "article"
    , Space
    , Cite
        [ Citation
            { citationId = "luohongyun2018"
            , citationPrefix = []
            , citationSuffix = []
            , citationMode = NormalCitation
            , citationNoteNum = 2
            , citationHash = 0
            }
        ]
        [ Str "(\32599\32418\20113"
        , Space
        , Str "et"
        , Space
        , Str "al."
        , Space
        , Str "2018)"
        ]
    , Str "."
    ]

因为@luohongyun2018是一个中文书目,所以我想替换它后面的最后一个英文et al.,即:

代码语言:javascript
复制
, Str "et"
, Space
, Str "al."

一个中文单词

代码语言:javascript
复制
, Str "\31561"

有可能通过Lua过滤器吗?按照Lua滤波器页面中的示例,我尝试过,但没有自己完成。

如有任何建议,将不胜感激。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-20 08:49:41

下面的过滤器做两件事:它检查引文是否包含汉字,如果包含,则继续替换et al.

对汉字的测试有点脆弱;使用标准Lua库的utf8.codepoint函数可以使它更加健壮。

代码语言:javascript
复制
function Cite (cite)
  return cite:walk{
    Inlines = function (inlines)
      local has_cjk = false
      inlines:walk {
        Str = function (s)
          has_cjk = has_cjk or
            pandoc.layout.real_length(s.text) > pandoc.text.len(s.text)
        end
      }
      -- do nothing if this does not contain wide chars.
      if not has_cjk then
        return nil
      end

      local i = 1
      local result = pandoc.Inlines{}
      while i <= #inlines do
        if i + 2 <= #inlines and
          inlines[i].text == 'et' and
          inlines[i+1].t == 'Space' and
          inlines[i+2].text == 'al.' then
          result:insert(pandoc.Str '等')
          i = i + 3
        else
          result:insert(inlines[i])
          i = i + 1
        end
      end
      return result
    end
  }
end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74489784

复制
相关文章

相似问题

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