Skip to content

Commit

Permalink
🐛 Fix Pf2e image paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Apr 28, 2024
1 parent f1fe6fe commit 7eea3bc
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/main/java/dev/ebullient/convert/RpgDataConvertCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public Integer call() {
boolean isTools = tui.readToolsDir(input, index::importTree);
if (isTools) { // we found the tools directory
toolsPath = input;
TtrpgConfig.setToolsPath(toolsPath);
} else {
// this is some other directory full of json
allOk &= tui.readDirectory("", input, index::importTree);
Expand Down
36 changes: 26 additions & 10 deletions src/main/java/dev/ebullient/convert/config/TtrpgConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@

public class TtrpgConfig {

public static final String DEFAULT_IMG_ROOT = "imgRoot";

static final Map<Datasource, DatasourceConfig> globalConfig = new HashMap<>();
static final Map<Datasource, CompendiumConfig> userConfig = new HashMap<>();
static final Set<String> missingSourceName = new HashSet<>();

private static Datasource datasource = Datasource.tools5e;
private static Tui tui;
private static ImageRoot internalImageRoot;
private static Path toolsPath;

public static void init(Tui tui, Datasource datasource) {
userConfig.clear();
internalImageRoot = null;
TtrpgConfig.tui = tui;
TtrpgConfig.datasource = datasource;
TtrpgConfig.internalImageRoot = null;
TtrpgConfig.toolsPath = null;
readSystemConfig();
}

Expand All @@ -53,6 +57,10 @@ public static String getConstant(String key) {
return activeDSConfig().constants.get(key);
}

public static void setToolsPath(Path toolsPath) {
TtrpgConfig.toolsPath = toolsPath;
}

private static DatasourceConfig activeDSConfig() {
return globalConfig.computeIfAbsent(datasource, (k) -> new DatasourceConfig());
}
Expand Down Expand Up @@ -111,6 +119,7 @@ private ImageRoot(String cfgRoot, ImageOptions options) {
} else {
if (cfgRoot.startsWith("http") || cfgRoot.startsWith("file:")) {
this.internalImageRoot = endWithSlash(cfgRoot);
this.copyInternal = options.copyInternal();
} else {
Path imgPath = Path.of("").resolve(cfgRoot).normalize().toAbsolutePath();
if (!imgPath.toFile().exists()) {
Expand All @@ -120,8 +129,8 @@ private ImageRoot(String cfgRoot, ImageOptions options) {
return;
}
this.internalImageRoot = endWithSlash(imgPath.toString());
this.copyInternal = true;
}
this.copyInternal = options.copyInternal();
Tui.instance().printlnf("[ 🖼️ OK] Using %s as the source for remote images (copyInternal=%s)",
this.internalImageRoot, this.copyInternal);
}
Expand All @@ -139,13 +148,6 @@ public boolean copyExternalToVault() {
return copyExternal;
}

private String endWithSlash(String path) {
if (path == null) {
return "";
}
return path.endsWith("/") ? path : path + "/";
}

public String getFallbackPath(String key) {
return fallbackPaths.getOrDefault(key, key);
}
Expand All @@ -157,13 +159,27 @@ public static ImageRoot internalImageRoot() {
ImageOptions options = getConfig().imageOptions();
String cfg = options.internalRoot;
if (cfg == null) {
cfg = activeDSConfig().constants.get("5etools-img");
String imgRoot = activeDSConfig().constants.get(DEFAULT_IMG_ROOT);
if (imgRoot == null && toolsPath != null && datasource == Datasource.toolsPf2e) {
cfg = toolsPath.resolve("..").normalize().toString();
} else if (imgRoot == null) {
cfg = "";
} else {
cfg = imgRoot;
}
}
internalImageRoot = root = new ImageRoot(cfg, options);
}
return root;
}

private static String endWithSlash(String path) {
if (path == null) {
return "";
}
return path.endsWith("/") ? path : path + "/";
}

public static JsonNode readIndex(String key) {
String file = activeDSConfig().indexes.get(key);
Optional<Path> root = file == null ? Optional.empty() : tui.resolvePath(Path.of(file));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/ebullient/convert/qute/ImageRef.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ public ImageRef build() {
if (sourceUrl.startsWith("http") || sourceUrl.startsWith("file")) {
sourceUrl = sourceUrl.replaceAll("^(https?):/+", "$1://");
copyToVault = imageRoot.copyExternalToVault();
} else if (sourceUrl.startsWith("stream/")) {
copyToVault = true;
} else if (!sourceUrl.startsWith("file:/")) {
sourceUrl = imageRoot.getRootPath() + sourceUrl;
copyToVault = imageRoot.copyInternalToVault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ default String _replaceTokenText(String input, boolean nested) {
}

String[] parts = match.group(1).split("\\|");
String imgRepo = TtrpgConfig.getConstant("5etools-img");
String imgRepo = TtrpgConfig.getConstant(TtrpgConfig.DEFAULT_IMG_ROOT);
String url = ImageRef.Builder.fixUrl(imgRepo + (imgRepo.endsWith("/") ? "" : "/") + parts[1]);

return String.format("[%s](%s)", parts[0], url);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/convertData.json
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
]
},
"constants": {
"5etools-img": "https://raw.githubusercontent.com/5etools-mirror-2/5etools-img/main/"
"imgRoot": "https://raw.githubusercontent.com/5etools-mirror-2/5etools-img/main/"
},
"markerFiles": [
"bestiary/bestiary-mm.json",
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/dev/ebullient/convert/Pf2eDataConvertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static void setupDir(String root) {

@Test
void testLiveData_Pf2eAllSources(QuarkusMainLauncher launcher) {
if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
// All, I mean it. Really for real.. ALL.
final Path allIndex = testOutput.resolve("all-index");
TestUtils.deleteDir(allIndex);
Expand All @@ -48,13 +48,13 @@ void testLiveData_Pf2eAllSources(QuarkusMainLauncher launcher) {
"-s", "ALL",
"-o", allIndex.toString(),
"-g", "pf2e",
TestUtils.TOOLS_PATH_PF2E.toString()));
TestUtils.PATH_PF2E_TOOLS_DATA.toString()));

args.addAll(TestUtils.getFilesFrom(TestUtils.TOOLS_PATH_PF2E.resolve("adventure"))
args.addAll(TestUtils.getFilesFrom(TestUtils.PATH_PF2E_TOOLS_DATA.resolve("adventure"))
.stream()
.filter(x -> !x.endsWith("-id.json"))
.toList());
args.addAll(TestUtils.getFilesFrom(TestUtils.TOOLS_PATH_PF2E.resolve("book")));
args.addAll(TestUtils.getFilesFrom(TestUtils.PATH_PF2E_TOOLS_DATA.resolve("book")));

LaunchResult result = launcher.launch(args.toArray(new String[0]));
assertThat(result.exitCode())
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 @@ -41,7 +41,7 @@ public class TestUtils {

public final static Path PATH_5E_HOMEBREW = PROJECT_PATH.resolve("sources/5e-homebrew");
public final static Path PATH_5E_UA = PROJECT_PATH.resolve("sources/5e-unearthed-arcana");
public final static Path TOOLS_PATH_PF2E = PROJECT_PATH.resolve("sources/Pf2eTools/data");
public final static Path PATH_PF2E_TOOLS_DATA = PROJECT_PATH.resolve("sources/Pf2eTools/data");

static String GENERATED_DOCS = PROJECT_PATH.resolve("docs/templates").normalize().toAbsolutePath().toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public CommonDataTests(TestInput variant, Path toolsData) throws Exception {
tui.setTemplates(templates);

TtrpgConfig.init(tui, Datasource.tools5e);
TtrpgConfig.setToolsPath(TestUtils.PATH_5E_TOOLS_DATA);

configurator = new Configurator(tui);
if (imgPresent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ public CommonDataTests(TestInput variant) throws Exception {
outputPath.toFile().mkdirs();

TtrpgConfig.init(tui, Datasource.toolsPf2e);
TtrpgConfig.setToolsPath(TestUtils.PATH_PF2E_TOOLS_DATA);

configurator = new Configurator(tui);

index = new Pf2eIndex(TtrpgConfig.getConfig());
templates.setCustomTemplates(TtrpgConfig.getConfig());

if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
switch (variant) {
case all:
configurator.addSources(List.of("*"));
Expand All @@ -72,9 +74,9 @@ public CommonDataTests(TestInput variant) throws Exception {

for (String x : List.of("books.json",
"book/book-crb.json", "book/book-gmg.json")) {
tui.readFile(TestUtils.TOOLS_PATH_PF2E.resolve(x), TtrpgConfig.getFixes(x), index::importTree);
tui.readFile(TestUtils.PATH_PF2E_TOOLS_DATA.resolve(x), TtrpgConfig.getFixes(x), index::importTree);
}
tui.readToolsDir(TestUtils.TOOLS_PATH_PF2E, index::importTree);
tui.readToolsDir(TestUtils.PATH_PF2E_TOOLS_DATA, index::importTree);
index.prepare();
}
}
Expand All @@ -88,7 +90,7 @@ public void cleanup() {
}

public void testDataIndex_pf2e() throws Exception {
if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
Path full = outputPath.resolve("allIndex.json");
index.writeFullIndex(full);

Expand Down Expand Up @@ -120,7 +122,7 @@ public void testNotes_p2fe() throws Exception {
Path rulesDir = outputPath.resolve(index.rulesFilePath());
Path compendiumDir = outputPath.resolve(index.compendiumFilePath());

if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
MarkdownWriter writer = new MarkdownWriter(outputPath, templates, tui);
index.markdownConverter(writer)
.writeNotesAndTables()
Expand All @@ -146,7 +148,7 @@ Map<Pf2eIndexType, Path> generateNotesForType(List<Pf2eIndexType> types) {
paths.add(p);
});

if (TestUtils.TOOLS_PATH_PF2E.toFile().exists()) {
if (TestUtils.PATH_PF2E_TOOLS_DATA.toFile().exists()) {
paths.forEach(p -> TestUtils.deleteDir(p));

MarkdownWriter writer = new MarkdownWriter(outputPath, templates, tui);
Expand Down

0 comments on commit 7eea3bc

Please sign in to comment.