Skip to content

Commit

Permalink
feat: 支持中英双语的参考文献 #3
Browse files Browse the repository at this point in the history
  • Loading branch information
OrangeX4 committed Apr 8, 2024
1 parent aee420f commit eec4dc5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 19 deletions.
9 changes: 1 addition & 8 deletions template/thesis.typ
Original file line number Diff line number Diff line change
Expand Up @@ -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 模板。
Expand Down
67 changes: 56 additions & 11 deletions utils/bilingual-bibliography.typ
Original file line number Diff line number Diff line change
@@ -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)

This comment has been minimized.

Copy link
@csimide

csimide Apr 8, 2024

Contributor

这一行我忘记删了,已经用不上 state

This comment has been minimized.

Copy link
@OrangeX4

OrangeX4 Apr 8, 2024

Author Member

感谢提醒

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,
)
}

0 comments on commit eec4dc5

Please sign in to comment.