From e5e7e04c2bda51a26a2f66df39676ebc27024918 Mon Sep 17 00:00:00 2001 From: ljacqu Date: Sat, 26 Dec 2015 14:00:15 +0100 Subject: [PATCH] Fix #336 Create task to generate a commands page - Fix logic for optional tags - Fix file output format - Add generated file to repository --- src/tools/commands/CommandPageCreater.java | 38 ++++++--- src/tools/commands/command_entry.tpl.md | 4 +- src/tools/commands/commands.tpl.md | 8 +- src/tools/docs/commands.md | 91 ++++++++++++++++++++++ src/tools/utils/TagReplacer.java | 27 ++++++- 5 files changed, 149 insertions(+), 19 deletions(-) create mode 100644 src/tools/docs/commands.md diff --git a/src/tools/commands/CommandPageCreater.java b/src/tools/commands/CommandPageCreater.java index 4d74dfbae..8f91b08e2 100644 --- a/src/tools/commands/CommandPageCreater.java +++ b/src/tools/commands/CommandPageCreater.java @@ -12,12 +12,15 @@ import utils.TagReplacer; import utils.ToolTask; import utils.ToolsConstants; +import java.util.Collection; import java.util.Map; import java.util.Scanner; import java.util.Set; public class CommandPageCreater implements ToolTask { + private static final String OUTPUT_FILE = ToolsConstants.DOCS_FOLDER + "commands.md"; + @Override public String getTaskName() { return "createCommandPage"; @@ -30,20 +33,33 @@ public class CommandPageCreater implements ToolTask { + "commands/command_entry.tpl.md"); StringBuilder commandsResult = new StringBuilder(); - for (CommandDescription command : baseCommands) { + addCommandsInfo(commandsResult, baseCommands, template, true); + + FileUtils.generateFileFromTemplate( + ToolsConstants.TOOLS_SOURCE_ROOT + "commands/commands.tpl.md", + OUTPUT_FILE, + ANewMap.with("commands", commandsResult.toString()).build()); + System.out.println("Wrote to '" + OUTPUT_FILE + "' with " + baseCommands.size() + " base commands."); + } + + private static void addCommandsInfo(StringBuilder sb, Collection commands, + final String template, boolean addNewLine) { + for (CommandDescription command : commands) { Map tags = ANewMap .with("command", CommandUtils.constructCommandPath(command)) .and("description", command.getDetailedDescription()) .and("arguments", formatArguments(command.getArguments())) .and("permissions", formatPermissions(command.getCommandPermissions())) .build(); - commandsResult.append(TagReplacer.applyReplacements(template, tags)); - } + sb.append(TagReplacer.applyReplacements(template, tags)); - FileUtils.generateFileFromTemplate( - ToolsConstants.TOOLS_SOURCE_ROOT + "commands/commands.tpl.md", - ToolsConstants.DOCS_FOLDER + "commands.md", - ANewMap.with("commands", commandsResult.toString()).build()); + if (!command.getChildren().isEmpty()) { + addCommandsInfo(sb, command.getChildren(), template, false); + } + if (addNewLine) { + sb.append("\n\n"); + } + } } private static String formatPermissions(CommandPermissions permissions) { @@ -54,16 +70,16 @@ public class CommandPageCreater implements ToolTask { for (PermissionNode node : permissions.getPermissionNodes()) { result += node.getNode() + " "; } - return result; + return result.trim(); } private static String formatArguments(Iterable arguments) { StringBuilder result = new StringBuilder(); for (CommandArgumentDescription argument : arguments) { String argumentName = argument.isOptional() - ? "[" + argument.getDescription() + "]" - : "<" + argument.getDescription() + ">"; - result.append(argumentName).append(" "); + ? "[" + argument.getName() + "]" + : "<" + argument.getName() + ">"; + result.append(" ").append(argumentName); } return result.toString(); } diff --git a/src/tools/commands/command_entry.tpl.md b/src/tools/commands/command_entry.tpl.md index c39b1deb1..c29784848 100644 --- a/src/tools/commands/command_entry.tpl.md +++ b/src/tools/commands/command_entry.tpl.md @@ -1,2 +1,2 @@ -{command}: {description} _{arguments}_ -[permissions]Permission: {permissions}[/permissions] +- **{command}**{arguments}: {description}[permissions] +
Requires `{permissions}`[/permissions] diff --git a/src/tools/commands/commands.tpl.md b/src/tools/commands/commands.tpl.md index f2334c1b5..c983ba4ce 100644 --- a/src/tools/commands/commands.tpl.md +++ b/src/tools/commands/commands.tpl.md @@ -1,4 +1,8 @@ -## AuthMe commands -You can use the following commands to use the functions of AuthMe: + + + +## AuthMe Commands +You can use the following commands to use the features of AuthMe. Mandatory arguments are marked with `< >` +brackets; optional arguments are enclosed in square brackets (`[ ]`). {commands} diff --git a/src/tools/docs/commands.md b/src/tools/docs/commands.md new file mode 100644 index 000000000..6000ffcb0 --- /dev/null +++ b/src/tools/docs/commands.md @@ -0,0 +1,91 @@ + + + +## AuthMe Commands +You can use the following commands to use the features of AuthMe. Mandatory arguments are marked with `< >` +brackets; optional arguments are enclosed in square brackets (`[ ]`). + +- **/authme**: The main AuthMeReloaded command. The root for all admin commands. +- **/authme help** [query]: View detailed help pages about AuthMeReloaded commands. +- **/authme register** <player> <password>: Register the specified player with the specified password. +
Requires `authme.admin.register` +- **/authme unregister** <player>: Unregister the specified player. +
Requires `authme.admin.unregister` +- **/authme forcelogin** [player]: Enforce the specified player to login. +
Requires `authme.player.canbeforced` +- **/authme password** <player> <pwd>: Change the password of a player. +
Requires `authme.admin.changepassword` +- **/authme lastlogin** [player]: View the date of the specified players last login. +
Requires `authme.admin.lastlogin` +- **/authme accounts** [player]: Display all accounts of a player by his player name or IP. +
Requires `authme.admin.accounts` +- **/authme getemail** [player]: Display the email address of the specified player if set. +
Requires `authme.admin.getemail` +- **/authme chgemail** <player> <email>: Change the email address of the specified player. +
Requires `authme.admin.changemail` +- **/authme getip** <player>: Get the IP address of the specified online player. +
Requires `authme.admin.getip` +- **/authme spawn** <player>: Teleport to the spawn. +
Requires `authme.admin.spawn` +- **/authme setspawn**: Change the player's spawn to your current position. +
Requires `authme.admin.setspawn` +- **/authme firstspawn**: Teleport to the first spawn. +
Requires `authme.admin.firstspawn` +- **/authme setfirstspawn**: Change the first player's spawn to your current position. +
Requires `authme.admin.setfirstspawn` +- **/authme purge** <days>: Purge old AuthMeReloaded data longer than the specified amount of days ago. +
Requires `authme.admin.purge` +- **/authme resetpos** <player>: Purge the last know position of the specified player. +
Requires `authme.admin.purgelastpos` +- **/authme purgebannedplayers**: Purge all AuthMeReloaded data for banned players. +
Requires `authme.admin.purgebannedplayers` +- **/authme switchantibot** [mode]: Switch or toggle the AntiBot mode to the specified state. +
Requires `authme.admin.switchantibot` +- **/authme reload**: Reload the AuthMeReloaded plugin. +
Requires `authme.admin.reload` +- **/authme version**: Show detailed information about the installed AuthMeReloaded version, the developers, contributors, and license. + + +- **/login** <password>: Command to log in using AuthMeReloaded. +
Requires `authme.player.login` +- **/login help** [query]: View detailed help pages about AuthMeReloaded login commands. + + +- **/logout**: Command to logout using AuthMeReloaded. +
Requires `authme.player.logout` +- **/logout help** [query]: View detailed help pages about AuthMeReloaded logout commands. + + +- **/register** <password> [verifyPassword]: Command to register using AuthMeReloaded. +
Requires `authme.player.register` +- **/register help** [query]: View detailed help pages about AuthMeReloaded register commands. + + +- **/unreg** <password>: Command to unregister using AuthMeReloaded. +
Requires `authme.player.unregister` +- **/unreg help** [query]: View detailed help pages about AuthMeReloaded unregister commands. + + +- **/changepassword** <oldPassword> <newPassword>: Command to change your password using AuthMeReloaded. +
Requires `authme.player.changepassword` +- **/changepassword help** [query]: View detailed help pages about AuthMeReloaded changepassword commands. + + +- **/email**: The AuthMeReloaded Email command base. +- **/email help** [query]: View detailed help pages about AuthMeReloaded email commands. +- **/email add** <email> <verifyEmail>: Add a new email address to your account. +
Requires `authme.player.email.add` +- **/email change** <oldEmail> <newEmail>: Change an email address of your account. +
Requires `authme.player.email.change` +- **/email recover** <email>: Recover your account using an Email address by sending a mail containing a new password. +
Requires `authme.player.email.recover` + + +- **/captcha** <captcha>: Captcha command for AuthMeReloaded. +
Requires `authme.player.captcha` +- **/captcha help** [query]: View detailed help pages about AuthMeReloaded captcha commands. + + +- **/converter** <job>: Converter command for AuthMeReloaded. +
Requires `authme.admin.converter` +- **/converter help** [query]: View detailed help pages about AuthMeReloaded converter commands. diff --git a/src/tools/utils/TagReplacer.java b/src/tools/utils/TagReplacer.java index 29a9b91c0..fedab4f90 100644 --- a/src/tools/utils/TagReplacer.java +++ b/src/tools/utils/TagReplacer.java @@ -4,6 +4,8 @@ import fr.xephi.authme.util.StringUtils; import java.util.Date; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * Class responsible for replacing template tags to actual content. @@ -15,6 +17,9 @@ import java.util.Map; */ public class TagReplacer { + private TagReplacer() { + } + /** * Replace a template with default tags and custom ones supplied by a map. * @@ -29,11 +34,9 @@ public class TagReplacer { final String name = tagRule.getKey(); final String value = tagRule.getValue(); - String replacement = StringUtils.isEmpty(value) ? "" : "\\1"; - result = result.replaceAll("\\[" + name + "\\](.*?)\\[/" + name + "\\]", replacement); - result = result.replace("{" + tagRule.getKey() + "}", tagRule.getValue()); + result = replaceOptionalTag(result, name, value) + .replace("{" + name + "}", value); } - return applyReplacements(result); } @@ -49,5 +52,21 @@ public class TagReplacer { .replace("{gen_warning}", "AUTO-GENERATED FILE! Do not edit this directly"); } + private static String replaceOptionalTag(String text, String tagName, String tagValue) { + Pattern regex = Pattern.compile("\\[" + tagName + "\\](.*?)\\[/" + tagName + "\\]", Pattern.DOTALL); + Matcher matcher = regex.matcher(text); + + if (!matcher.find()) { + // Couldn't find results, so just return text as it is + return text; + } else if (StringUtils.isEmpty(tagValue)) { + // Tag is empty, replace [tagName]some_text[/tagName] to nothing + return matcher.replaceAll(""); + } else { + // Tag is not empty, so replace [tagName]some_text[/tagName] to some_text + return matcher.replaceAll(matcher.group(1)); + } + } + }