Skip to content

Commit

Permalink
Added feature to parse refrences from .bib files.
Browse files Browse the repository at this point in the history
  • Loading branch information
uthpalaherath committed Oct 2, 2024
1 parent a547a97 commit bfa88a9
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 43 deletions.
7 changes: 7 additions & 0 deletions materials/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class AddReferenceForm(forms.Form):
# widget=forms.ClearableFileInput(attrs={'multiple': True, 'onchange':'handleFileSelect();' }),
help_text="Upload RIS file reference",
)
inputBIBFile = forms.FileField(
required=False,
widget=forms.ClearableFileInput(
attrs={"allow_multiple_selected": True, "onchange": "handleFileSelect();"}
),
help_text="Upload BIB file reference",
)
title = forms.CharField(
widget=forms.TextInput(attrs={"class": "form-control", "id": "title"}),
max_length=1000,
Expand Down
142 changes: 99 additions & 43 deletions materials/static/materials/javascript/autoupdates.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// autoupdates.js

function handleFileSelect() {
// Function to handle RIS file parsing and field population
function handleRISFileSelect() {
const fileInput = document.querySelector('[name="inputRISFile"]');

if (fileInput.files.length > 0) {
Expand Down Expand Up @@ -54,61 +55,115 @@ function handleFileSelect() {
}
});

// Set the values of the form inputs
document.querySelector('[name="title"]').value = title.trim();
document.querySelector('[name="journal"]').value = journal.trim();
document.querySelector('[name="vol"]').value = volume.trim();
document.querySelector('[name="pages_start"]').value = pages_start.trim();
document.querySelector('[name="pages_end"]').value = pages_end.trim();
document.querySelector('[name="year"]').value = year.trim();
document.querySelector('[name="doi_isbn"]').value = doi && isbn ? doi + ', ' + isbn : doi || isbn;

const addAuthorButton = document.querySelector('#add-more-authors-btn');

authors.forEach((author, index) => {
if (index > 0) addAuthorButton.click();

const authorParts = author.split(', ');
document.querySelector(`[name="first-name-${index + 1}"]`).value = authorParts[1] || '';
document.querySelector(`[name="last-name-${index + 1}"]`).value = authorParts[0] || '';
});
};

reader.readAsText(file);
}
}

// Function to handle BIB file parsing and field population
function handleBIBFileSelect() {
const fileInput = document.querySelector('[name="inputBIBFile"]');

if (fileInput.files.length > 0) {
const file = fileInput.files[0];
const reader = new FileReader();

reader.onload = function (event) {
const fileContent = event.target.result;
const lines = fileContent.split('\n'); // Split content into lines

// Initialize variables to store extracted data
let title = '';
let journal = '';
let volume = '';
let pages_start = '';
let pages_end = '';
let year = '';
let doi = '';
let authors = [];

// Iterate over lines and extract data from the .bib file
lines.forEach(line => {
line = line.trim(); // Remove leading/trailing whitespace and trailing commas
if (line.startsWith('title =')) {
title = line.replace(/title\s*=\s*[{"]/i, '').replace(/[",}]/g, '').trim();
}
else if (line.startsWith('journal =')) {
journal = line.replace(/journal\s*=\s*[{"]/i, '').replace(/[",}]/g, '').trim();
}
else if (line.startsWith('volume =')) {
volume = line.replace(/volume\s*=\s*[{"]/i, '').replace(/[",}]/g, '').trim();
}
else if (line.startsWith('pages =')) {
const pages = line.replace(/pages\s*=\s*[{"]/i, '').replace(/[",}]/g, '').trim();
const pagesSplit = pages.split('-'); // Split by dash for start and end pages
pages_start = pagesSplit[0].trim();
pages_end = pagesSplit.length > 1 ? pagesSplit[1].trim() : '';
}
else if (line.startsWith('year =')) {
year = line.replace(/year\s*=\s*[{"]/i, '').replace(/[",}]/g, '').trim();
}
else if (line.startsWith('doi =')) {
doi = line.replace(/doi\s*=\s*[{"]/i, '').replace(/[",}]/g, '').trim();
}
else if (line.startsWith('author =')) {
const authorString = line.replace(/author\s*=\s*[{"]/i, '').replace(/[",}]/g, '').trim();
authors = authorString.split(' and ').map(author => author.trim());
}
});

// 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();
}
document.querySelector('[name="title"]').value = title.trim();
document.querySelector('[name="journal"]').value = journal.trim();
document.querySelector('[name="vol"]').value = volume.trim();
document.querySelector('[name="pages_start"]').value = pages_start.trim();
document.querySelector('[name="pages_end"]').value = pages_end.trim();
document.querySelector('[name="year"]').value = year.trim();

const doiIsbnInput = document.querySelector('[name="doi_isbn"]');
if (doiIsbnInput) {
if (doi && isbn) {
doiIsbnInput.value = doi + ', ' + isbn;
} else {
doiIsbnInput.value = doi || isbn;
}
doiIsbnInput.value = doi.trim();
}

const addAuthorButton = document.querySelector('#add-more-authors-btn'); // Button to add more authors
const addAuthorButton = document.querySelector('#add-more-authors-btn');

// Fill authors one by one
// Populate authors and trigger the add more authors button for each subsequent author
authors.forEach((author, index) => {
if (index > 0) {
// Click the "Add more authors" button for additional authors
addAuthorButton.click();
}
if (index > 0) addAuthorButton.click();

const authorParts = author.split(', ');
const lastName = authorParts[0];
const firstName = authorParts.length > 1 ? authorParts[1] : '';
// Split the author by spaces and assume the last part is the last name
const nameParts = author.split(' ');
const lastName = nameParts.pop(); // The last part is the last name
const firstName = nameParts.join(' '); // The remaining parts are the first name

// Populate the respective author fields dynamically
// Populate the author fields dynamically
const firstNameField = document.querySelector(`[name="first-name-${index + 1}"]`);
const lastNameField = document.querySelector(`[name="last-name-${index + 1}"]`);
const institutionField = document.querySelector(`[name="institution-${index + 1}"]`);

if (firstNameField && lastNameField) {
if (firstNameField && lastNameField && institutionField) {
firstNameField.value = firstName;
lastNameField.value = lastName;
institutionField.value = ''; // No institution provided in .bib
}
});
};
Expand All @@ -117,5 +172,6 @@ function handleFileSelect() {
}
}

// Attach the function to the input file change event
document.querySelector('[name="inputRISFile"]').addEventListener('change', handleFileSelect);
// Attach the functions to their respective input file change events
document.querySelector('[name="inputRISFile"]').addEventListener('change', handleRISFileSelect);
document.querySelector('[name="inputBIBFile"]').addEventListener('change', handleBIBFileSelect);
4 changes: 4 additions & 0 deletions materials/templates/materials/add_data.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ <h4>Add Data</h4>
<div class="form-group">
<label for="{{ reference_form.inputRISFile.id_for_label }}">Upload RIS File:</label>
{{ reference_form.inputRISFile }}
</div>
<div class="form-group">
<label for="{{ reference_form.inputBIBFile.id_for_label }}">Upload BIB File:</label>
{{ reference_form.inputBIBFile }}
</div>
<div class="card-body">
<form id="reference-form" method="post">
Expand Down

0 comments on commit bfa88a9

Please sign in to comment.