Skip to content

Commit

Permalink
LogosHTML: Allow specifying order of Logos links
Browse files Browse the repository at this point in the history
Prefixes can be ordered globally (e.g. first all Strongs then all Morph)
or per Strong number (so that links belonging to one Strong number stay
together). For the former, prefix the property value with "global:".
  • Loading branch information
schierlm committed Oct 12, 2024
1 parent 93eeed0 commit c31e28e
Showing 1 changed file with 32 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -21,6 +22,8 @@
/** Generator for Logos links. */
public class LogosLinksGenerator {

private boolean orderPerGroup = false;
private List<String> prefixOrder = null;
private Map<String, List<ExtraLinkRule>> extraLinkRules;

public LogosLinksGenerator() throws IOException {
Expand Down Expand Up @@ -74,10 +77,19 @@ public LogosLinksGenerator() throws IOException {
}
}
}
String linkPrefixOrder = System.getProperty("biblemulticonverter.logos.linkprefixorder", "");
if (!linkPrefixOrder.isEmpty()) {
if (linkPrefixOrder.startsWith("global:")) {
linkPrefixOrder = linkPrefixOrder.substring(7);
} else {
orderPerGroup = true;
}
prefixOrder = Arrays.asList(linkPrefixOrder.split(","));
}
}

public List<String> generateLinks(boolean nt, Versification.Reference verseReference, char[] strongsPrefixes, int[] strongs, String[] rmac, int[] sourceIndices) {
List<String> links = new ArrayList<String>();
List<String> allLinks = new ArrayList<String>();
String[] expandedStrongs = strongs == null ? null : new String[strongs.length];
if (strongs != null) {
for (int i = 0; i < strongs.length; i++) {
Expand All @@ -86,6 +98,7 @@ public List<String> generateLinks(boolean nt, Versification.Reference verseRefer
}
int max = Math.max(strongs == null ? 0 : strongs.length, rmac == null ? 0 : rmac.length);
for (int i = 0; i < max; i++) {
List<String> links = orderPerGroup ? new ArrayList<>() : allLinks;
if (strongs != null && i < strongs.length) {
boolean skipStrongs = false;
for (ExtraLinkRule r : extraLinkRules.getOrDefault(expandedStrongs[i], Collections.emptyList())) {
Expand Down Expand Up @@ -125,8 +138,25 @@ else if (expandedStrongs[i].charAt(0) == 'H')
}
}
}
if (orderPerGroup) {
sortLinks(links);
allLinks.addAll(links);
}
}
return links;
if (!orderPerGroup) {
sortLinks(allLinks);
}
return allLinks;
}

private void sortLinks(List<String> links) {
if (prefixOrder == null || prefixOrder.isEmpty())
return;
links.sort(Comparator.comparing(link -> {
int colonPos = link.indexOf(':');
int pos = colonPos == -1 ? -1 : prefixOrder.indexOf(link.substring(0, colonPos));
return pos == -1 ? Integer.MAX_VALUE : pos;
}));
}

private static class ExtraLinkRule {
Expand Down

0 comments on commit c31e28e

Please sign in to comment.