Skip to content

Commit

Permalink
add yaml loadtext and to_text, fixes #1853
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 17, 2019
1 parent cc7d572 commit b5d7d7b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
Expand Up @@ -4461,7 +4461,7 @@ public void registerCoreMembers() {

// <--[command]
// @Name Yaml
// @Syntax yaml [create]/[load:<file> (fix_formatting)]/[unload]/[savefile:<file>]/[set <key>([<#>])(:<action>):<value>] [id:<name>]
// @Syntax yaml [create]/[load:<file> (fix_formatting)]/[loadtext:<text> (fix_formatting)]/[unload]/[savefile:<file>]/[set <key>([<#>])(:<action>):<value>] [id:<name>]
// @Required 2
// @Stable stable
// @Short Edits a YAML configuration file.
Expand Down Expand Up @@ -4513,7 +4513,7 @@ public void registerCoreMembers() {
// - yaml id:myfile set my.key[2]:hello
// -->
registerCoreMember(YamlCommand.class,
"YAML", "yaml [create]/[load:<file> (fix_formatting)]/[unload]/[savefile:<file>]/[set <key>([<#>])(:<action>):<value>] [id:<name>]", 2);
"YAML", "yaml [create]/[load:<file> (fix_formatting)]/[loadtext:<text> (fix_formatting)]/[unload]/[savefile:<file>]/[set <key>([<#>])(:<action>):<value>] [id:<name>]", 2);

// <--[command]
// @Name Zap
Expand Down
Expand Up @@ -46,7 +46,7 @@ private YamlConfiguration getYaml(String id) {
return yamls.get(id.toUpperCase());
}

public static enum Action {LOAD, UNLOAD, CREATE, WRITE, SAVE, SET}
public static enum Action {LOAD, LOADTEXT, UNLOAD, CREATE, WRITE, SAVE, SET}

public static enum YAML_Action {
SET_VALUE, INCREASE, DECREASE, MULTIPLY,
Expand All @@ -64,6 +64,11 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
scriptEntry.addObject("action", new Element("LOAD"));
scriptEntry.addObject("filename", arg.asElement());
}
if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("LOADTEXT")) {
scriptEntry.addObject("action", new Element("LOADTEXT"));
scriptEntry.addObject("raw_text", arg.asElement());
}
else if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("SAVEFILE", "FILESAVE")) {
scriptEntry.addObject("action", new Element("SAVE"));
Expand Down Expand Up @@ -205,6 +210,7 @@ else if (flagArgs[1].equals("//") || flagArgs[1].equals("/")) {
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {

Element filename = scriptEntry.getElement("filename");
Element rawText = scriptEntry.getElement("raw_text");
Element key = scriptEntry.getElement("key");
dObject value = scriptEntry.getdObject("value");
Element split = scriptEntry.getElement("split");
Expand All @@ -225,6 +231,7 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept
+ (key != null ? key.debug() : "")
+ (value != null ? value.debug() : "")
+ (split != null ? split.debug() : "")
+ (rawText != null ? rawText.debug() : "")
+ fixFormatting.debug());

}
Expand Down Expand Up @@ -295,6 +302,19 @@ public void run() {
}
break;

case LOADTEXT:
String str = rawText.asString();
if (fixFormatting.asBoolean()) {
str = ScriptHelper.ClearComments("", str, false);
}
YamlConfiguration config = YamlConfiguration.load(str);
if (yamls.containsKey(id)) {
yamls.remove(id);
}
yamls.put(id, config);
scriptEntry.setFinished(true);
break;

case UNLOAD:
if (yamls.containsKey(id)) {
yamls.remove(id);
Expand Down Expand Up @@ -696,7 +716,7 @@ public void yaml(ReplaceableTagEvent event) {

// <--[tag]
// @attribute <yaml[<id>].to_json>
// @returns dList
// @returns Element
// @description
// Converts the YAML container to a JSON array.
// -->
Expand All @@ -705,5 +725,16 @@ public void yaml(ReplaceableTagEvent event) {
event.setReplaced(new Element(jsobj.toString()).getAttribute(attribute.fulfill(1)));
return;
}

// <--[tag]
// @attribute <yaml[<id>].to_text>
// @returns Element
// @description
// Converts the YAML container to raw YAML text.
// -->
if (attribute.startsWith("to_text")) {
event.setReplaced(new Element(getYaml(id).saveToString()).getAttribute(attribute.fulfill(1)));
return;
}
}
}

0 comments on commit b5d7d7b

Please sign in to comment.