forked from larsgw/citation.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
172 lines (150 loc) · 5.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
var core = require('@citation-js/core')
require('@citation-js/plugin-bibjson')
require('@citation-js/plugin-bibtex')
require('@citation-js/plugin-csl')
require('@citation-js/plugin-doi')
require('@citation-js/plugin-ris')
// require('@citation-js/plugin-wikidata')
var citeproc = require('citeproc')
var name = require('@citation-js/name')
var date = require('@citation-js/date')
function clone (obj) {
const copy = {}
for (let key in obj) {
copy[key] = typeof obj[key] === 'object' ? clone(obj[key]) : obj[key]
}
return copy
}
const attr = {
getAttributedEntry (string, name, value) {
return string.replace(/^\s*<[a-z]+/i, `$& data-${name}="${value}"`)
},
getPrefixedEntry (value, id) {
return attr.getAttributedEntry(value, 'csl-entry-id', id)
},
getWrappedEntry (value, source, affixes) {
const getAffix = (source, affix) => typeof affix === 'function' ? affix(source) : affix == null ? affix : ''
if (affixes.prepend == null && affixes.append == null) {
return value
}
let prefix = getAffix(source, affixes.prepend)
let suffix = getAffix(source, affixes.append)
let start = ''
let end = ''
const match = value.match(/^([^>]+>)([\s\S]+)(<[^<]+)$/i)
if (match) {
start = match[1]
value = match[2]
end = match[3]
}
return start + prefix + value + suffix + end
}
}
function Cite (data, opts) {
if (!(this instanceof Cite)) {
return new Cite(data, opts)
}
var self = new core.Cite(data, opts)
this._options = self._options
this.log = self.log
this.data = self.data
}
Cite.prototype = Object.create(core.Cite.prototype)
Cite.async = core.Cite.async
Cite.validateOptions = core.Cite.validateOptions
Cite.validateOutputOptions = core.Cite.validateOutputOptions
Cite.input = core.plugins.input.chain
Cite.inputAsync = core.plugins.input.chainAsync
Cite.util = Object.assign({ attr }, core.util)
Cite.version = {
cite: core.version,
citeproc: citeproc.PROCESSOR_VERSION
}
const CSL = core.plugins.config.get('@csl')
Cite.CSL = {
engine: require('@citation-js/plugin-csl/lib/engines').fetchEngine,
item (data) {
return id => data.find(entry => entry.id === id)
},
locale (lang) {
return CSL.styles.get(CSL.styles.has(lang) ? lang : 'en-US')
},
style (style) {
return CSL.templates.get(CSL.templates.has(style) ? style : 'apa')
},
register: {
addTemplate: CSL.templates.add.bind(CSL.templates),
getTemplate: CSL.templates.get.bind(CSL.templates),
hasTemplate: CSL.templates.has.bind(CSL.templates),
addLocale: CSL.locales.add.bind(CSL.locales),
getLocale: CSL.locales.get.bind(CSL.locales),
hasLocale: CSL.locales.has.bind(CSL.locales)
}
}
Cite.plugins = clone(core.plugins)
delete Cite.plugins.input.util.clean
Cite.parse = Object.assign({
input: {
chain: core.plugins.input.chain,
chainAsync: core.plugins.input.chainAsync,
chainLink: core.plugins.input.chainLink,
chainLinkAsync: core.plugins.input.chainLinkAsync,
data: core.plugins.input.data,
dataAsync: core.plugins.input.dataAsync,
type: core.plugins.input.type,
async: {
chain: core.plugins.input.chainAsync,
chainLink: core.plugins.input.chainLinkAsync,
data: core.plugins.input.dataAsync
}
},
name: name.parse,
date: date.parse,
csl: core.plugins.input.util.clean,
bibjson: require('@citation-js/plugin-bibjson').parsers.json.record,
bibtex: ((bibtex) => ({
json: bibtex.parsers.json.parse,
prop: bibtex.parsers.prop.parse,
text: bibtex.parsers.text.parse,
type: bibtex.parsers.type.parse
}))(require('@citation-js/plugin-bibtex/lib/input')),
bibtxt: ((bibtxt) => ({
text: bibtxt.parse,
textEntry: bibtxt.textEntry
}))(require('@citation-js/plugin-bibtex/lib/input/bibtxt')),
doi: ((doi) => ({
api: doi.parsers.api.parse,
id: doi.parsers.id.parse,
async: {
api: doi.parsers.api.parseAsync
}
}))(require('@citation-js/plugin-doi')),
json: require('@citation-js/core/lib/plugin-common/input').parsers.json.parse,
// wikidata: ((wikidata) => ({
// json: wikidata.parsers.entity.parse,
// list: wikidata.parsers.id.parse,
// prop: wikidata.parsers.prop.parse,
// type: wikidata.parsers.prop.parseType,
// async: {
// json: wikidata.parsers.entity.parseAsync,
// prop (...args) {
// return Promise.resolve(wikidata.parsers.prop.parse.apply(this, args))
// }
// }
// }))(require('@citation-js/plugin-wikidata'))
}, Cite.plugins.input)
Cite.get = Object.assign({
dict: Cite.plugins.dict,
name: name.format,
date: date.format,
bibtex: {
json: require('@citation-js/plugin-bibtex/lib/output/json').default,
label: require('@citation-js/plugin-bibtex/lib/output/label').default,
text: require('@citation-js/plugin-bibtex/lib/output/text').default,
type: require('@citation-js/plugin-bibtex/lib/output/type').default
},
bibtxt: require('@citation-js/plugin-bibtex/lib/output/bibtxt').default,
json: require('@citation-js/core/lib/plugin-common/output').default.data,
label: require('@citation-js/core/lib/plugin-common/output').default.label
}, Cite.plugins.output)
module.exports = Cite