Web help refinement.

This commit is contained in:
StevenLawson 2013-08-27 13:49:45 -04:00
parent be87075337
commit 08a9329864
4 changed files with 67 additions and 18 deletions

View File

@ -18,6 +18,11 @@ public class HTMLGenerationTools
return "<p>" + escapeHtml4(data) + "</p>\r\n";
}
public static String heading(String data, int level)
{
return "<h" + level + ">" + escapeHtml4(data) + "</h" + level + ">\r\n";
}
public static <K, V> String list(Map<K, V> map)
{
StringBuilder output = new StringBuilder();

View File

@ -13,6 +13,8 @@ import org.bukkit.command.CommandMap;
import org.bukkit.command.PluginIdentifiableCommand;
import static me.StevenLawson.TotalFreedomMod.HTTPD.HTMLGenerationTools.*;
import static org.apache.commons.lang3.StringEscapeUtils.*;
import org.apache.commons.lang3.StringUtils;
public class Module_help extends TFM_HTTPD_Module
{
@ -34,6 +36,8 @@ public class Module_help extends TFM_HTTPD_Module
return paragraph("Error loading commands.");
}
responseBody.append(heading("Command Help Index", 1));
final Map<String, List<Command>> commandsByPlugin = new HashMap<String, List<Command>>();
final Iterator<Map.Entry<String, Command>> itKnownCommands = knownCommands.entrySet().iterator();
@ -74,15 +78,30 @@ public class Module_help extends TFM_HTTPD_Module
}
});
List<String> descriptions = new ArrayList<String>();
responseBody.append(heading(pluginName, 2)).append("<ul>\r\n");
for (Command command : commands)
{
descriptions.add(command.getName() + " (" + command.getUsage().replace("<command>", command.getName()).trim() + "): " + command.getDescription());
responseBody
.append("<li><div><span class=\"commandName\">")
.append(escapeHtml4(command.getName()))
.append("</span> - Usage: <span class=\"commandUsage\">")
.append(escapeHtml4(command.getUsage().replace("<command>", command.getName()).trim()))
.append("</span>");
if (!command.getAliases().isEmpty())
{
responseBody.append(" - Aliases: <span class=\"commandAliases\">")
.append(escapeHtml4(StringUtils.join(command.getAliases(), ", ")))
.append("</span>");
}
responseBody
.append(paragraph(pluginName))
.append(list(descriptions));
responseBody.append("<br /><span class=\"commandDescription\">")
.append(escapeHtml4(command.getDescription()))
.append("</span></div></li>\r\n");
}
responseBody.append("</ul>\r\n");
}
return responseBody.toString();
@ -93,4 +112,10 @@ public class Module_help extends TFM_HTTPD_Module
{
return "TotalFreedomMod :: WebHelp";
}
@Override
public String getStyle()
{
return ".commandName{font-weight:bold;}.commandDescription{padding-left:15px;}li{margin:.15em;padding:.15em;}";
}
}

View File

@ -24,8 +24,13 @@ public abstract class TFM_HTTPD_Module
public abstract String getTitle();
public String getStyle()
{
return "";
}
public final Response getResponse()
{
return new TFM_HTTPD_PageBuilder(getBody(), getTitle()).getResponse();
return new TFM_HTTPD_PageBuilder(getBody(), getTitle(), getStyle()).getResponse();
}
}

View File

@ -5,26 +5,29 @@ import me.StevenLawson.TotalFreedomMod.HTTPD.NanoHTTPD.Response;
public class TFM_HTTPD_PageBuilder
{
private static final String TEMPLATE =
"<!DOCTYPE html>\n"
+ "<html>\n"
+ "<head>\n"
+ "<title>{$TITLE}</title>\n"
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
+ "</head>\n"
+ "<body>{$BODY}</body>\n"
+ "</html>\n";
"<!DOCTYPE html>\r\n"
+ "<html>\r\n"
+ "<head>\r\n"
+ "<title>{$TITLE}</title>\r\n"
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"
+ "<style type=\"text/css\">{$STYLE}</style>\r\n"
+ "</head>\r\n"
+ "<body>{$BODY}</body>\r\n"
+ "</html>\r\n";
//
private String body;
private String title;
private String body = "";
private String title = "";
private String style = "";
public TFM_HTTPD_PageBuilder()
{
}
public TFM_HTTPD_PageBuilder(String body, String title)
public TFM_HTTPD_PageBuilder(String body, String title, String style)
{
this.body = body;
this.title = title;
this.style = style;
}
public void setBody(String body)
@ -37,8 +40,19 @@ public class TFM_HTTPD_PageBuilder
this.title = title;
}
public void setStyle(String style)
{
this.style = style;
}
public Response getResponse()
{
return new Response(TEMPLATE.replace("{$TITLE}", title).replace("{$BODY}", body));
return new Response(this.toString());
}
@Override
public String toString()
{
return TEMPLATE.replace("{$BODY}", body).replace("{$TITLE}", title).replace("{$STYLE}", style);
}
}