Skip to content

Commit 18103a9

Browse files
committed
Tweak markdown parsing, extra context
1 parent f21cd9f commit 18103a9

10 files changed

Lines changed: 152 additions & 91 deletions

File tree

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ dependencies {
5656
//modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
5757
modCompileOnly("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")
5858
modLocalRuntime("net.fabricmc.fabric-api:fabric-api:${project.fabric_version}")
59-
modLocalRuntime("eu.pb4:polymer-reg-sync-manipulator:0.0.1+1.19")
6059

6160
// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
6261
// You may need to force-disable transitiveness on them.

docs/user/general.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ Inner part of placeholder can take shape of either `category:placeholder` or `ca
1010
Additionally, some placeholders might have additional or required argument provided after first space. It's format
1111
fully depend on mod providing it.
1212

13-
You can check list of [build in placeholders here](/users/default-placeholders)
14-
and [placeholders from mods here](/users/mod-placeholders).
13+
You can check list of [build in placeholders here](/user/default-placeholders)
14+
and [placeholders from mods here](/user/mod-placeholders).
1515

1616
### List of mods supporting displaying Placeholder API's placeholders:
1717

gradle.properties

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G
33

44
# Fabric Properties
55
# check these on https://fabricmc.net/use
6-
minecraft_version=1.19.3-rc1
7-
yarn_mappings=1.19.3-rc1+build.1
6+
minecraft_version=1.19.3
7+
yarn_mappings=1.19.3+build.1
88
loader_version=0.14.11
99

1010
#Fabric api
1111
fabric_version=0.67.1+1.19.3
1212

1313
# Mod Properties
14-
mod_version = 2.0.0-pre.2+1.19.3
14+
mod_version = 2.0.0-pre.3+1.19.3
1515
maven_group = eu.pb4
1616
archives_base_name = placeholder-api
1717

src/main/java/eu/pb4/placeholders/api/PlaceholderContext.java

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package eu.pb4.placeholders.api;
22

33
import com.mojang.authlib.GameProfile;
4-
import com.mojang.brigadier.context.CommandContext;
4+
import eu.pb4.placeholders.impl.placeholder.ViewObjectImpl;
55
import net.minecraft.entity.Entity;
66
import net.minecraft.server.MinecraftServer;
77
import net.minecraft.server.command.CommandOutput;
88
import net.minecraft.server.command.ServerCommandSource;
99
import net.minecraft.server.network.ServerPlayerEntity;
1010
import net.minecraft.server.world.ServerWorld;
1111
import net.minecraft.text.Text;
12+
import net.minecraft.util.Identifier;
1213
import net.minecraft.util.math.Vec2f;
1314
import net.minecraft.util.math.Vec3d;
1415

@@ -19,9 +20,18 @@ public record PlaceholderContext(MinecraftServer server,
1920
@Nullable ServerWorld world,
2021
@Nullable ServerPlayerEntity player,
2122
@Nullable Entity entity,
22-
@Nullable GameProfile gameProfile
23+
@Nullable GameProfile gameProfile,
24+
ViewObject view
2325
) {
2426

27+
public PlaceholderContext(MinecraftServer server,
28+
ServerCommandSource source,
29+
@Nullable ServerWorld world,
30+
@Nullable ServerPlayerEntity player,
31+
@Nullable Entity entity,
32+
@Nullable GameProfile gameProfile) {
33+
this(server, source, world, player, entity, gameProfile, ViewObject.DEFAULT);
34+
}
2535

2636

2737
public static ParserContext.Key<PlaceholderContext> KEY = new ParserContext.Key<>("placeholder_context", PlaceholderContext.class);
@@ -46,28 +56,68 @@ public ParserContext asParserContext() {
4656
return ParserContext.of(KEY, this);
4757
}
4858

59+
public PlaceholderContext withView(ViewObject view) {
60+
return new PlaceholderContext(this.server, this.source, this.world, this.player, this.entity, this.gameProfile, view);
61+
}
62+
63+
public void addToContext(ParserContext context) {
64+
context.with(KEY, this);
65+
}
66+
67+
4968
public static PlaceholderContext of(MinecraftServer server) {
50-
return new PlaceholderContext(server, server.getCommandSource(), null, null, null, null);
69+
return of(server, ViewObject.DEFAULT);
70+
}
71+
72+
public static PlaceholderContext of(MinecraftServer server, ViewObject view) {
73+
return new PlaceholderContext(server, server.getCommandSource(), null, null, null, null, view);
5174
}
5275

5376
public static PlaceholderContext of(GameProfile profile, MinecraftServer server) {
77+
return of(profile, server, ViewObject.DEFAULT);
78+
}
79+
80+
public static PlaceholderContext of(GameProfile profile, MinecraftServer server, ViewObject view) {
5481
var name = profile.getName() != null ? profile.getName() : profile.getId().toString();
5582
return new PlaceholderContext(server, new ServerCommandSource(CommandOutput.DUMMY, Vec3d.ZERO, Vec2f.ZERO, server.getOverworld(), server.getPermissionLevel(profile), name, Text.literal(name), server, null), null, null, null, profile);
5683
}
5784

5885
public static PlaceholderContext of(ServerPlayerEntity player) {
59-
return new PlaceholderContext(player.getServer(), player.getCommandSource(), player.getWorld(), player, player, player.getGameProfile());
86+
return of(player, ViewObject.DEFAULT);
87+
}
88+
89+
public static PlaceholderContext of(ServerPlayerEntity player, ViewObject view) {
90+
return new PlaceholderContext(player.getServer(), player.getCommandSource(), player.getWorld(), player, player, player.getGameProfile(), view);
6091
}
6192

6293
public static PlaceholderContext of(ServerCommandSource source) {
63-
return new PlaceholderContext(source.getServer(), source, source.getWorld(), source.getPlayer(), source.getEntity(), source.getPlayer() != null ? source.getPlayer().getGameProfile() : null);
94+
return of(source, ViewObject.DEFAULT);
95+
}
96+
97+
public static PlaceholderContext of(ServerCommandSource source, ViewObject view) {
98+
return new PlaceholderContext(source.getServer(), source, source.getWorld(), source.getPlayer(), source.getEntity(), source.getPlayer() != null ? source.getPlayer().getGameProfile() : null, view);
6499
}
65100

66101
public static PlaceholderContext of(Entity entity) {
102+
return of(entity, ViewObject.DEFAULT);
103+
}
104+
105+
public static PlaceholderContext of(Entity entity, ViewObject view) {
67106
if (entity instanceof ServerPlayerEntity player) {
68-
return of(player);
107+
return of(player, view);
69108
} else {
70-
return new PlaceholderContext(entity.getServer(), entity.getCommandSource(), (ServerWorld) entity.getWorld(), null, entity, null);
109+
return new PlaceholderContext(entity.getServer(), entity.getCommandSource(), (ServerWorld) entity.getWorld(), null, entity, null, view);
71110
}
72111
}
112+
113+
114+
public interface ViewObject {
115+
ViewObject DEFAULT = of(new Identifier("placeholder_api", "default"));
116+
117+
static ViewObject of(Identifier identifier) {
118+
return new ViewObjectImpl(identifier);
119+
}
120+
121+
Identifier identifier();
122+
}
73123
}

src/main/java/eu/pb4/placeholders/api/Placeholders.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,31 @@ public static PlaceholderResult parsePlaceholder(Identifier identifier, String a
6161
* @return Text
6262
*/
6363
public static ParentNode parseNodes(TextNode node) {
64-
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(node, PLACEHOLDER_PATTERN, PLACEHOLDER_GETTER, NodeParser.NOOP));
64+
return parseNodes(node, PlaceholderContext.KEY);
65+
}
66+
67+
public static ParentNode parseNodes(TextNode node, ParserContext.Key<PlaceholderContext> contextKey) {
68+
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(contextKey, node, PLACEHOLDER_PATTERN, PLACEHOLDER_GETTER, NodeParser.NOOP));
6569
}
6670

6771
public static ParentNode parseNodes(TextNode node, Pattern pattern) {
68-
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(node, pattern, PLACEHOLDER_GETTER, NodeParser.NOOP));
72+
return parseNodes(node, pattern, PlaceholderContext.KEY);
73+
}
74+
75+
public static ParentNode parseNodes(TextNode node, Pattern pattern, ParserContext.Key<PlaceholderContext> contextKey) {
76+
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(contextKey, node, pattern, PLACEHOLDER_GETTER, NodeParser.NOOP));
6977
}
7078

7179
public static ParentNode parseNodes(TextNode node, Pattern pattern, PlaceholderGetter placeholderGetter) {
72-
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(node, pattern, placeholderGetter, NodeParser.NOOP));
80+
return parseNodes(node, pattern, placeholderGetter, PlaceholderContext.KEY);
81+
}
82+
83+
public static ParentNode parseNodes(TextNode node, Pattern pattern, PlaceholderGetter placeholderGetter, ParserContext.Key<PlaceholderContext> contextKey) {
84+
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(contextKey, node, pattern, placeholderGetter, NodeParser.NOOP));
7385
}
7486

7587
public static ParentNode parseNodes(TextNode node, Pattern pattern, Map<String, Text> placeholders) {
76-
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(node, pattern, new PlaceholderGetter() {
88+
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(PlaceholderContext.KEY, node, pattern, new PlaceholderGetter() {
7789
@Override
7890
public PlaceholderHandler getPlaceholder(String placeholder) {
7991
return (ctx, arg) -> PlaceholderResult.value(placeholders.get(placeholder));
@@ -87,7 +99,11 @@ public boolean isContextOptional() {
8799
}
88100

89101
public static ParentNode parseNodes(TextNode node, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key) {
90-
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(node, pattern, new PlaceholderGetter() {
102+
return parseNodes(node, pattern, placeholders, key, PlaceholderContext.KEY);
103+
}
104+
105+
public static ParentNode parseNodes(TextNode node, Pattern pattern, Set<String> placeholders, ParserContext.Key<PlaceholderGetter> key, ParserContext.Key<PlaceholderContext> contextKey) {
106+
return new ParentNode(NodePlaceholderParserImpl.recursivePlaceholderParsing(contextKey, node, pattern, new PlaceholderGetter() {
91107
@Override
92108
public PlaceholderHandler getPlaceholder(String placeholder, ParserContext context) {
93109
var get = context.get(key);

src/main/java/eu/pb4/placeholders/api/parsers/MarkdownLiteParserV1.java

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public TextNode[] parseNodes(TextNode input) {
5858
if (input instanceof LiteralNode literalNode) {
5959
var list = new ArrayList<SubNode<?>>();
6060
parseLiteral(literalNode, list::add);
61-
return parseSubNodes(list.listIterator(), null, -1);
61+
return parseSubNodes(list.listIterator(), null, -1, false);
6262
} else if (input instanceof TranslatedNode translatedNode) {
6363
var list = new ArrayList<>();
6464
for (var arg : translatedNode.args()) {
@@ -78,7 +78,7 @@ public TextNode[] parseNodes(TextNode input) {
7878
list.add(new SubNode<>(SubNodeType.TEXT_NODE, TextNode.asSingle(parseNodes(children))));
7979
}
8080
}
81-
return new TextNode[]{parentTextNode.copyWith(parseSubNodes(list.listIterator(), null, -1), this)};
81+
return new TextNode[]{parentTextNode.copyWith(parseSubNodes(list.listIterator(), null, -1, false), this)};
8282
} else {
8383
return new TextNode[]{input};
8484
}
@@ -142,7 +142,7 @@ private void parseLiteral(LiteralNode literalNode, Consumer<SubNode<?>> consumer
142142
}
143143
}
144144

145-
private TextNode[] parseSubNodes(ListIterator<SubNode<?>> nodes, @Nullable SubNodeType endAt, int count) {
145+
private TextNode[] parseSubNodes(ListIterator<SubNode<?>> nodes, @Nullable SubNodeType endAt, int count, boolean requireEmpty) {
146146
var out = new ArrayList<TextNode>();
147147
int startIndex = nodes.nextIndex();
148148
var builder = new StringBuilder();
@@ -152,7 +152,16 @@ private TextNode[] parseSubNodes(ListIterator<SubNode<?>> nodes, @Nullable SubNo
152152
if (next.type == endAt) {
153153
int foundCount = 1;
154154

155-
if (foundCount == count) {
155+
boolean endingOrSpace;
156+
if (requireEmpty && nodes.hasNext()) {
157+
var prev = nodes.next();
158+
endingOrSpace = prev.type != SubNodeType.STRING || ((String) prev.value).startsWith(" ");
159+
nodes.previous();
160+
} else {
161+
endingOrSpace = true;
162+
}
163+
164+
if (foundCount == count && endingOrSpace) {
156165
if (!builder.isEmpty()) {
157166
out.add(new LiteralNode(builder.toString()));
158167
}
@@ -164,6 +173,14 @@ private TextNode[] parseSubNodes(ListIterator<SubNode<?>> nodes, @Nullable SubNo
164173
while (nodes.hasNext()) {
165174
if (nodes.next().type == endAt) {
166175
if ((++foundCount) == count) {
176+
if (requireEmpty && nodes.hasNext()) {
177+
var prev = nodes.next();
178+
nodes.previous();
179+
if (prev.type == SubNodeType.STRING && !((String) prev.value).startsWith(" ")) {
180+
break;
181+
}
182+
}
183+
167184
if (!builder.isEmpty()) {
168185
out.add(new LiteralNode(builder.toString()));
169186
}
@@ -190,7 +207,7 @@ private TextNode[] parseSubNodes(ListIterator<SubNode<?>> nodes, @Nullable SubNo
190207
builder.append((String) next.value);
191208
continue;
192209
} else if (next.type == SubNodeType.BACK_TICK && this.allowedFormatting.contains(MarkdownFormat.QUOTE)) {
193-
var value = parseSubNodes(nodes, next.type, 1);
210+
var value = parseSubNodes(nodes, next.type, 1, false);
194211

195212
if (value != null) {
196213
if (!builder.isEmpty()) {
@@ -201,7 +218,7 @@ private TextNode[] parseSubNodes(ListIterator<SubNode<?>> nodes, @Nullable SubNo
201218
continue;
202219
}
203220
} else if (next.type == SubNodeType.SPOILER_LINE && this.allowedFormatting.contains(MarkdownFormat.SPOILER)) {
204-
var value = parseSubNodes(nodes, next.type, 1);
221+
var value = parseSubNodes(nodes, next.type, 1, false);
205222

206223
if (value != null) {
207224
if (!builder.isEmpty()) {
@@ -212,7 +229,7 @@ private TextNode[] parseSubNodes(ListIterator<SubNode<?>> nodes, @Nullable SubNo
212229
continue;
213230
}
214231
} else if (next.type == SubNodeType.DOUBLE_WAVY_LINE && this.allowedFormatting.contains(MarkdownFormat.STRIKETHROUGH)) {
215-
var value = parseSubNodes(nodes, next.type, 1);
232+
var value = parseSubNodes(nodes, next.type, 1, false);
216233

217234
if (value != null) {
218235
if (!builder.isEmpty()) {
@@ -232,7 +249,7 @@ private TextNode[] parseSubNodes(ListIterator<SubNode<?>> nodes, @Nullable SubNo
232249
if (nexter.type == next.type) {
233250
two = true;
234251
var i = nodes.nextIndex();
235-
var value = parseSubNodes(nodes, next.type, 2);
252+
var value = parseSubNodes(nodes, next.type, 2, false);
236253

237254
if (value != null) {
238255
if (!builder.isEmpty()) {
@@ -248,15 +265,26 @@ private TextNode[] parseSubNodes(ListIterator<SubNode<?>> nodes, @Nullable SubNo
248265
}
249266

250267
if (!two && this.allowedFormatting.contains(MarkdownFormat.ITALIC)) {
251-
var value = parseSubNodes(nodes, next.type, 1);
268+
boolean startingOrSpace;
269+
if (nodes.hasPrevious()) {
270+
var prev = nodes.previous();
271+
startingOrSpace = prev.type != SubNodeType.STRING || ((String) prev.value).endsWith(" ");
272+
nodes.next();
273+
} else {
274+
startingOrSpace = true;
275+
}
276+
277+
if (startingOrSpace) {
278+
var value = parseSubNodes(nodes, next.type, 1, next.type == SubNodeType.FLOOR);
252279

253-
if (value != null) {
254-
if (!builder.isEmpty()) {
255-
out.add(new LiteralNode(builder.toString()));
256-
builder = new StringBuilder();
280+
if (value != null) {
281+
if (!builder.isEmpty()) {
282+
out.add(new LiteralNode(builder.toString()));
283+
builder = new StringBuilder();
284+
}
285+
out.add(new FormattingNode(value, Formatting.ITALIC));
286+
continue;
257287
}
258-
out.add(new FormattingNode(value, Formatting.ITALIC));
259-
continue;
260288
}
261289
}
262290
}

0 commit comments

Comments
 (0)