diff --git a/materials/forms.py b/materials/forms.py index a9ce0d4..55fdc6c 100644 --- a/materials/forms.py +++ b/materials/forms.py @@ -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, diff --git a/materials/static/materials/javascript/autoupdates.js b/materials/static/materials/javascript/autoupdates.js index 30decb1..f769c08 100644 --- a/materials/static/materials/javascript/autoupdates.js +++ b/materials/static/materials/javascript/autoupdates.js @@ -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) { @@ -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 } }); }; @@ -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); diff --git a/materials/templates/materials/add_data.html b/materials/templates/materials/add_data.html index d772523..e7dda85 100644 --- a/materials/templates/materials/add_data.html +++ b/materials/templates/materials/add_data.html @@ -17,6 +17,10 @@