Skip to content

Commit

Permalink
🎨 ✨ Config: full-source.homebrew; cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ebullient committed Jan 19, 2024
1 parent aa057b6 commit 53b17c0
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 148 deletions.
16 changes: 12 additions & 4 deletions examples/config/config.5e.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@
"template" : {
"background" : "examples/templates/tools5e/images-background2md.txt"
},
"useDiceRoller" : false,
"tagPrefix" : "",
"useDiceRoller" : true,
"tagPrefix" : "ttrpg-cli",
"images" : {
"relativeRemoteRoot" : "alternate/path/for/remote/images",
"copyRemote" : false,
"checkRemote" : false
},
"full-source" : {
"adventure" : [
"LMoP"
],
"book" : [
"PHB"
],
"adventure" : [
"LMoP"
"homebrew" : [
"homebrew/collection/Kobold Press; Deep Magic 14 Elemental Magic.json"
]
}
}
12 changes: 10 additions & 2 deletions examples/config/config.5e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ excludePattern:
- "race|.*|dmg"
template:
background: "examples/templates/tools5e/images-background2md.txt"
useDiceRoller: true
tagPrefix: "ttrpg-cli"
images:
relativeRemoteRoot: "alternate/path/for/remote/images"
copyRemote: false
checkRemote: false
full-source:
book:
- "PHB"
adventure:
- "LMoP"
book:
- "PHB"
homebrew:
- "homebrew/collection/Kobold Press; Deep Magic 14 Elemental Magic.json"
12 changes: 7 additions & 5 deletions examples/config/config.pf2e.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
"include" : [
"ability|buck|b1"
],
"includeGroup" : [ ],
"exclude" : [
"background|insurgent|apg"
],
Expand All @@ -20,13 +19,16 @@
"template" : {
"ability" : "../path/to/ability2md.txt"
},
"useDiceRoller" : false,
"tagPrefix" : "",
"useDiceRoller" : true,
"tagPrefix" : "ttrpg-cli",
"images" : {
"copyRemote" : false,
"checkRemote" : false
},
"full-source" : {
"book" : [
"crb",
"gmg"
],
"adventure" : [ ]
]
}
}
5 changes: 5 additions & 0 deletions examples/config/config.pf2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ excludePattern:
- "background|.*|lowg"
template:
ability: "../path/to/ability2md.txt"
useDiceRoller: true
tagPrefix: "ttrpg-cli"
images:
copyRemote: false
checkRemote: false
full-source:
book:
- "crb"
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/dev/ebullient/convert/RpgDataConvertCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ public Integer call() {
allOk &= tui.readFile(toolsPath.resolve(book), TtrpgConfig.getFixes(book), index::importTree);
}
}
for (String brew : config.getHomebrew()) {
allOk &= tui.readFile(Path.of(brew), TtrpgConfig.getFixes(brew), index::importTree);
}

