Skip to content

Commit

Permalink
[WIP] Adds the ability to specify [camelN] as a title-related field m…
Browse files Browse the repository at this point in the history
…arker for citation key generation (#10772)

* add `camelShort` citation key marker

Add the ability to set a citation key marker `camelShort` that is identical to the `camel` marker but with a maximum of 7 words.

* change name to `shortcamel` 

change name to `shortcamel` to match current naming scheme

* Update src/main/java/org/jabref/logic/citationkeypattern/BracketedPattern.java

* refactor [shortcamel] title to instead use [camelN] to select N words from camelCase title. Not functional yet.

* refactor getTitleWordsWithSpaces to call getSomeWords so that the new function can be used for camelN

* add [camelN] functionality

* add test cases for [camelN]

* add `[camelN]` to changelog

* remove trailing whitespace

---------

Co-authored-by: Christoph <siedlerkiller@gmail.com>
  • Loading branch information
mkdjr and Siedlerchr authored Feb 19, 2024
1 parent b53d7a3 commit 35316f8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We added ability to jump to an entry in the command line using `-j CITATIONKEY`. [koppor#540](https://github.com/koppor/jabref/issues/540)
- We added a new boolean to the style files for Openoffice/Libreoffice integration to switch between ZERO_WIDTH_SPACE (default) and no space. [#10843](https://github.com/JabRef/jabref/pull/10843)
- We added the possibility to redownload files that had been present but are no longer in the specified location. [#10848](https://github.com/JabRef/jabref/issues/10848)
- We added the citation key pattern `[camelN]`. Equivalent to the first N words of the `[camel]` pattern.

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ public static String getFieldValue(BibEntry entry, String pattern, Character key
} else if ("veryshorttitle".equals(pattern)) {
return getTitleWords(1,
removeSmallWords(entry.getResolvedFieldOrAlias(StandardField.TITLE, database).orElse("")));
} else if (pattern.matches("camel[\\d]+")) {
int num = Integer.parseInt(pattern.substring(5));
return getCamelizedTitle_N(entry.getResolvedFieldOrAlias(StandardField.TITLE, database).orElse(""), num);
} else if ("camel".equals(pattern)) {
return getCamelizedTitle(entry.getResolvedFieldOrAlias(StandardField.TITLE, database).orElse(""));
} else if ("shortyear".equals(pattern)) {
Expand Down Expand Up @@ -662,6 +665,36 @@ private static String camelizeTitle(String title) {
return stringBuilder.toString();
}

/**
* Capitalises and concatenates the words out of the "title" field in the given BibTeX entry, to a maximum of N words.
*/
public static String getCamelizedTitle_N(String title, int number) {
return keepLettersAndDigitsOnly(camelizeTitle_N(title, number));
}

private static String camelizeTitle_N(String title, int number) {
StringBuilder stringBuilder = new StringBuilder();
String formattedTitle = formatTitle(title);

try (Scanner titleScanner = new Scanner(formattedTitle)) {
while (titleScanner.hasNext()) {
String word = titleScanner.next();

// Camelize the word
word = word.substring(0, 1).toUpperCase(Locale.ROOT) + word.substring(1);

if (stringBuilder.length() > 0) {
stringBuilder.append(' ');
}
stringBuilder.append(word);
}
}

String camelString = stringBuilder.toString();

return getSomeWords(number, camelString);
}

/**
* Capitalises the significant words of the "title" field in the given BibTeX entry
*/
Expand Down Expand Up @@ -704,7 +737,11 @@ public static String removeSmallWords(String title) {
private static String getTitleWordsWithSpaces(int number, String title) {
String formattedTitle = formatTitle(title);

try (Scanner titleScanner = new Scanner(formattedTitle)) {
return getSomeWords(number, formattedTitle);
}

private static String getSomeWords(int number, String string) {
try (Scanner titleScanner = new Scanner(string)) {
return titleScanner.tokens()
.limit(number)
.collect(Collectors.joining(" "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,19 @@ void expandBracketsDoesNotTruncateWithoutAnArgumentToTruncateModifier() {
BracketedPattern.expandBrackets("[fulltitle:truncate]", ';', dbentry, database));
}

/**
* Test the [camelN] title marker.
*/
@Test
void expandBracketsCamelNTitle() {
assertEquals("Open",
BracketedPattern.expandBrackets("[camel1]", ';', dbentry, database));
assertEquals("OpenSourceSoftwareAnd",
BracketedPattern.expandBrackets("[camel4]", ';', dbentry, database));
assertEquals("OpenSourceSoftwareAndThePrivateCollectiveInnovationModelIssues",
BracketedPattern.expandBrackets("[camel10]", ';', dbentry, database));
}

@Test
void expandBracketsWithAuthorStartingWithBrackets() {
// Issue https://github.com/JabRef/jabref/issues/3920
Expand Down

0 comments on commit 35316f8

Please sign in to comment.