-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
144 lines (116 loc) · 4.08 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
const fs = require('fs-extra');
const path = require('path');
const Computron = require('computron');
const { getStylesheetFrom } = require('tei-conditor');
const _ = require('lodash');
const computronInstances = [];
const coXslt = {};
coXslt.initialJob = callback => {
const possibleSources = [
'crossref',
'hal',
'pubmed',
'sudoc-ouvrages',
'sudoc-theses',
];
const promises = [];
possibleSources.forEach(source => {
promises.push(getStylesheetFrom(source)
.then(stylesheets => {
return new Promise((resolve, reject) => {
if (stylesheets.length === 0) {
return reject(new Error(`No stylesheet found for ${source}`));
}
if (stylesheets.length > 1) {
return reject(new Error('More than one stylesheet found'));
}
const computronInstance = new Computron();
const stylesheet = stylesheets.pop();
try {
computronInstance.loadStylesheet(stylesheet.path);
} catch (err) {
return reject(err);
}
computronInstances.push({
name: source,
instance: computronInstance,
});
resolve();
});
}),
);
});
Promise.all(promises).then(() => callback()).catch(callback);
};
coXslt.doTheJob = (docObject, callback) => {
if (!docObject.idIstex) {
return callback(handleError(docObject, 'NoIdIstexError', new Error('No idIstex found in docObject')));
}
if (!docObject.corpusOutput) {
return callback(handleError(docObject, 'NoCorpusOutputError', new Error('No corpusOutput found in docObject')));
}
const originalXmlFile = _.find(docObject.metadata, { mime: 'application/xml', original: true });
if (!originalXmlFile) {
return callback(handleError(docObject, 'NoOriginalXmlError', new Error('No original XML found in metadata array')));
}
// If docObject.source is undefined, set it to what is after 'conditor:' in docObject.cartoType
if (!docObject.source && docObject.cartoType.startsWith('conditor:')) {
docObject.source = docObject.cartoType.substring(9);
} else if (!docObject.source && docObject.cartoType.startsWith('corhal:')) {
docObject.source = docObject.cartoType.substring(7);
}
const { idIstex } = docObject;
const teiDocDirectory = path.join(docObject.corpusOutput, idIstex[0], idIstex[1], idIstex[2], idIstex, 'metadata');
const teiDocPath = path.join(teiDocDirectory, `${idIstex}.tei.xml`);
let transformer = computronInstances.find(element => element.name === docObject.source);
if (!transformer) {
return callback(handleError(docObject, 'NoComputronInstanceError', new Error(`No Computron instance found for ${docObject.source}`)));
}
transformer = transformer.instance;
fs.ensureDir(teiDocDirectory)
.then(() => {
const conf = coXslt.config ? coXslt.config : {};
if (!conf.today) conf.today = today();
return new Promise((resolve, reject) => {
try {
const result = transformer.apply(originalXmlFile.path, conf);
resolve(result);
} catch (err) {
reject(handleError(docObject, 'ApplyStylesheetError', err));
}
});
})
.then(xmlTei => {
return new Promise((resolve, reject) => {
fs.writeFile(teiDocPath, xmlTei, 'utf-8')
.then(() => resolve())
.catch(err => reject(handleError(docObject, 'WriteFileError', err)));
});
})
.then(() => {
docObject.metadata.push({
path: teiDocPath,
mime: 'application/tei+xml',
original: false,
});
docObject.path = teiDocPath;
callback();
})
.catch(callback);
};
function handleError (docObject, errName, originalErr) {
docObject.errCode = errName;
docObject.errMsg = originalErr.message;
originalErr.name = errName;
return originalErr;
}
function today () {
const date = new Date();
let dd = date.getDate();
let mm = date.getMonth() + 1;
const yyyy = date.getFullYear();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
return yyyy + '/' + mm + '/' + dd;
}
module.exports = coXslt;