Skip to content

Commit

Permalink
🚸🐛 Add a QuteDataTraits and fix some trait/tag bugs
Browse files Browse the repository at this point in the history
- Add a QuteDataTraits - dedicated class to hold traits so we can have
  easier bare linking
- Rename collectTraitsFrom to getTraits, and pull Tag adding into
  QuteDataTraits
- Fix creature statblock so it doesn't generate traits property when we
  don't have any generic traits
- Fix some items being incorrectly tagged with their source (e.g. tagged
  as "trait/preparation-sog2" rather than "trait/preparation")
- Fix some incorrectly formatted markdown links when the trait is
  surrounded in square brackets (e.g. for classes and magical traditions)

Fix
  • Loading branch information
miscoined committed Jul 18, 2024
1 parent 5ca33c2 commit d89513d
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 86 deletions.
23 changes: 10 additions & 13 deletions examples/templates/pf2etools/creature2md-yamlStatblock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,18 @@ sourcebook: {source.quoted}
name: {name.quoted}
level: Creature {level}

{#if alignment}
alignment: {alignment}
{/if}{#if rarity}
rarity: {rarity}
{/if}{#if size}
size: {size}
{/if}{#if traits}
{#with traits.getFirst("Alignment")}
alignment: {withoutTitle.quoted}
{/with}{#with traits.getFirst("Rarity")}
rarity: {withoutTitle.quoted}
{/with}{#with traits.getFirst("Size")}
size: {withoutTitle.quoted}
{/with}{#with traits.genericTraits}
traits:
{#each traits}
{! Don't include traits which are already included above by the statblock layout !}
{#if !it.title || !(it.title.contains("Alignment Trait") || it.title.contains("Size Trait") || it.title.contains("Rarity Trait"))}
- "{it.withoutTitle}"
{/if}
{#each this}
- {it.withoutTitle.quoted}
{/each}
{/if}
{/with}

{#if perception}
modifier: {perception}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import com.fasterxml.jackson.databind.JsonNode;

import dev.ebullient.convert.tools.Tags;
import dev.ebullient.convert.tools.pf2e.qute.Pf2eQuteBase;
import dev.ebullient.convert.tools.pf2e.qute.Pf2eQuteNote;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataRef;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataTraits;

public abstract class Json2QuteBase implements JsonSource {
protected final Pf2eIndex index;
protected final Pf2eIndexType type;
protected final JsonNode rootNode;
protected final Pf2eSources sources;
protected final Tags tags;
protected final Set<QuteDataRef> traits;
protected final QuteDataTraits traits;
protected final List<String> entries;

public Json2QuteBase(Pf2eIndex index, Pf2eIndexType type, JsonNode rootNode) {
Expand All @@ -29,7 +28,7 @@ public Json2QuteBase(Pf2eIndex index, Pf2eIndexType type, JsonNode rootNode, Pf2
this.rootNode = rootNode;
this.sources = sources;
this.tags = new Tags(sources);
this.traits = collectTraitsFrom(rootNode, tags);
this.traits = getTraits(rootNode).addToTags(tags);
this.entries = new ArrayList<>(SourceField.entries.transformListFrom(rootNode, this, "##"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static dev.ebullient.convert.StringUtil.toOrdinal;
import static dev.ebullient.convert.StringUtil.toTitleCase;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand All @@ -19,7 +18,6 @@
import dev.ebullient.convert.qute.NamedText;
import dev.ebullient.convert.tools.pf2e.Pf2eJsonNodeReader.Pf2eAttack;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataActivity.Activity;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataRef;
import dev.ebullient.convert.tools.pf2e.qute.QuteDeity;
import dev.ebullient.convert.tools.pf2e.qute.QuteInlineAttack;
import dev.ebullient.convert.tools.pf2e.qute.QuteInlineAttack.AttackRangeType;
Expand Down Expand Up @@ -154,10 +152,6 @@ private NamedText buildAvatarAbility(JsonNode abilityNode) {
}

private QuteInlineAttack buildAvatarAttack(JsonNode actionNode, AttackRangeType rangeType) {
Collection<QuteDataRef> traits = collectTraitsFrom(actionNode, tags);
Pf2eDeity.preciousMetal.getListOfStrings(actionNode, tui()).stream().map(QuteDataRef::new).forEach(traits::add);
Pf2eDeity.traitNote.getTextFrom(actionNode).map(QuteDataRef::new).ifPresent(traits::add);

return new QuteInlineAttack(
Pf2eAttack.name.getTextOrDefault(actionNode, "attack"),
Pf2eActivity.toQuteActivity(this, Activity.single, null),
Expand All @@ -167,7 +161,9 @@ private QuteInlineAttack buildAvatarAttack(JsonNode actionNode, AttackRangeType
.map(field -> field.getTextOrEmpty(actionNode))
.filter(StringUtil::isPresent)
.toList(),
traits,
getTraits(actionNode).addToTags(tags)
.addTraits(Pf2eDeity.preciousMetal.getListOfStrings(actionNode, tui()))
.addTrait(Pf2eDeity.traitNote.getTextOrEmpty(actionNode)),
Pf2eDeity.note.replaceTextFrom(actionNode, this),
this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public static QuteItemWeaponData buildWeaponData(JsonNode source,
JsonSource convert, Tags tags) {

QuteItemWeaponData weaponData = new QuteItemWeaponData();
weaponData.traits = convert.collectTraitsFrom(source, tags);
weaponData.traits = convert.getTraits(source).addToTags(tags);
weaponData.type = SourceField.type.getTextOrEmpty(source);
weaponData.damage = getDamageString(source, convert);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static dev.ebullient.convert.StringUtil.join;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -13,11 +12,9 @@
import dev.ebullient.convert.io.Tui;
import dev.ebullient.convert.qute.QuteUtil;
import dev.ebullient.convert.tools.JsonNodeReader.FieldValue;
import dev.ebullient.convert.tools.Tags;
import dev.ebullient.convert.tools.pf2e.Json2QuteItem.Pf2eItem;
import dev.ebullient.convert.tools.pf2e.qute.Pf2eQuteBase;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataActivity;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataRef;

public interface JsonSource extends JsonTextReplacement {

Expand Down Expand Up @@ -584,9 +581,7 @@ default List<String> embedGenericData(String tag, JsonNode data) {
text.add("title: " + title);

// Add traits
Tags tags = new Tags();
Collection<QuteDataRef> traits = collectTraitsFrom(data, tags);
text.add(join(" ", traits) + " ");
text.add(join(" ", getTraits(data)) + " ");
maybeAddBlankLine(text);

// Add rendered sections
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.MatchResult;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Collector;
import com.fasterxml.jackson.databind.JsonNode;

import dev.ebullient.convert.config.CompendiumConfig;
import dev.ebullient.convert.io.Tui;
import dev.ebullient.convert.tools.JsonNodeReader;
import dev.ebullient.convert.tools.JsonNodeReader.FieldValue;
import dev.ebullient.convert.tools.JsonTextConverter;
import dev.ebullient.convert.tools.Tags;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataActivity.Activity;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataRef;
import dev.ebullient.convert.tools.pf2e.qute.QuteDataTraits;

public interface JsonTextReplacement extends JsonTextConverter<Pf2eIndexType> {
enum Field implements Pf2eJsonNodeReader {
Expand Down Expand Up @@ -219,17 +217,15 @@ default String replaceActionAs(MatchResult match) {
/**
* Collect and linkify traits from the specified node.
*
* @param tags The tags to populate while collecting traits. If null, then don't populate any tags.
*
* @return a set of {@link QuteDataRef}s to trait notes, or an empty set (never null)
* @return a {@link QuteDataTraits} which may be empty (never null)
*/
default Set<QuteDataRef> collectTraitsFrom(JsonNode sourceNode, Tags tags) {
default QuteDataTraits getTraits(JsonNode sourceNode) {
return Field.traits.getListOfStrings(sourceNode, tui()).stream()
.peek(tags == null ? (t -> {}) : t -> tags.add("trait", t))
.sorted()
.map(s -> linkify(Pf2eIndexType.trait, s))
.map(QuteDataRef::fromMarkdownLink)
.collect(Collectors.toCollection(TreeSet::new));
.map(s -> QuteDataRef.fromMarkdownLink(linkify(Pf2eIndexType.trait, s)))
.collect(Collector.of(QuteDataTraits::new, QuteDataTraits::add, (a, b) -> {
a.addAll(b);
return b;
}));
}

default String linkifyRuneItem(MatchResult match) {
Expand Down Expand Up @@ -373,6 +369,8 @@ default String linkifyTrait(String match) {
default String linkifyTrait(JsonNode traitNode, String linkText) {
if (traitNode != null) {
String source = SourceField.source.getTextOrEmpty(traitNode);
// Some traits are surrounded in square brackets. Strip this out to avoid messing up link rendering.
linkText = linkText.replaceFirst("^\\[(.*)]$", "$1");

return "[%s](%s/%s%s.md \"%s\")".formatted(
linkText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import static java.util.Objects.requireNonNullElse;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
Expand Down Expand Up @@ -95,7 +95,7 @@ default List<QuteInlineAttack> getAttacksFrom(JsonNode source, JsonSource conver
* traits from these activation components to {@code traits}. Return an empty list if we couldn't get activation
* components.
*/
default List<String> getActivationComponentsFrom(JsonNode source, Set<QuteDataRef> traits, JsonSource convert) {
default List<String> getActivationComponentsFrom(JsonNode source, Collection<QuteDataRef> traits, JsonSource convert) {
List<String> rawComponents = getListOfStrings(source, convert.tui()).stream()
.map(s -> s.replaceFirst("^\\((%s)\\)$", "\1")) // remove parens
.toList();
Expand Down Expand Up @@ -655,7 +655,7 @@ public static QuteInlineAttack getAttack(JsonNode node, JsonSource convert) {
attack.getIntFrom(node).orElse(null),
formattedDamage,
types.replaceTextFromList(node, convert),
convert.collectTraitsFrom(node, null),
convert.getTraits(node),
hasMultilineEffect ? List.of() : attackEffects,
hasMultilineEffect ? String.join("\n", attackEffects) : null,
noMAP.booleanOrDefault(node, false) ? List.of() : List.of("no multiple attack penalty"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,6 @@ public QuteCreature(
this.ritualCasting = ritualCasting;
}

/** Return the size of this creature, or null if it has none. */
public String getSize() {
return traits.stream()
.filter(ref -> ref.title() != null && ref.title().contains("Size Trait"))
.findAny().map(QuteDataRef::displayText).orElse(null);
}

/** The alignment of this creature, or null if it has none. */
public String getAlignment() {
return traits.stream()
.filter(ref -> ref.title() != null && ref.title().contains("Alignment Trait"))
.findAny().map(QuteDataRef::displayText).orElse(null);
}

/** The rarity of this creature, or null if it has none. */
public String getRarity() {
return traits.stream()
.filter(ref -> ref.title() != null && ref.title().contains("Rarity Trait"))
.findAny().map(QuteDataRef::displayText).orElse(null);
}

/**
* The languages and language features known by a creature. Example default output:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public record QuteDataRef(String displayText, String notePath, String title) implements Comparable<QuteDataRef> {

private static final Pattern MARKDOWN_LINK_PAT = Pattern.compile("^\\[(?<display>[^]]+)]\\((?<path>.*?)(?: \"(?<title>.*)\")?\\)$");
private static final Pattern MARKDOWN_LINK_PAT = Pattern.compile("^\\[(?<display>.+)]\\((?<path>.*?)(?: \"(?<title>.*)\")?\\)$");

public QuteDataRef(String displayText) {
this(displayText, null, null);
Expand Down
Loading

0 comments on commit d89513d

Please sign in to comment.