-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I parse/save header/comment correctly? #53
Comments
Also, when I get/set a comment, the empty comment lines and blank lines are removed, is there any way to keep it? For example: after use yaml.setComment("option", "New Comment") the config will become: # Example Config
# Generated by {version}
# check update
check_update: true
# New Comment
option: 1 Apparently, the blank line above New Comment was removed. Also, it also strips my comment formatting, for example: ############### Example #####################
#
# Test
#
########################################### it will become: # Example
# Test |
Currently, the header is considered to be the block comment of the first entry (root), without stripping You can get the header you want with something like: yaml.options().header().split("\n\n")[0] The current header behaviour is mostly the same as the original Bukkit YamlConfiguration. But maybe some rework can be done to achieve this most intuitive behaviour. |
thanks, how about the comment issue? |
I think it is not possible at the moment to add blank lines along with a comment programmatically. Current blank lines that exist in the file will be preserved untouched, but if you use Currently, the formatted comments like the one you provide are supposed to be written on the default file manually and not programmatically using You can set blank lines as a comment with yaml.setComment("option", "\nNew Comment"); The output will be: # Example Config
# Generated by {version}
# check update
check_update: true
#
# New Comment
option: 1 If the comment begins with a Currently adding a yaml.setComment("option", "One line\nAnother line"); # Example Config
# Generated by {version}
# check update
check_update: true
# One line
# Another line
option: 1
|
The 1.8 release includes a comment formatting feature useful to control how you want to format and retrieve comments. This is how to do what you want after upgrading: // Create YamlFile with relative path
final YamlFile yamlFile = new YamlFile("issue-53.yml");
// Load file with comments
try {
yamlFile.createOrLoadWithComments();
} catch (IOException e) {
e.printStackTrace();
}
// Set header format
yamlFile.options().headerFormatter()
.prefixFirst("############### Example #####################\n#")
.suffixLast("\n#\n#############################################");
// Set header
yamlFile.options().header("Example Config\nGenerated by {version}");
// Set comment formatter
// DEFAULT (unset) does not add any blank line
// PRETTY will add a blank line above comments at root keys (those with indent 0)
yamlFile.setCommentFormat(YamlCommentFormat.PRETTY);
// Add values along with comments using the alternative API (path method)
yamlFile.path("check_update").set(true).comment("check update");
yamlFile.path("option").set(1).comment("another option");
// Get header
System.out.println("Header without format: \n\n" + yamlFile.options().header());
System.out.println();
System.out.println("Header with format: \n\n" + yamlFile.getHeader());
System.out.println();
// Get first comment
System.out.println("check_update comment: " + yamlFile.getComment("check_update"));
// Save file
try {
yamlFile.save();
} catch (IOException e) {
e.printStackTrace();
} Output
issue-53.yml ############### Example #####################
#
# Example Config
# Generated by {version}
#
#############################################
# check update
check_update: true
# another option
option: 1 Another way to set blank lines (explicitly): // Create YamlFile with relative path
final YamlFile yamlFile = new YamlFile("issue-53-2.yml");
// Load file with comments
try {
yamlFile.createOrLoadWithComments();
} catch (IOException e) {
e.printStackTrace();
}
// If no comment format is set, it is equivalent to:
// yamlFile.setCommentFormat(YamlCommentFormat.DEFAULT);
// Add some values
yamlFile.set("option-1", 1);
yamlFile.set("option-2", 2);
yamlFile.set("nested.option-3", 3);
yamlFile.set("nested.option-4", 4);
// This does not add any blank line (DEFAULT format)
yamlFile.setComment("option-1", "DEFAULT Comment");
// If each comment line is prefixed with # then the default formatter is ignored (RAW format)
yamlFile.setComment("option-2", "\n# RAW Comment");
// Add a blank line without comment
yamlFile.setBlankLine("nested");
// This is a multiline comment, both lines with # prefix (DEFAULT format)
yamlFile.setComment("nested.option-3", "\nDEFAULT Comment with new line");
// This will add a blank line above (BLANK_LINE format)
yamlFile.setComment("nested.option-4", "BLANK_LINE Comment", YamlCommentFormat.BLANK_LINE);
// Save file
try {
yamlFile.save();
} catch (IOException e) {
e.printStackTrace();
} issue-53-2.yml # DEFAULT Comment
option-1: 1
# RAW Comment
option-2: 2
nested:
#
# DEFAULT Comment with new line
option-3: 3
# BLANK_LINE Comment
option-4: 4 More information: Another example with custom comment formatting with custom prefix and suffix: |
I'm closing this issue as I think is resolved. If you have any question or encounter a related problem with this issue feel free to ask or reopen it. |
Suppose I have a yaml config:
yaml.option().header()
will return:This is obviously not what I need 🤔
yaml.getComment("check_update")
also returnsIs there any way to separate them?
The text was updated successfully, but these errors were encountered: