Skip to content

Commit dfef211

Browse files
authored
Merge pull request l0o0#43 from aiyamia/namepatent-feature-ext
feat: 允许更多的附件文件名称规则, close l0o0#37
2 parents b4e11e3 + 5a52e58 commit dfef211

File tree

3 files changed

+47
-33
lines changed

3 files changed

+47
-33
lines changed

chrome/content/scripts/jasminum.js

+45-31
Original file line numberDiff line numberDiff line change
@@ -186,41 +186,55 @@ Zotero.Jasminum = {
186186
splitFilename: function (filename) {
187187
// Make query parameters from filename
188188
var patent = Zotero.Prefs.get("jasminum.namepatent");
189-
var patentArr = patent.split("_");
189+
var patentSepArr = patent.split(/{%[^}]+}/);
190+
var patentSepRegArr = patentSepArr.map(x => x.replace(/([\[\\\^\$\.\|\?\*\+\(\)])/g,'\\$&'));
191+
var patentMainArr = patent.match(/{%[^}]+}/g);
192+
//文件名中的作者姓名字段里不能包含下划线,请使用“&,,”等字符分隔多个作者,或仅使用第一个作者名(加不加“等”都行)。
193+
var patentMainRegArr = patentMainArr.map(x => x.replace(/.+/,/{%y}/.test(x)?'(\\d+)':(/{%a}/.test(x)?'([^_]+)':'(.+)')));
194+
var regStrInterArr = patentSepRegArr.map((_,i)=>[patentSepRegArr[i],patentMainRegArr[i]]);
195+
var patentReg = new RegExp([].concat.apply([],regStrInterArr).filter(Boolean).join(''),'g');
190196
var prefix = filename.substr(0, filename.length - 4);
191197
var prefix = prefix.replace(/\.ashx$/g, ""); // 删除末尾.ashx字符
192-
var author = "";
193-
var title = "";
194-
if (prefix.includes("_")) {
195-
// 有下划线
196-
// Remove year string
197-
if (patent.includes("{%y}")) {
198-
patentArr.splice(patentArr.indexOf("{%y}"), 1);
199-
prefix = prefix.replace(/[0-9]{4}[\._]/g, "");
200-
}
201-
var prefixArr = prefix.replace(/^_|_$/g, "").split("_");
202-
console.log(patentArr);
203-
console.log(prefixArr);
204-
if (patentArr.includes("{%g}")) {
205-
var authorIdx = patentArr.indexOf("{%g}");
206-
var authorIdx = authorIdx === 0 ? 0 : prefixArr.length - 1;
207-
console.log(authorIdx);
208-
author = prefixArr[authorIdx];
209-
prefixArr.splice(authorIdx, 1);
210-
var missIndex = prefixArr.indexOf("省略");
211-
if (missIndex > 0) {
212-
prefixArr.splice(missIndex - 1, 3); // Delete before and after 省略
213-
}
214-
title = prefixArr.join(" ");
215-
} else {
216-
title = prefixArr.join(" ");
217-
}
218-
} else {
219-
// 无下划线直接把文件名认为title
220-
title = prefix;
198+
var prefixMainArr = patentReg.exec(prefix);
199+
var titleIdx = patentMainArr.indexOf('{%t}');
200+
var authorIdx = patentMainArr.indexOf('{%a}');
201+
var titleRaw = (titleIdx!=-1)?prefixMainArr[titleIdx+1]:'';
202+
var authors = (authorIdx!=-1)?prefixMainArr[authorIdx+1]:'';
203+
var authorArr = authors.split(/[,&]/);
204+
var author = authorArr[0]
205+
if (authorArr.length == 1){
206+
//删除名字后可能出现的“等”字,此处未能做到识别该字是否属于作者姓名。
207+
//这种处理方式的问题:假如作者名最后一个字为“等”,例如:“刘等”,此时会造成误删。
208+
//于是对字符数进行判断,保证删除“等”后,至少还剩两个字符,尽可能地避免误删。
209+
210+
author = (author.endsWith('等')&&author.length>2)?author.substr(0, author.length - 1):author;
211+
}
212+
213+
//为了避免文件名中的标题字段里存在如下两种情况而导致的搜索失败:
214+
//原标题过长,文件名出现“_省略_”;
215+
//原标题有特殊符号(如希腊字母、上下标)导致的标题变动,此时标题也会出现“_”。
216+
//于是只取用标题中用“_”分割之后的最长的部分作为用于搜索的标题。
217+
218+
//这种处理方式的问题:假如“最长的部分”中存在知网改写的部分,也可能搜索失败。
219+
//不过这只是理论上可能存在的情形,目前还未实际遇到。
220+
221+
var title;
222+
223+
if (/_/.test(titleRaw)){
224+
225+
//getLongestText函数,用于拿到字符串数组中的最长字符
226+
//摘自https://stackoverflow.com/a/59935726
227+
const getLongestText = (arr) => arr.reduce(
228+
(savedText, text) => (text.length > savedText.length ? text : savedText),
229+
'',
230+
);
231+
title = getLongestText(titleRaw.split(/_/));
232+
}else{
233+
title = titleRaw;
221234
}
235+
222236
return {
223-
author: author.replace(",", ""),
237+
author: author,
224238
keyword: title,
225239
};
226240
},

chrome/locale/en-US/overlay.dtd

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<!ENTITY jasminum.pdftk.info "Jasminum use PDFtk server to add bookmarks. Please install PDFtk server properly and choose folder contains PDFtk server execute file">
1616
<!ENTITY pdftk.path.check.ok "PDFtk server path is OK.">
1717
<!ENTITY pdftk.path.check.error "Can't find PDFtk execute file in this folder.">
18-
<!ENTITY jasminum.namepatent.desc "Filename format:{&#37;g}=Chinese fullname,{&#37;t}=title, {&#37;y}=year, seperate by '_',ignore file extension">
18+
<!ENTITY jasminum.namepatent.desc "Filename format:{&#37;t}=title,{&#37;a}=author, {&#37;y}=year,{&#37;j}=other info(such as source), no limit on delimiters,ignore file extension">
1919
<!ENTITY jasminum.zhnamesplit.label "Split firstname and lastname when get metadata(Including CNKI web translators)">
2020
<!ENTITY jasminum.rename.label "Use Zotfile to rename attachment(Need Zotfile installed)">
2121
<!ENTITY jasminum.autobookmark.label "Add bookmark to thesis PDF when adding thesis item from CNKI">

chrome/locale/zh-CN/overlay.dtd

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<!ENTITY jasminum.pdftk.info "添加书签功能依赖于PDFtk server,正确安装并设置好安装目录才能实现书签添加功能,请选择PDFtk可执行文件所在目录">
1616
<!ENTITY pdftk.path.check.ok "PDFtk Server安装目录检查通过">
1717
<!ENTITY pdftk.path.check.error "未在该目录下发现pdftk执行程序">
18-
<!ENTITY jasminum.namepatent.desc "根据文件名抓取知网元数据,文件名格式设置:{&#37;g}=全名,{&#37;t}=标题名,{&#37;y}=年份,以下划线_分隔,不用考虑文件后缀名">
18+
<!ENTITY jasminum.namepatent.desc "根据文件名抓取知网元数据,文件名格式设置:{&#37;t}=标题,{&#37;a}=作者,{&#37;y}=年份,{&#37;j}=其他(例如来源信息);分隔符依实情指定,可连续使用多个;不用考虑文件后缀名">
1919
<!ENTITY jasminum.zhnamesplit.label "抓取题录信息时拆分中文姓,名(包括知网页面抓取)">
2020
<!ENTITY jasminum.rename.label "拉取知网元数据后调用Zotfile重命名附件(需安装Zotfile)">
2121
<!ENTITY jasminum.autobookmark.label "从知网添加学位论文时自动添加书签">

0 commit comments

Comments
 (0)