UNPKG

4.86 kBJavaScriptView Raw
1//import xml from "xml";
2
3export default function(data) {
4
5
6 var date = data.publishedDate;
7
8 var batch_timestamp = Math.floor(Date.now() / 1000);
9 var batch_id = data.authors.length ? data.authors[0].lastName.toLowerCase().slice(0,20) : "Anonymous";
10 batch_id += "_" + date.getFullYear();
11 batch_id += "_" + data.title.split(" ")[0].toLowerCase().slice(0,20) + "_" + batch_timestamp;
12 // generate XML
13 var crf_data =
14 {doi_batch : [
15
16 { _attr: {
17 version: "4.3.7",
18 xmlns: "http://www.crossref.org/schema/4.3.7",
19 "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
20 "xsi:schemaLocation": "http://www.crossref.org/schema/4.3.7 http://www.crossref.org/schemas/crossref4.3.7.xsd",
21 }},
22
23 { head: [
24 {doi_batch_id: batch_id},
25 {timestamp: batch_timestamp},
26 {depositor: [
27 {depositor_name: data.journal.publisherName},
28 {email_address: data.journal.publisherEmail},
29 ]},
30 {registrant: data.journal.publisherName},
31 ]},
32
33 {body: [
34 {journal: [
35
36 {journal_metadata: [
37 {full_title: data.journal.full_title || data.journal.title},
38 {abbrev_title: data.journal.abbrev_title || data.journal.title || data.journal.full_title},
39 {doi_data: [
40 {doi: data.journal.doi},
41 {resource: data.journal.url},
42 ]},
43 ]},
44
45 {journal_issue: [
46 {publication_date: [
47 {month: date.getMonth()+1},
48 {year: date.getFullYear()},
49 ]},
50 {journal_volume: [
51 {volume: data.volume},
52 ]},
53 {issue: data.issue},
54 ]},
55
56 {journal_article: [
57 {titles: [
58 {title: data.title},
59 ]},
60 { contributors:
61 data.authors.map((author, ind) => (
62 {person_name: [
63 { _attr: {
64 contributor_role: "author",
65 sequence: (ind == 0)? "first" : "additional"
66 }},
67 {given_name: author.firstName},
68 {surname: author.lastName},
69 {affiliation: author.affiliation}
70 // TODO: ORCID?
71 ]}
72 ))
73 },
74 {publication_date: [
75 {month: date.getMonth()+1},
76 {day: date.getDate()},
77 {year: date.getFullYear()}
78 ]},
79 { publisher_item: [
80 {item_number: data.doi}
81 ]},
82 {doi_data: [
83 {doi: data.doi},
84 //{timestamp: ""},
85 {resource: data.url},
86 ]},
87 {citation_list:
88 data.citations.map(key =>
89 citation_xml(key, data.bibliography[key]))
90 }
91
92 ]},
93
94 ]},
95 ]},
96 ]};
97
98 return xml(crf_data);
99}
100
101function citation_xml(key, ent){
102 if (ent == undefined) return {};
103 var info = [];
104 info.push({_attr: {key: key}});
105 if ("title" in ent)
106 info.push({article_title: ent.title});
107 if ("author" in ent)
108 info.push({author: ent.author.split(" and ")[0].split(",")[0].trim()});
109 if ("journal" in ent)
110 info.push({journal_title: ent.journal});
111 if ("booktitle" in ent)
112 info.push({volume_title: ent.booktitle});
113 if ("volume" in ent)
114 info.push({volume: ent.volume});
115 if ("issue" in ent)
116 info.push({issue: ent.issue});
117 if ("doi" in ent)
118 info.push({doi: ent.doi});
119 return {citation: info}
120}
121
122function xml(obj) {
123 //console.log(typeof(obj), obj)
124 if (typeof obj === 'string') return obj;
125 if (typeof obj === 'number') return ""+obj;
126 let keys = Object.keys(obj);
127 if (keys.length != 1) console.error("can't interpret ", obj, "as xml");
128 let name = keys[0];
129 var full_content = obj[name];
130 var attr = {};
131 if (Array.isArray(full_content)){
132 var content = [];
133 for (var i in full_content) {
134 var obj = full_content[i];
135 var obj_name = Object.keys(obj)[0];
136 if ("_attr" == obj_name) {
137 attr = obj["_attr"];
138 } else {
139 //console.log(Object.keys(obj)[0])
140 content.push(obj);
141 }
142 }
143 } else {
144 content = full_content;
145 }
146 if (content == undefined){
147 content = "undefined"
148 }
149
150 let attr_string = "";
151 for (var k in attr) {
152 attr_string += ` ${k}=\"${attr[k]}\"`
153 }
154
155 //console.log(typeof content, Array.isArray(content), content instanceof String, content)
156 if (Array.isArray(content)){
157 content = content.map(xml);
158 content = content.join("\n").split("\n");
159 content = content.map(s => " " + s).join("\n")
160 var result = `<${name}${attr_string}>\n${content}\n</${name}>`;
161 } else {
162 content = xml(content);
163 var result = `<${name}${attr_string}>${content}</${name}>`;
164 }
165 return result;
166}