if (!allOk) {
tui.println("❌ errors reading data. Check the following: ",
Expand Down Expand Up @@ -237,7 +240,7 @@ public Integer call() {
tpl.setCustomTemplates(config);

MarkdownWriter writer = new MarkdownWriter(output, tpl, tui);
index.markdownConverter(writer, TtrpgConfig.imageFallbackPaths())
index.markdownConverter(writer)
.writeAll()
.writeNotesAndTables()
.writeImages();
Expand Down
64 changes: 40 additions & 24 deletions src/main/java/dev/ebullient/convert/config/CompendiumConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -18,6 +20,7 @@
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;

Expand All @@ -35,13 +38,15 @@ public class CompendiumConfig {

String tagPrefix = "";
PathAttributes paths;
ImageOptions images;
boolean allSources = false;
boolean useDiceRoller = false;
final Set<String> allowedSources = new HashSet<>();
final Set<String> includedKeys = new HashSet<>();
final Set<String> includedGroups = new HashSet<>();
final Set<String> excludedKeys = new HashSet<>();
final Set<Pattern> excludedPatterns = new HashSet<>();
final Set<String> homebrew = new HashSet<>();
final Set<String> adventures = new HashSet<>();
final Set<String> books = new HashSet<>();
final Map<String, Path> customTemplates = new HashMap<>();
Expand Down Expand Up @@ -126,19 +131,19 @@ public boolean groupIsIncluded(String group) {
}

public String rulesVaultRoot() {
return getPaths().rulesVaultRoot;
return pathAttributes().rulesVaultRoot;
}

public String compendiumVaultRoot() {
return getPaths().compendiumVaultRoot;
return pathAttributes().compendiumVaultRoot;
}

public Path rulesFilePath() {
return getPaths().rulesFilePath;
return pathAttributes().rulesFilePath;
}

public Path compendiumFilePath() {
return getPaths().compendiumFilePath;
return pathAttributes().compendiumFilePath;
}

public String tagOf(String... tag) {
Expand Down Expand Up @@ -175,6 +180,10 @@ public List<String> getAdventures() {
.toList();
}

public Collection<String> getHomebrew() {
return Collections.unmodifiableCollection(homebrew);
}

public Path getCustomTemplate(String id) {
return customTemplates.get(id);
}
Expand Down Expand Up @@ -209,13 +218,20 @@ private void addExcludePattern(String value) {
excludedPatterns.add(Pattern.compile(String.join("|", split)));
}

private PathAttributes getPaths() {
private PathAttributes pathAttributes() {
if (paths == null) {
return paths = new PathAttributes();
}
return paths;
}

ImageOptions imageOptions() {
if (images == null) {
return images = new ImageOptions();
}
return images;
}

/**
* Create / populate CompendiumConfig in TtrpgConfig
*/
Expand Down Expand Up @@ -303,6 +319,7 @@ private void readConfig(CompendiumConfig config, JsonNode node) {

config.books.addAll(input.fullSource.book);
config.adventures.addAll(input.fullSource.adventure);
config.homebrew.addAll(input.fullSource.homebrew);

config.paths = new PathAttributes(config.paths, input.paths);

Expand Down Expand Up @@ -415,54 +432,53 @@ JsonNode get(JsonNode node) {
}

@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public static class InputConfig {
@JsonProperty()
List<String> from = new ArrayList<>();

@JsonProperty()
@JsonAlias({ "convert" })
@JsonProperty(value = "full-source")
FullSource fullSource = new FullSource();

InputPaths paths = new InputPaths();

@JsonProperty()
List<String> include = new ArrayList<>();

@JsonProperty()
List<String> includeGroup = new ArrayList<>();

@JsonProperty()
List<String> exclude = new ArrayList<>();

@JsonProperty()
List<String> excludePattern = new ArrayList<>();

@JsonProperty()
Map<String, String> template = new HashMap<>();

@JsonProperty()
boolean useDiceRoller = false;

@JsonProperty()
String tagPrefix = "";

@JsonAlias({ "convert" })
@JsonProperty(value = "full-source")
FullSource fullSource = new FullSource();
ImageOptions images = new ImageOptions();
}

@RegisterForReflection
static class FullSource {
@JsonProperty()
List<String> book = new ArrayList<>();
@JsonInclude(JsonInclude.Include.NON_EMPTY)
static class ImageOptions {
String relativeRemoteRoot;
boolean copyRemote;
boolean checkRemote;
}

@JsonProperty()
@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_EMPTY)
static class FullSource {
List<String> adventure = new ArrayList<>();
List<String> book = new ArrayList<>();
List<String> homebrew = new ArrayList<>();
}

@RegisterForReflection
@JsonInclude(JsonInclude.Include.NON_EMPTY)
static class InputPaths {
@JsonProperty()
String compendium;

@JsonProperty()
String rules;
}
}
38 changes: 25 additions & 13 deletions src/main/java/dev/ebullient/convert/config/TtrpgConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,41 +45,49 @@ public static CompendiumConfig getConfig(Datasource datasource) {
return userConfig.computeIfAbsent(datasource, (k) -> new CompendiumConfig(datasource, tui));
}

private static DatasourceConfig activeConfig() {
private static DatasourceConfig activeDSConfig() {
return globalConfig.computeIfAbsent(datasource, (k) -> new DatasourceConfig());
}

public static List<Fix> getFixes(String filepath) {
return activeConfig().findFixesFor(filepath.replace('\\', '/'));
return activeDSConfig().findFixesFor(filepath.replace('\\', '/'));
}

public static String sourceToLongName(String src) {
return activeConfig().abvToName.getOrDefault(sourceToAbbreviation(src).toLowerCase(), src);
return activeDSConfig().abvToName.getOrDefault(sourceToAbbreviation(src).toLowerCase(), src);
}

public static String sourceToAbbreviation(String src) {
return activeConfig().longToAbv.getOrDefault(src.toLowerCase(), src);
return activeDSConfig().longToAbv.getOrDefault(src.toLowerCase(), src);
}

public static Collection<String> getTemplateKeys() {
return activeConfig().templateKeys;
return activeDSConfig().templateKeys;
}

public static boolean addHomebrewSource(String name, String abv, String longAbv) {
return activeConfig().addSource(name, abv, longAbv);
return activeDSConfig().addSource(name, abv, longAbv);
}

public static void includeBookAdventureSource(String src) {
Configurator config = new Configurator(getConfig());
config.setSources(List.of(src));
}

public static String remoteImageRoot() {
String remoteImageRoot = getConfig().imageOptions().relativeRemoteRoot;
if (remoteImageRoot != null) {
remoteImageRoot = activeDSConfig().constants.get("remoteImageRoot");
}
return remoteImageRoot;
}

public static Map<String, String> imageFallbackPaths() {
return activeConfig().fallbackImagePaths;
return activeDSConfig().fallbackImagePaths;
}

public static JsonNode readIndex(String key) {
String file = activeConfig().indexes.get(key);
String file = activeDSConfig().indexes.get(key);
Optional<Path> root = file == null ? Optional.empty() : tui.resolvePath(Path.of(file));
if (root.isEmpty()) {
return NullNode.getInstance();
Expand All @@ -94,11 +102,11 @@ public static JsonNode readIndex(String key) {
}

public static JsonNode activeGlobalConfig(String key) {
return activeConfig().data.get(key);
return activeDSConfig().data.get(key);
}

public static void checkKnown(Collection<String> bookSources) {
DatasourceConfig activeConfig = activeConfig();
DatasourceConfig activeConfig = activeDSConfig();
bookSources.forEach(s -> {
String check = s.toLowerCase();
if (activeConfig.abvToName.containsKey(check)) {
Expand All @@ -115,17 +123,17 @@ public static void checkKnown(Collection<String> bookSources) {
}

public static Collection<String> getMarkerFiles() {
DatasourceConfig activeConfig = activeConfig();
DatasourceConfig activeConfig = activeDSConfig();
return Collections.unmodifiableSet(activeConfig.markerFiles);
}

public static Collection<String> getFileSources() {
DatasourceConfig activeConfig = activeConfig();
DatasourceConfig activeConfig = activeDSConfig();
return Collections.unmodifiableSet(activeConfig.sources);
}

public static void addDefaultAliases(Map<String, String> aliases) {
DatasourceConfig activeConfig = activeConfig();
DatasourceConfig activeConfig = activeDSConfig();
activeConfig.aliases.forEach((k, v) -> aliases.putIfAbsent(k, v));
}

Expand All @@ -148,6 +156,7 @@ protected static void readSystemConfig(JsonNode node) {
if (srdEntries != null) {
config.data.put(ConfigKeys.srdEntries.name(), srdEntries);
}
config.constants.putAll(ConfigKeys.constants.getAsMap(config5e));
config.aliases.putAll(ConfigKeys.aliases.getAsMap(config5e));
config.abvToName.putAll(ConfigKeys.abvToName.getAsKeyLowerMap(config5e));
config.longToAbv.putAll(ConfigKeys.longToAbv.getAsKeyLowerMap(config5e));
Expand Down Expand Up @@ -184,6 +193,7 @@ protected static void readSystemConfig(JsonNode node) {

static class DatasourceConfig {
final Map<String, JsonNode> data = new HashMap<>();
final Map<String, String> constants = new HashMap<>();
final Map<String, String> aliases = new HashMap<>();
final Map<String, String> abvToName = new HashMap<>();
final Map<String, String> longToAbv = new HashMap<>();
Expand Down Expand Up @@ -239,7 +249,9 @@ enum ConfigKeys implements JsonNodeReader {
abvToName,
config5e,
configPf2e,
constants,
fallbackImage,
remoteImageRoot,
fixes,
indexes,
longToAbv,
Expand Down
Loading

0 comments on commit 53b17c0

Please sign in to comment.