Skip to content

Commit ce5cd12

Browse files
josephyimjinchat-joseph-yimpalukku
authored
add: CAYW endpoint formats (#13785)
* add new formatters and cayw resource tests * update changelog * correct method name and expected result in tests * add sources of new formatter * use bib entry constructor that takes citation key in test case * use builder of BibEntry with citation key * Update the format of typst to consider special character in key, and improve code readability of switch statement. * Remove unnecessary constructor code for consistency * Ue cite in typst format only when key contains slash * Remove meaningless comment * Update jabsrv/src/main/java/org/jabref/http/server/cayw/format/TypstFormatter.java * Update jabsrv/src/test/java/org/jabref/http/server/cayw/format/CAYWFormattersTest.java * Fix test * Update jabsrv/src/test/java/org/jabref/http/server/cayw/format/CAYWFormattersTest.java --------- Co-authored-by: jinchat-joseph-yim <joseph.yim@jinchat.com> Co-authored-by: Philip <37398281+palukku@users.noreply.github.com>
1 parent 261ae7c commit ce5cd12

File tree

15 files changed

+356
-34
lines changed

15 files changed

+356
-34
lines changed

.jbang/JabSrvLauncher.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/CAYWQueryParams.java
2020
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/CAYWResource.java
2121
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/format/BibLatexFormatter.java
22+
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/format/NatbibFormatter.java
23+
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/format/MMDFormatter.java
24+
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/format/PandocFormatter.java
25+
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/format/TypstFormatter.java
2226
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/format/CAYWFormatter.java
2327
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/format/FormatterService.java
2428
//SOURCES ../jabsrv/src/main/java/org/jabref/http/server/cayw/format/SimpleJsonFormatter.java

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
3737
- We added support for finding CSL-Styles based on their short title (e.g. apa instead of "american psychological association"). [#13728](https://github.com/JabRef/jabref/pull/13728)
3838
- We added a field for the latest ICORE conference ranking lookup on the General Tab. [#13476](https://github.com/JabRef/jabref/issues/13476)
3939
- We added BibLaTeX datamodel validation support in order to improve error message quality in entries' fields validation. [#13318](https://github.com/JabRef/jabref/issues/13318)
40+
- We added more supported formats of CAYW endpoint of HTTP server. [#13578](https://github.com/JabRef/jabref/issues/13578)
4041

4142
### Changed
4243

jabsrv/src/main/java/org/jabref/http/server/cayw/CAYWQueryParams.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class CAYWQueryParams {
2222
private String clipboard;
2323

2424
@QueryParam("command")
25-
@DefaultValue("autocite")
2625
private String command;
2726

2827
@QueryParam("minimize")
@@ -46,8 +45,8 @@ public class CAYWQueryParams {
4645
@QueryParam("application")
4746
private String application;
4847

49-
public String getCommand() {
50-
return command;
48+
public Optional<String> getCommand() {
49+
return Optional.ofNullable(command);
5150
}
5251

5352
public boolean isClipboard() {

jabsrv/src/main/java/org/jabref/http/server/cayw/CAYWResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public Response getCitation(
7474
if (queryParams.isProbe()) {
7575
return Response.ok("ready").build();
7676
}
77-
77+
7878
BibDatabaseContext databaseContext = getBibDatabaseContext(queryParams);
7979

8080
// Selected parameter handling
@@ -127,7 +127,7 @@ public Response getCitation(
127127

128128
// Push to Application parameter handling
129129
if (queryParams.getApplication().isPresent()) {
130-
CitationCommandString citationCmd = new CitationCommandString("\\".concat(queryParams.getCommand()).concat("{"), ",", "}");
130+
CitationCommandString citationCmd = new CitationCommandString("\\".concat(queryParams.getCommand().orElse("autocite")).concat("{"), ",", "}");
131131
PushToApplications.getApplication(queryParams.getApplication().get(), LOGGER::info, preferences.getPushToApplicationPreferences().withCitationCommand(citationCmd))
132132
.ifPresent(application -> application.pushEntries(searchResults.stream().map(CAYWEntry::bibEntry).toList()));
133133
}

jabsrv/src/main/java/org/jabref/http/server/cayw/format/BibLatexFormatter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jabref.http.server.cayw.format;
22

33
import java.util.List;
4+
import java.util.Optional;
45
import java.util.stream.Collectors;
56

67
import org.jabref.http.server.cayw.CAYWQueryParams;
@@ -13,9 +14,10 @@
1314
@Service
1415
public class BibLatexFormatter implements CAYWFormatter {
1516

16-
@Override
17-
public String getFormatName() {
18-
return "biblatex";
17+
private final String defaultCommand;
18+
19+
public BibLatexFormatter(String defaultCommand) {
20+
this.defaultCommand = defaultCommand;
1921
}
2022

2123
@Override
@@ -25,15 +27,16 @@ public MediaType getMediaType() {
2527

2628
@Override
2729
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
28-
String command = queryParams.getCommand();
30+
String command = queryParams.getCommand().orElse(defaultCommand);
2931

3032
List<BibEntry> bibEntries = caywEntries.stream()
3133
.map(CAYWEntry::bibEntry)
3234
.toList();
3335

3436
return "\\%s{%s}".formatted(command,
3537
bibEntries.stream()
36-
.map(entry -> entry.getCitationKey().orElse(""))
38+
.map(BibEntry::getCitationKey)
39+
.flatMap(Optional::stream)
3740
.collect(Collectors.joining(",")));
3841
}
3942
}

jabsrv/src/main/java/org/jabref/http/server/cayw/format/CAYWFormatter.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
public interface CAYWFormatter {
1111

12-
String getFormatName();
13-
1412
MediaType getMediaType();
1513

1614
String format(CAYWQueryParams caywQueryParams, List<CAYWEntry> caywEntries);
Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.jabref.http.server.cayw.format;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
3+
import java.util.Locale;
54

65
import org.jabref.http.server.cayw.CAYWQueryParams;
76

@@ -10,20 +9,28 @@
109
@Service
1110
public class FormatterService {
1211

13-
private static final String DEFAULT_FORMATTER = "biblatex";
14-
private final Map<String, CAYWFormatter> formatters;
15-
16-
public FormatterService() {
17-
this.formatters = new HashMap<>();
18-
registerFormatter(new SimpleJsonFormatter());
19-
registerFormatter(new BibLatexFormatter());
20-
}
21-
22-
public void registerFormatter(CAYWFormatter formatter) {
23-
formatters.putIfAbsent(formatter.getFormatName(), formatter);
24-
}
25-
2612
public CAYWFormatter getFormatter(CAYWQueryParams queryParams) {
27-
return formatters.getOrDefault(queryParams.getFormat().toLowerCase(), formatters.get(DEFAULT_FORMATTER));
13+
String format = queryParams.getFormat().toLowerCase(Locale.ROOT);
14+
15+
return switch (format) {
16+
case "natbib",
17+
"latex",
18+
"cite" ->
19+
new NatbibFormatter("cite");
20+
case "citep" ->
21+
new NatbibFormatter("citep");
22+
case "citet" ->
23+
new NatbibFormatter("citet");
24+
case "mmd" ->
25+
new MMDFormatter();
26+
case "pandoc" ->
27+
new PandocFormatter();
28+
case "simple-json" ->
29+
new SimpleJsonFormatter();
30+
case "typst" ->
31+
new TypstFormatter();
32+
default ->
33+
new BibLatexFormatter("autocite");
34+
};
2835
}
2936
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jabref.http.server.cayw.format;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
7+
import org.jabref.http.server.cayw.CAYWQueryParams;
8+
import org.jabref.http.server.cayw.gui.CAYWEntry;
9+
import org.jabref.model.entry.BibEntry;
10+
11+
import jakarta.ws.rs.core.MediaType;
12+
13+
public class MMDFormatter implements CAYWFormatter {
14+
15+
@Override
16+
public MediaType getMediaType() {
17+
return MediaType.TEXT_PLAIN_TYPE;
18+
}
19+
20+
@Override
21+
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
22+
List<BibEntry> bibEntries = caywEntries.stream()
23+
.map(CAYWEntry::bibEntry)
24+
.toList();
25+
26+
return bibEntries.stream()
27+
.map(entry -> entry.getCitationKey().map("[#%s][]"::formatted))
28+
.flatMap(Optional::stream)
29+
.collect(Collectors.joining(""));
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.jabref.http.server.cayw.format;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
7+
import org.jabref.http.server.cayw.CAYWQueryParams;
8+
import org.jabref.http.server.cayw.gui.CAYWEntry;
9+
import org.jabref.model.entry.BibEntry;
10+
11+
import jakarta.ws.rs.core.MediaType;
12+
import org.jvnet.hk2.annotations.Service;
13+
14+
@Service
15+
public class NatbibFormatter implements CAYWFormatter {
16+
17+
private final String defaultCommand;
18+
19+
public NatbibFormatter(String defaultCommand) {
20+
this.defaultCommand = defaultCommand;
21+
}
22+
23+
@Override
24+
public MediaType getMediaType() {
25+
return MediaType.TEXT_PLAIN_TYPE;
26+
}
27+
28+
@Override
29+
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
30+
String command = queryParams.getCommand().orElse(defaultCommand);
31+
32+
List<BibEntry> bibEntries = caywEntries.stream()
33+
.map(CAYWEntry::bibEntry)
34+
.toList();
35+
36+
return "\\%s{%s}".formatted(command,
37+
bibEntries.stream()
38+
.map(BibEntry::getCitationKey)
39+
.flatMap(Optional::stream)
40+
.collect(Collectors.joining(",")));
41+
}
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jabref.http.server.cayw.format;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
7+
import org.jabref.http.server.cayw.CAYWQueryParams;
8+
import org.jabref.http.server.cayw.gui.CAYWEntry;
9+
import org.jabref.model.entry.BibEntry;
10+
11+
import jakarta.ws.rs.core.MediaType;
12+
13+
public class PandocFormatter implements CAYWFormatter {
14+
15+
@Override
16+
public MediaType getMediaType() {
17+
return MediaType.TEXT_PLAIN_TYPE;
18+
}
19+
20+
@Override
21+
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
22+
List<BibEntry> bibEntries = caywEntries.stream()
23+
.map(CAYWEntry::bibEntry)
24+
.toList();
25+
26+
return "[%s]".formatted(bibEntries.stream()
27+
.map(entry -> entry.getCitationKey().map("@%s"::formatted))
28+
.flatMap(Optional::stream)
29+
.collect(Collectors.joining("; ")));
30+
}
31+
}

0 commit comments

Comments
 (0)