Skip to content

Commit

Permalink
fix: 修复读取配置信息失效的异常, closed #236 closed #214 closed #194
Browse files Browse the repository at this point in the history
  • Loading branch information
l0o0 committed Dec 20, 2023
1 parent 8b4e137 commit 48d59a4
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 90 deletions.
8 changes: 5 additions & 3 deletions src/modules/cnki.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { config } from "../../package.json";
import { getHTMLDoc, getHTMLText, string2HTML } from "../utils/http";
import { getString } from "../utils/locale";
import { getPref } from "../utils/prefs";
import { getItems, isCNKIPDF } from "../utils/tools";
import { showPop } from "../utils/window";
import { addBookmarkItem } from "./bookmark";
Expand Down Expand Up @@ -494,7 +495,7 @@ export async function fixItem(newItems: Zotero.Item[], targetData: any) {
Zotero.Items.erase(newItem.getNotes());
}
// 是否处理中文姓名. For Chinese name
if (Zotero.Prefs.get("jasminum.zhnamesplit")) {
if (getPref("zhnamesplit")) {
creators = newItem.getCreators() as MyCreator[];
for (let i = 0; i < creators.length; i++) {
const creator = creators[i];
Expand Down Expand Up @@ -575,6 +576,7 @@ export async function fixItem(newItems: Zotero.Item[], targetData: any) {
newItem.setField("extra", extraString);

// Keep tags according global config.
// This is a Zotero pref
if (Zotero.Prefs.get("automaticTags") === false) {
newItem.setTags([]);
}
Expand Down Expand Up @@ -673,7 +675,7 @@ export async function searchCNKIMetadata(items: Zotero.Item[]) {
item.parentID = newItem.id;
// Use Zotfile to rename file
if (
Zotero.Prefs.get("jasminum.rename") &&
getPref("rename") &&
typeof Zotero.ZotFile != "undefined"
) {
Zotero.ZotFile.renameSelectedAttachments();
Expand All @@ -682,7 +684,7 @@ export async function searchCNKIMetadata(items: Zotero.Item[]) {
await item.saveTx();
await newItem.saveTx();
// Add bookmark after PDF attaching to new item
if (Zotero.Prefs.get("jasminum.autobookmark") && isCNKIPDF(item)) {
if (getPref("autobookmark") && isCNKIPDF(item)) {
await addBookmarkItem(item);
}
} else {
Expand Down
174 changes: 87 additions & 87 deletions src/modules/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@ import { getItems } from "../utils/tools";

// Functions used in menu
export async function concatName(items: Zotero.Item[]) {
const isSplitEnName = getPref("ennamesplit");
for (const item of items) {
const creators = item.getCreators();
for (let i = 0; i < creators.length; i++) {
const creator = creators[i];
creator.fieldMode = 1;
if (
// English Name
creator.lastName!.search(/[A-Za-z]/) !== -1 ||
creator.lastName!.search(/[A-Za-z]/) !== -1
) {
// 如果不拆分/合并英文名,则跳过
if (!isSplitEnName) continue;
creator.lastName = creator.firstName + " " + creator.lastName;
} else {
// For Chinese Name
creator.lastName = creator.lastName! + creator.firstName;
}
creator.firstName = "";
creator.fieldMode = 1; // 0: two-field, 1: one-field (with empty first name)
creators[i] = creator;
}
if (creators != item.getCreators()) {
item.setCreators(creators);
await item.saveTx();
}
const isSplitEnName = getPref("ennamesplit");
for (const item of items) {
const creators = item.getCreators();
for (let i = 0; i < creators.length; i++) {
const creator = creators[i];
creator.fieldMode = 1;
if (
// English Name
creator.lastName!.search(/[A-Za-z]/) !== -1 ||
creator.lastName!.search(/[A-Za-z]/) !== -1
) {
// 如果不拆分/合并英文名,则跳过
if (!isSplitEnName) continue;
creator.lastName = creator.firstName + " " + creator.lastName;
} else {
// For Chinese Name
creator.lastName = creator.lastName! + creator.firstName;
}
creator.firstName = "";
creator.fieldMode = 1; // 0: two-field, 1: one-field (with empty first name)
creators[i] = creator;
}
if (creators != item.getCreators()) {
item.setCreators(creators);
await item.saveTx();
}
}
}

export async function concatNameMenu(type: "items" | "collection") {
Expand All @@ -39,38 +39,38 @@ export async function concatNameMenu(type: "items" | "collection") {
}

export async function splitName(items: Zotero.Item[]) {
const isSplitEnName = getPref("ennamesplit");
for (const item of items) {
const creators = item.getCreators();
for (let i = 0; i < creators.length; i++) {
const creator = creators[i];
creator.fieldMode = 0;
if (
// English Name
(creator.lastName!.search(/[A-Za-z]/) >= 0 ||
creator.firstName!.search(/[A-Za-z]/) >= 0) &&
creator.firstName === "" // 名为空
) {
// 如果不拆分/合并英文名,则跳过
if (!isSplitEnName) continue;
const EnglishName = creator.lastName;
const temp = EnglishName!.split(/[\n\s+,]/g).filter(Boolean); // 过滤空字段
creator.lastName = temp.pop();
creator.firstName = temp.join(" ");
} else if (creator.firstName === "") {
// For Chinese Name,名为空
const chineseName = creator.lastName || creator.firstName;
creator.lastName = chineseName.charAt(0);
creator.firstName = chineseName.substr(1);
}
creator.fieldMode = 0; // 0: two-field, 1: one-field (with empty first name)
creators[i] = creator;
}
if (creators != item.getCreators()) {
item.setCreators(creators);
await item.saveTx();
}
const isSplitEnName = getPref("ennamesplit");
for (const item of items) {
const creators = item.getCreators();
for (let i = 0; i < creators.length; i++) {
const creator = creators[i];
creator.fieldMode = 0;
if (
// English Name
(creator.lastName!.search(/[A-Za-z]/) >= 0 ||
creator.firstName!.search(/[A-Za-z]/) >= 0) &&
creator.firstName === "" // 名为空
) {
// 如果不拆分/合并英文名,则跳过
if (!isSplitEnName) continue;
const EnglishName = creator.lastName;
const temp = EnglishName!.split(/[\n\s+,]/g).filter(Boolean); // 过滤空字段
creator.lastName = temp.pop();
creator.firstName = temp.join(" ");
} else if (creator.firstName === "") {
// For Chinese Name,名为空
const chineseName = creator.lastName || creator.firstName;
creator.lastName = chineseName.charAt(0);
creator.firstName = chineseName.substr(1);
}
creator.fieldMode = 0; // 0: two-field, 1: one-field (with empty first name)
creators[i] = creator;
}
if (creators != item.getCreators()) {
item.setCreators(creators);
await item.saveTx();
}
}
}

export async function splitNameMenu(type: "items" | "collection") {
Expand All @@ -83,31 +83,31 @@ export async function splitNameMenu(type: "items" | "collection") {
* 使用此函数将分号分隔的姓名分隔到不同的条目中。
*/
export async function splitSemicolonNames(type: string) {
const items = getItems(type);
for (const item of items) {
const creators = item.getCreators();
const newlist = [];
for (const creator of creators) {
if (creator.lastName!.search(";") && creator.firstName === "") {
const names = creator
.lastName!.split(";")
.filter((s) => s !== "");
for (const name of names) {
newlist.push({
firstName: "",
lastName: name,
creatorType: "author",
});
}
} else {
newlist.push(creator);
}
}
if (newlist !== creators) {
item.setCreators(newlist);
await item.saveTx();
const items = getItems(type);
for (const item of items) {
const creators = item.getCreators();
const newlist = [];
for (const creator of creators) {
if (creator.lastName!.search(";") && creator.firstName === "") {
const names = creator
.lastName!.split(";")
.filter((s) => s !== "");
for (const name of names) {
newlist.push({
firstName: "",
lastName: name,
creatorType: "author",
});
}
} else {
newlist.push(creator);
}
}
if (newlist !== creators) {
item.setCreators(newlist);
await item.saveTx();
}
}
}

/**
Expand Down Expand Up @@ -136,16 +136,16 @@ export async function splitSemicolonNames(type: string) {
* @return {void}
*/
export async function manualSetLanguage(items: Zotero.Item[]) {
const defaultLanguage = Zotero.Prefs.get("jasminum.language") as string;
for (const item of items) {
if (item.getField("language") != defaultLanguage) {
item.setField("language", defaultLanguage);
await item.saveTx();
}
const defaultLanguage = getPref("language") as string;
for (const item of items) {
if (item.getField("language") != defaultLanguage) {
item.setField("language", defaultLanguage);
await item.saveTx();
}
}
}

export async function manualSetLanguageMenu(type: "items"|"collection") {
export async function manualSetLanguageMenu(type: "items" | "collection") {
const items = getItems(type);
await manualSetLanguage(items);
}
Expand Down

0 comments on commit 48d59a4

Please sign in to comment.