Skip to content

Commit

Permalink
🐛 Fix table roller links; resolves #351
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Mar 24, 2024
1 parent 91310bf commit a85e180
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ private void addIfPresent(List<QuteNote> notes, Set<String> table, String title)
.map(x -> x.replaceAll("^\\|\\s*[\\d-]+\\s*", ""))
.collect(Collectors.toList());

String blockid = "^" + Tui.slugify(title);
String slug = slugify(title);
String blockid = "^" + slug;
List<String> text = new ArrayList<>();
text.add(String.format("`dice: [](%s.md#%s)`", slugify(title), blockid));
text.add(String.format("`dice: [](%s.md#%s)`", slug, blockid));
text.add("");
text.addAll(listToTable(title, rows));

Expand Down Expand Up @@ -81,74 +82,74 @@ private void addIdealsIfPresent(List<QuteNote> notes) {

text.add("| All Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^good-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^evil-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^lawful-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^chaotic-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^good-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^evil-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^lawful-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^chaotic-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^all-ideals");
text.add("");
text.add("| Chaotic Good Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^good-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^chaotic-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^good-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^chaotic-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^cg-ideals");
text.add("");
text.add("| Chaotic Evil Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^evil-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^chaotic-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^evil-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^chaotic-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^ce-ideals");
text.add("");
text.add("| Chaotic Neutral Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^chaotic-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^chaotic-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^cn-ideals");
text.add("");
text.add("| Lawful Good Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^good-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^lawful-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^good-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^lawful-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^lg-ideals");
text.add("");
text.add("| Lawful Evil Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^evil-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^lawful-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^evil-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^lawful-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^le-ideals");
text.add("");
text.add("| Lawful Neutral Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^lawful-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^lawful-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^ln-ideals");
text.add("");
text.add("| Neutral Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^n-ideals");
text.add("");
text.add("| Neutral Good Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^good-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^good-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^ng-ideals");
text.add("");
text.add("| Neutral Evil Ideals |");
text.add("|------------|");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^evil-ideals)` " + " |");
text.add("| `dice: [](" + index.compendiumVaultRoot() + "tables/ideals.md#^universal-ideals-any)` " + " |");
text.add("| `dice: [](ideals.md#^neutral-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^evil-ideals)` " + " |");
text.add("| `dice: [](ideals.md#^universal-ideals-any)` " + " |");
text.add("^ne-ideals");

maybeAddBlankLine(text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class Json2QuteClass extends Json2QuteCommon {
final String subclassTitle;
final String decoratedClassName;

String filename = null;

Json2QuteClass(Tools5eIndex index, Tools5eIndexType type, JsonNode jsonNode) {
super(index, type, jsonNode);
boolean pushed = parseState().push(getSources(), rootNode); // store state
Expand All @@ -54,6 +56,13 @@ public class Json2QuteClass extends Json2QuteCommon {
}
}

@Override
public String getFileName() {
return filename == null
? super.getFileName()
: filename;
}

@Override
protected QuteClass buildQuteResource() {
Tags tags = new Tags(getSources());
Expand Down Expand Up @@ -91,6 +100,7 @@ public List<QuteSubclass> buildSubclasses() {

for (Subclass sc : subclasses) {
boolean pushed = parseState().push(sc.sources);
filename = Tools5eQuteBase.fixFileName(sc.getName(), sc.sources);
try {
Tags tags = new Tags(sc.sources);
tags.add("subclass", getName(), sc.shortName);
Expand Down Expand Up @@ -124,6 +134,7 @@ public List<QuteSubclass> buildSubclasses() {
tags));
} finally {
parseState().pop(pushed);
filename = null;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ default ImageRef buildImageRef(JsonMediaHref mediaHref, String imageBasePath) {
return getSources().buildImageRef(index(), mediaHref, imageBasePath, useCompendium());
}

default String getFileName() {
return Tools5eQuteBase.fixFileName(getName(), getSources());
}

/**
* External (and recursive) entry point for content parsing.
* Parse attributes of the given node and add resulting lines
Expand Down Expand Up @@ -715,7 +719,7 @@ default void appendTable(List<String> text, JsonNode tableNode) {
}
if (header.matches(JsonTextConverter.DICE_TABLE_HEADER) && !blockid.isBlank()) {
// prepend a dice roller
String targetFile = Tools5eQuteBase.fixFileName(getName(), getSources());
String targetFile = getFileName();
table.add(0, String.format("`dice: [](%s.md#%s)`", targetFile, blockid));
table.add(1, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,8 @@ default String linkifyCreature(String match) {
return linkText;
}
boolean isNpc = Json2QuteMonster.isNpc(jsonSource);
return linkOrText(linkText, key, Tools5eQuteBase.monsterPath(isNpc, creatureType),
return linkOrText(linkText, key,
Tools5eQuteBase.monsterPath(isNpc, creatureType),
Tools5eQuteBase.fixFileName(resourceName, sources));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Collection;
import java.util.List;

import com.fasterxml.jackson.databind.JsonNode;

import dev.ebullient.convert.io.Tui;
import dev.ebullient.convert.qute.QuteBase;
import dev.ebullient.convert.tools.CompendiumSources;
Expand Down Expand Up @@ -31,15 +33,24 @@ public Tools5eQuteBase(CompendiumSources sources, String name, String source, St
}

public static String fixFileName(String name, Tools5eSources sources) {
return fixFileName(name, sources.primarySource(), sources.getType());
Tools5eIndexType type = sources.getType();
JsonNode node = sources.findNode();
String primarySource = sources.primarySource();
return switch(type) {
case background -> fixFileName(type.decoratedName(node), primarySource, type);
case deity -> Tui.slugify(getDeityResourceName(name, primarySource, node.get("pantheon").asText()));
case subclass -> getSubclassResource(name, node.get("className").asText(), primarySource);
default -> fixFileName(name, primarySource, type);
};
}

public static String fixFileName(String name, String source, Tools5eIndexType type) {
if (type == Tools5eIndexType.adventureData
|| type == Tools5eIndexType.adventure
|| type == Tools5eIndexType.book
|| type == Tools5eIndexType.bookData) {
return name; // file name is based on chapter, etc.
|| type == Tools5eIndexType.bookData
|| type == Tools5eIndexType.tableGroup) {
return Tui.slugify(name); // file name is based on chapter, etc.
}
name = Tui.slugify(name.replaceAll(" \\(\\*\\)", "-gv"));
return name + sourceIfNotDefault(source, type);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/dev/ebullient/convert/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class TestUtils {
// - title is optional
final static String nextLink = "(?!\\]\\()"; // negative lookahead for ](
final static String linkTitle = "( \".+?\")?"; // optional link title
final static String linkText = "(" + nextLink + ".)+"; // any sequence of characters except ](
final static String linkText = "(" + nextLink + ".)*?"; // any sequence of characters except ](
final static String vaultPath = "(" + nextLink + "[^ ])+"; // any sequence of characters except ]( or space
final static Pattern markdownLinkPattern = Pattern
.compile("\\[" + linkText + "\\]\\((" + vaultPath + ")" + linkTitle + "\\)");
Expand Down

0 comments on commit a85e180

Please sign in to comment.