Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@


Comment on lines +1 to +2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set up your workspace with autoformatting and checkstyle according to our guide
https://devdocs.jabref.org/getting-into-the-code/guidelines-for-setting-up-a-local-workspace/intellij-13-code-style.html

package org.jabref.http.server.cayw.format;

import java.util.List;
Expand All @@ -14,8 +16,8 @@
public class BibLatexFormatter implements CAYWFormatter {

@Override
public String getFormatName() {
return "biblatex";
public List<String> getFormatNames() {
return List.of("biblatex");
}

@Override
Expand All @@ -24,13 +26,11 @@ public MediaType getMediaType() {
}

@Override
public String format(CAYWQueryParams queryParams, List<CAYWEntry> cawEntries) {
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
String command = queryParams.getCommand();

List<BibEntry> bibEntries = cawEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();

List<BibEntry> bibEntries = caywEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();
return "\\%s{%s}".formatted(command,
bibEntries.stream()
.map(entry -> entry.getCitationKey().orElse(""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@

public interface CAYWFormatter {

String getFormatName();
List<String> getFormatNames();

default String getFormatName() {
return getFormatNames().get(0);
}

MediaType getMediaType();

String format(CAYWQueryParams caywQueryParams, List<CAYWEntry> cawEntries);
String format(CAYWQueryParams caywQueryParams, List<CAYWEntry> caywEntries);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jabref.http.server.cayw.format;

import java.util.List;
import java.util.stream.Collectors;

import org.jabref.http.server.cayw.CAYWQueryParams;
import org.jabref.http.server.cayw.gui.CAYWEntry;
import org.jabref.model.entry.BibEntry;

import jakarta.ws.rs.core.MediaType;
import org.jvnet.hk2.annotations.Service;

@Service
public class CitepFormatter implements CAYWFormatter {

@Override
public List<String> getFormatNames() {
return List.of("citep", "cite");
}

@Override
public MediaType getMediaType() {
return MediaType.TEXT_PLAIN_TYPE;
}

@Override
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
List<BibEntry> bibEntries = caywEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();
String joined = bibEntries.stream()
.map(entry -> entry.getCitationKey().orElse(""))
.collect(Collectors.joining(","));
return "\\citep{" + joined + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,32 @@

@Service
public class FormatterService {

private static final String DEFAULT_FORMATTER = "biblatex";
private final Map<String, CAYWFormatter> formatters;

public FormatterService() {
this.formatters = new HashMap<>();
registerFormatter(new SimpleJsonFormatter());
registerFormatter(new BibLatexFormatter());
registerFormatter(new NatbibFormatter());
registerFormatter(new LatexFormatter());
registerFormatter(new CiteFormatter());
registerFormatter(new nabbingFormatter());
registerFormatter(new MMDFormatter());
registerFormatter(new CitepFormatter());
registerFormatter(new MmdFormatter());
registerFormatter(new PandocFormatter());
registerFormatter(new TypstFormatter());
registerFormatter(new SimpleJsonFormatter());
}

public void registerFormatter(CAYWFormatter formatter) {
formatters.putIfAbsent(formatter.getFormatName(), formatter);
for (String name : formatter.getFormatNames()) {
formatters.putIfAbsent(name.toLowerCase(), formatter);
}
}

public CAYWFormatter getFormatter(CAYWQueryParams queryParams) {
return formatters.getOrDefault(queryParams.getFormat().toLowerCase(), formatters.get(DEFAULT_FORMATTER));
String format = queryParams.getFormat();
if (format == null) {
return formatters.get(DEFAULT_FORMATTER);
}
return formatters.getOrDefault(format.toLowerCase(), formatters.get(DEFAULT_FORMATTER));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.http.server.cayw.format;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import org.jabref.http.server.cayw.CAYWQueryParams;
Expand All @@ -15,8 +14,8 @@
public class LatexFormatter implements CAYWFormatter {

@Override
public String getFormatName() {
return "latex";
public List<String> getFormatNames() {
return List.of("latex", "tex");
}

@Override
Expand All @@ -25,15 +24,11 @@ public MediaType getMediaType() {
}

@Override
public String format(CAYWQueryParams queryParams, List<CAYWEntry> cawEntries) {
String command = Optional.ofNullable(queryParams.getCommand())
.filter(cmd -> !cmd.isBlank())
.orElse("cite");

List<BibEntry> bibEntries = cawEntries.stream()
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
String command = queryParams.getCommand() != null ? queryParams.getCommand() : "autocite";
List<BibEntry> bibEntries = caywEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();

return "\\%s{%s}".formatted(command,
bibEntries.stream()
.map(entry -> entry.getCitationKey().orElse(""))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty string is used as a fallback for missing citation keys, which could lead to invalid LaTeX syntax. Should handle this case more explicitly or throw an exception.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import org.jvnet.hk2.annotations.Service;

@Service
public class MMDFormatter implements CAYWFormatter {
public class MmdFormatter implements CAYWFormatter {

@Override
public String getFormatName() {
return "mmd";
public List<String> getFormatNames() {
return List.of("mmd", "multimarkdown");
}

@Override
Expand All @@ -24,13 +24,12 @@ public MediaType getMediaType() {
}

@Override
public String format(CAYWQueryParams queryParams, List<CAYWEntry> cawEntries) {
List<BibEntry> bibEntries = cawEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();

public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
List<BibEntry> bibEntries = caywEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();
Comment on lines +28 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This intermediate list creation is unnecessary since the stream is used immediately after. The operations can be chained directly for better performance.

return bibEntries.stream()
.map(entry -> "[@"+ entry.getCitationKey().orElse("") + "]")
.collect(Collectors.joining(" "));
.map(bibEntry -> "[@" + bibEntry.getCitationKey().orElse("") + "]")
.collect(Collectors.joining(""));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import org.jvnet.hk2.annotations.Service;

@Service
public class CiteFormatter implements CAYWFormatter {
public class NatbibFormatter implements CAYWFormatter {

@Override
public String getFormatName() {
return "citep";
public List<String> getFormatNames() {
return List.of("natbib", "nat");
}

@Override
Expand All @@ -24,19 +24,30 @@ public MediaType getMediaType() {
}

@Override
public String format(CAYWQueryParams queryParams, List<CAYWEntry> cawEntries) {
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
String command = queryParams.getCommand();
if (command == null || command.isBlank()) {
command = "citep"; // default for citep
if (command == null) {
command = "citep";
}

List<BibEntry> bibEntries = cawEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();
command = mapToNatbibCommand(command);

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

return "\\%s{%s}".formatted(command,
bibEntries.stream()
.map(entry -> entry.getCitationKey().orElse(""))
.collect(Collectors.joining(",")));
}

private String mapToNatbibCommand(String command) {
return switch (command) {
case "author" -> "citeauthor";
case "textcite" -> "citet";
case "year" -> "citeyear";
default -> command;
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@

@Service
public class PandocFormatter implements CAYWFormatter {

@Override
public String getFormatName() {
return "pandoc";
public List<String> getFormatNames() {
return List.of("pandoc", "markdown");
}

@Override
Expand All @@ -24,13 +23,21 @@ public MediaType getMediaType() {
}

@Override
public String format(CAYWQueryParams queryParams, List<CAYWEntry> cawEntries) {
List<BibEntry> bibEntries = cawEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();

return bibEntries.stream()
.map(entry -> "[@"+ entry.getCitationKey().orElse("") + "]")
.collect(Collectors.joining(" "));
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
String command = queryParams.getCommand();
List<BibEntry> bibEntries = caywEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();
// Handle "parencite" command with square brackets
if ("parencite".equalsIgnoreCase(command)) {
Comment on lines +31 to +32
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is trivial and only restates what is obvious from the code itself. Comments should add new information or reasoning that cannot be derived directly from the code.

return bibEntries.stream()
.map(bibEntry -> "[@" + bibEntry.getCitationKey().orElse("") + "]")
.collect(Collectors.joining(""));
} else {
// Default: bare @key format with semicolon separator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment merely describes what the code does without providing additional context or reasoning. Such trivial comments should be removed as they don't add value beyond what's visible in the code.

return bibEntries.stream()
.map(bibEntry -> "@" + bibEntry.getCitationKey().orElse(""))
.collect(Collectors.joining("; "));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@

@Service
public class SimpleJsonFormatter implements CAYWFormatter {

private final Gson gson;

public SimpleJsonFormatter() {
this.gson = new GsonFactory().provide();
}

@Override
public String getFormatName() {
return "simple-json";
public List<String> getFormatNames() {
return List.of("simple-json");
}

@Override
Expand All @@ -31,10 +30,10 @@ public MediaType getMediaType() {
}

@Override
public String format(CAYWQueryParams queryParams, List<CAYWEntry> cawEntries) {
List<SimpleJson> simpleJsons = cawEntries.stream()
.map(caywEntry -> SimpleJson.fromBibEntry(caywEntry.bibEntry()))
.toList();
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
List<SimpleJson> simpleJsons = caywEntries.stream()
.map(caywEntry -> SimpleJson.fromBibEntry(caywEntry.bibEntry()))
.toList();
return gson.toJson(simpleJsons);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
public class TypstFormatter implements CAYWFormatter {

@Override
public String getFormatName() {
return "typst";
public List<String> getFormatNames() {
return List.of("typst", "typ");
}

@Override
Expand All @@ -24,13 +24,12 @@ public MediaType getMediaType() {
}

@Override
public String format(CAYWQueryParams queryParams, List<CAYWEntry> cawEntries) {
List<BibEntry> bibEntries = cawEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();

public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
List<BibEntry> bibEntries = caywEntries.stream()
.map(CAYWEntry::bibEntry)
.toList();
Comment on lines +28 to +30
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intermediate list creation is unnecessary since the stream is used immediately after. The stream operations could be chained directly for better performance.

return bibEntries.stream()
.map(entry -> "@" + entry.getCitationKey().orElse(""))
.collect(Collectors.joining(" "));
.map(bibEntry -> "@" + bibEntry.getCitationKey().orElse(""))
.collect(Collectors.joining(", "));
}
}
Loading
Loading