Skip to content

Commit

Permalink
Parse additional details (except author info) from RIS files.
Browse files Browse the repository at this point in the history
  • Loading branch information
uthpalaherath committed Oct 1, 2024
1 parent bfdecac commit 1d3bd78
Showing 1 changed file with 78 additions and 13 deletions.
91 changes: 78 additions & 13 deletions materials/static/materials/javascript/autoupdates.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// autoupdates.js

function handleFileSelect() {
const fileInput = document.querySelector('[name="inputRISFile"]');
const textInput = document.querySelector('[name="title"]');


if (fileInput.files.length > 0) {
const file = fileInput.files[0];
const reader = new FileReader();
Expand All @@ -10,20 +11,84 @@ function handleFileSelect() {
const fileContent = event.target.result;
const lines = fileContent.split('\n'); // Split content into lines

// Search for lines starting with "T1 -" or "TI -"
const relevantLines = lines.filter(line =>
line.trim().startsWith("TI -") || line.trim().startsWith("T1 -")
);
// Initialize variables to store extracted data
let title = '';
let journal = '';
let volume = '';
let pages_start = '';
let pages_end = '';
let year = '';
let doi = '';
let isbn = '';

// Iterate over lines and extract data
lines.forEach(line => {
line = line.trim(); // Remove leading/trailing whitespace
if (line.startsWith('TI -') || line.startsWith('T1 -')) {
title += line.replace(/^(T1|TI)\s*-\s*/, '') + ' ';
}
else if (line.startsWith('JO -') || line.startsWith('JF -')) {
journal += line.replace(/^(JO|JF)\s*-\s*/, '') + ' ';
}
else if (line.startsWith('VL -')) {
volume += line.replace(/^VL\s*-\s*/, '');
}
else if (line.startsWith('SP -')) {
pages_start += line.replace(/^SP\s*-\s*/, '');
}
else if (line.startsWith('EP -')) {
pages_end += line.replace(/^EP\s*-\s*/, '');
}
else if (line.startsWith('PY -')) {
year += line.replace(/^PY\s*-\s*/, '');
}
else if (line.startsWith('DO -')) {
doi += line.replace(/^DO\s*-\s*/, '').trim();
}
else if (line.startsWith('SN -')) {
isbn += line.replace(/^SN\s*-\s*/, '').trim();
}
// Add more fields as needed
});

// Now set the values of the form inputs
const titleInput = document.querySelector('[name="title"]');
if (titleInput) {
titleInput.value = title.trim();
}
const journalInput = document.querySelector('[name="journal"]');
if (journalInput) {
journalInput.value = journal.trim();
}
const volumeInput = document.querySelector('[name="vol"]');
if (volumeInput) {
volumeInput.value = volume.trim();
}
const pagesStartInput = document.querySelector('[name="pages_start"]');
if (pagesStartInput) {
pagesStartInput.value = pages_start.trim();
}
const pagesEndInput = document.querySelector('[name="pages_end"]');
if (pagesEndInput) {
pagesEndInput.value = pages_end.trim();
}
const yearInput = document.querySelector('[name="year"]');
if (yearInput) {
yearInput.value = year.trim();
}

// Extract the text after "T1 -" or "T1 -"
const extractedText = relevantLines.map(line =>
line.trim().replace(/^(T1|TI) -/, "")
).join("\n");
// Set the DOI and ISBN with a comma and space separator if both are present
const doiIsbnInput = document.querySelector('[name="doi_isbn"]');
if (doiIsbnInput) {
if (doi && isbn) {
doiIsbnInput.value = doi + ', ' + isbn;
} else {
doiIsbnInput.value = doi || isbn;
}
}

// Set the extracted text to textInput.value
textInput.value = extractedText;
};

reader.readAsText(file); // Read the file as text
}
}
}

0 comments on commit 1d3bd78

Please sign in to comment.