-
Notifications
You must be signed in to change notification settings - Fork 3
story/VSPC-277 #360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Girik1105
wants to merge
5
commits into
develop
Choose a base branch
from
story/VSPC-277
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−128
Open
story/VSPC-277 #360
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
02571ff
[VSPC-277] Fixed html for reference logos and bold title
Girik1105 83bd297
[VSPC-277] Fixed button to icon, the delete icon misaslignment fixed
Girik1105 d4f1031
[VSPC-277] Fixed alignment of new reference button and delete icon
Girik1105 43e45ac
[VSPC-277] Removed dev comments
Girik1105 2078680
[VSPC-277] Fixed bibliography edit issue, fixed alignment of buttons,…
Girik1105 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
218 changes: 218 additions & 0 deletions
218
vspace/src/main/java/edu/asu/diging/vspace/core/util/CitationFormatter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package edu.asu.diging.vspace.core.util; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class CitationFormatter { | ||
|
||
// Pattern to match citation syntax: [@author year, pages] | ||
private static final Pattern CITATION_PATTERN = Pattern.compile( | ||
"\\[@([^,]+)\\s+(\\d{4})(?:,\\s*([^\\]]+))?\\]" | ||
); | ||
|
||
// Pattern to match full citation entries: {author, year, title, journal, etc.} | ||
private static final Pattern FULL_CITATION_PATTERN = Pattern.compile( | ||
"\\{([^}]+)\\}" | ||
); | ||
|
||
/** | ||
* Formats text containing citation markers into proper APA format. | ||
* | ||
* @param text The input text containing citation markers | ||
* @return Formatted text with proper citations | ||
*/ | ||
public static String formatCitations(String text) { | ||
if (text == null || text.isEmpty()) { | ||
return text; | ||
} | ||
|
||
StringBuilder result = new StringBuilder(); | ||
String[] lines = text.split("\n"); | ||
|
||
for (String line : lines) { | ||
result.append(formatLine(line)).append("\n"); | ||
} | ||
|
||
return result.toString().trim(); | ||
} | ||
|
||
/** | ||
* Formats a single line of text with citations. | ||
*/ | ||
private static String formatLine(String line) { | ||
// Handle full citation entries (transform to reference list format) | ||
line = formatFullCitations(line); | ||
|
||
// Handle in-text citations | ||
line = formatInTextCitations(line); | ||
|
||
return line; | ||
} | ||
|
||
/** | ||
* Formats in-text citations like [@author 2020] to (Author, 2020) | ||
*/ | ||
private static String formatInTextCitations(String text) { | ||
Matcher matcher = CITATION_PATTERN.matcher(text); | ||
StringBuffer result = new StringBuffer(); | ||
|
||
while (matcher.find()) { | ||
String author = matcher.group(1).trim(); | ||
String year = matcher.group(2); | ||
String pages = matcher.group(3); | ||
|
||
// Capitalize first letter of author's last name | ||
author = capitalizeAuthor(author); | ||
|
||
String replacement; | ||
if (pages != null && !pages.trim().isEmpty()) { | ||
replacement = String.format("(%s, %s, %s)", author, year, pages.trim()); | ||
} else { | ||
replacement = String.format("(%s, %s)", author, year); | ||
} | ||
|
||
matcher.appendReplacement(result, replacement); | ||
} | ||
matcher.appendTail(result); | ||
|
||
return result.toString(); | ||
} | ||
|
||
/** | ||
* Formats full citation entries into APA reference format | ||
*/ | ||
private static String formatFullCitations(String text) { | ||
Matcher matcher = FULL_CITATION_PATTERN.matcher(text); | ||
StringBuffer result = new StringBuffer(); | ||
|
||
while (matcher.find()) { | ||
String citationData = matcher.group(1); | ||
String formattedReference = parseAndFormatReference(citationData); | ||
matcher.appendReplacement(result, formattedReference); | ||
} | ||
matcher.appendTail(result); | ||
|
||
return result.toString(); | ||
} | ||
|
||
/** | ||
* Parses citation data and formats it as APA reference | ||
*/ | ||
private static String parseAndFormatReference(String citationData) { | ||
String[] parts = citationData.split(","); | ||
if (parts.length < 3) { | ||
return citationData; // Return as-is if not enough parts | ||
} | ||
|
||
String author; | ||
String year; | ||
String title; | ||
int titleIndex; | ||
|
||
// Check if the first part looks like "LastName, FirstName" format | ||
if (parts.length >= 4 && parts[1].trim().matches("^[A-Z][a-z]*\\.?$|^[A-Z][a-z]+$")) { | ||
// Author is "LastName, FirstName" format (first two parts) | ||
author = parts[0].trim() + ", " + parts[1].trim(); | ||
year = parts[2].trim(); | ||
title = parts[3].trim(); | ||
titleIndex = 4; | ||
} else { | ||
// Author is just the first part | ||
author = parts[0].trim(); | ||
year = parts[1].trim(); | ||
title = parts[2].trim(); | ||
titleIndex = 3; | ||
} | ||
|
||
// Basic APA format: Author, A. (Year). Title. | ||
StringBuilder reference = new StringBuilder(); | ||
reference.append(capitalizeAuthor(author)); | ||
reference.append(" (").append(year).append("). "); | ||
reference.append("*").append(title).append("*"); | ||
|
||
if (parts.length > titleIndex) { | ||
String journal = parts[titleIndex].trim(); | ||
reference.append(". ").append(journal); | ||
} | ||
|
||
if (parts.length > titleIndex + 1) { | ||
String pages = parts[titleIndex + 1].trim(); | ||
reference.append(", ").append(pages); | ||
} | ||
|
||
reference.append("."); | ||
|
||
return reference.toString(); | ||
} | ||
|
||
/** | ||
* Capitalizes author name properly for citations | ||
*/ | ||
private static String capitalizeAuthor(String author) { | ||
if (author == null || author.isEmpty()) { | ||
return author; | ||
} | ||
|
||
// Handle "et al." case - preserve it as is | ||
if (author.toLowerCase().contains("et al")) { | ||
return author; // Keep original formatting for et al. | ||
} | ||
|
||
// Handle "lastname, firstname" format | ||
if (author.contains(",")) { | ||
String[] nameParts = author.split(","); | ||
if (nameParts.length >= 2) { | ||
String lastName = nameParts[0].trim(); | ||
String firstName = nameParts[1].trim(); | ||
return capitalizeFirstLetter(lastName) + ", " + | ||
(firstName.length() > 0 ? Character.toUpperCase(firstName.charAt(0)) + "." : ""); | ||
} | ||
} | ||
|
||
// Handle "firstname lastname" format | ||
String[] words = author.split("\\s+"); | ||
if (words.length >= 2) { | ||
String firstName = words[0]; | ||
String lastName = words[words.length - 1]; | ||
return capitalizeFirstLetter(lastName) + ", " + | ||
(firstName.length() > 0 ? Character.toUpperCase(firstName.charAt(0)) + "." : ""); | ||
} | ||
|
||
return capitalizeFirstLetter(author); | ||
} | ||
|
||
/** | ||
* Capitalizes the first letter of a string | ||
*/ | ||
private static String capitalizeFirstLetter(String str) { | ||
if (str == null || str.isEmpty()) { | ||
return str; | ||
} | ||
return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase(); | ||
} | ||
|
||
/** | ||
* Validates if text contains properly formatted citations | ||
*/ | ||
public static boolean hasValidCitations(String text) { | ||
if (text == null || text.isEmpty()) { | ||
return true; // Empty text is valid | ||
} | ||
|
||
// Check for basic citation patterns | ||
return CITATION_PATTERN.matcher(text).find() || | ||
FULL_CITATION_PATTERN.matcher(text).find() || | ||
!containsUnformattedReferences(text); | ||
} | ||
|
||
/** | ||
* Checks if text contains unformatted references that should be citations | ||
*/ | ||
private static boolean containsUnformattedReferences(String text) { | ||
// Look for patterns that suggest unformatted references | ||
String lowerText = text.toLowerCase(); | ||
return lowerText.contains("journal article") || | ||
lowerText.contains("report") || | ||
(lowerText.contains("20") && lowerText.matches(".*\\b\\d{4}\\b.*")); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this class used in this pr?