-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlistPublications.js
More file actions
88 lines (73 loc) · 2.29 KB
/
listPublications.js
File metadata and controls
88 lines (73 loc) · 2.29 KB
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
const fs = require('fs');
const path = require('path');
const { glob } = require('glob');
const readline = require('readline');
const { Cite } = require('@citation-js/core')
// Load plugins
require('@citation-js/plugin-doi')
require('@citation-js/plugin-csl')
const header = `
This page contains a list of research articles published by members
of the Oxford RSE team, grouped by project.
`;
// Entry point
async function main() {
try {
const people = await loadPeopleNames('people.txt');
const files = await glob('data/**/*.txt');
console.log(header);
for (const file of files.reverse()) {
await processFile(file, people);
}
} catch (err) {
console.error('Error:', err);
}
}
// Function to load people names
async function loadPeopleNames(filePath) {
const readStream = fs.createReadStream(filePath);
const rl = readline.createInterface({ input: readStream });
const people = [];
for await (const line of rl) {
const lineTrimmed = line.trim();
if (lineTrimmed.startsWith('#') || lineTrimmed === '') continue;
people.push(lineTrimmed);
}
return people;
}
// Function to process a single file
async function processFile(filePath, people) {
const readStream = fs.createReadStream(filePath);
const rl = readline.createInterface({ input: readStream });
const stem = path.basename(filePath, path.extname(filePath));
console.log(`## ${stem}\n`)
for await (const line of rl) {
const trimmedLine = line.trim();
if (trimmedLine === '') continue;
const processed = await retrieveCitation(trimmedLine);
console.log("-", linkURLs(highlightMatches(processed, people)))
}
}
function highlightMatches(text, words) {
if (!text || !Array.isArray(words) || words.length === 0) return text;
const escapedWords = words.map(word => word.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
const regex = new RegExp(`(${escapedWords.join('|')})`, 'gi');
return text.replace(regex, '**$1**');
}
function linkURLs(text) {
return text.replace(
/\bhttps?:\/\/\S+\b/g,
match => `<${match}>`
);
}
// Function to process a single line
async function retrieveCitation(line) {
const citation = line.trim()
const data = await Cite.async(citation)
return data.format('bibliography', {
format: 'text',
template: 'apa',
lang: 'en-US'
})
};
main();