From eec4dc51ba8e22323b50f0eed000d6ac5a336248 Mon Sep 17 00:00:00 2001 From: OrangeX4 <318483724@qq.com> Date: Mon, 8 Apr 2024 16:03:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E4=B8=AD=E8=8B=B1?= =?UTF-8?q?=E5=8F=8C=E8=AF=AD=E7=9A=84=E5=8F=82=E8=80=83=E6=96=87=E7=8C=AE?= =?UTF-8?q?=20#3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- template/thesis.typ | 9 +---- utils/bilingual-bibliography.typ | 67 ++++++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 19 deletions(-) diff --git a/template/thesis.typ b/template/thesis.typ index 1c1fc4b..6ee880a 100644 --- a/template/thesis.typ +++ b/template/thesis.typ @@ -196,16 +196,9 @@ $ F_n = floor(1 / sqrt(5) phi.alt^n) $ } // 中英双语参考文献 -// Demo 版本,可能存在 bug,如果失效请使用 Typst 默认的参考文献 +// Demo 版本,默认使用 gb-7714-2015-numeric 样式 #bilingual-bibliography() -// 默认参考文献 -// #bibliography( -// title: "参考文献", -// style: "gb-7714-2015-numeric", -// "ref.bib", -// ) - // 致谢 #acknowledgement[ 感谢 NJU-LUG,感谢 NJUThesis LaTeX 模板。 diff --git a/utils/bilingual-bibliography.typ b/utils/bilingual-bibliography.typ index a461f81..ac1d895 100644 --- a/utils/bilingual-bibliography.typ +++ b/utils/bilingual-bibliography.typ @@ -1,34 +1,79 @@ +// Author: csimide, OrangeX4 // Tested only on GB-7714-2015-Numeric #let bilingual-bibliography( bibliography: none, title: "参考文献", full: false, + style: "gb-7714-2015-numeric", mapping: (:), ) = { assert(bibliography != none, message: "请传入带有 source 的 bibliography 函数。") + // Please fill in the remaining mapping table here mapping = ( "等": "et al", "卷": "Vol.", + "册": "Bk.", + "译": "tran" ) + mapping - let using-chinese = state("using-chinese-in-bibliography", false) - show regex("^\[\d+\]$"): it => { - using-chinese.update(false) - it + + let to-string(content) = { + if content.has("text") { + content.text + } else if content.has("children") { + content.children.map(to-string).join("") + } else if content.has("child") { + to-string(content.child) + } else if content.has("body") { + to-string(content.body) + } else if content == [ ] { + " " + } } - show regex("\p{sc=Hani}+"): it => { - context if using-chinese.get() { - it + + let using-chinese = state("using-chinese-in-bibliography", false) + show grid.cell.where(x:1): it => { + // 后续的操作是对 string 进行的。 + let ittext = to-string(it) + // 判断是否为中文文献:去除特定词组后,仍有至少两个连续汉字。 + let pureittext = ittext.replace(regex("[等卷册和版本章期页篇译间者(不详)]"), "") + if pureittext.find(regex("\p{sc=Hani}+")) != none { + ittext } else { - mapping.at(it.text, default: it) + // 不是中文文献,进行替换 + // 第xxx卷、第xxx册的情况:变为 Vol. XXX 或 Bk. XXX。 + let reptext = ittext + reptext = reptext.replace(regex("(第\s?)?\d+\s?[卷册]"), itt => { + if itt.text.contains("卷") {"Vol. "} else {"Bk. "} + itt.text.find(regex("\d+")) + }) + // 第xxx版/第xxx本的情况:变为 1st ed 格式。 + reptext = reptext.replace(regex("(第\s?)?\d+\s?[版本]"), itt => { + let num = itt.text.find(regex("\d+")) + num + if num.clusters().len() == 2 and num.clusters().first() == "1" { + "th" + } else { + ( + "1": "st", + "2": "nd", + "3": "rd" + ).at(num.clusters().last(), default: "th") + } + " ed" + }) + // 其他情况:直接替换 + reptext = reptext.replace(regex("\p{sc=Hani}+"), itt => { + mapping.at(itt.text, default: itt) + }) + reptext } - using-chinese.update(true) } - set text(lang: "zh") + set text(lang: "zh") bibliography( title: title, full: full, - style: "gb-7714-2015-numeric", + style: style, ) } \ No newline at end of file