NIHVIVO-564 Modifications to directive help output. Included changing template directive iface to an abstract class that provides a default help() method and some utilities.
This commit is contained in:
parent
677dfb18d0
commit
5e8eb80732
7 changed files with 95 additions and 54 deletions
|
@ -0,0 +1,38 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.directives;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.controller.freemarker.FreemarkerHelper;
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateDirectiveModel;
|
||||
|
||||
public abstract class BaseTemplateDirectiveModel implements TemplateDirectiveModel {
|
||||
|
||||
public String help(Configuration config) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String name = getDirectiveName();
|
||||
map.put("name", name);
|
||||
|
||||
return mergeToTemplate(map, config);
|
||||
}
|
||||
|
||||
protected String getDirectiveName() {
|
||||
String className = this.getClass().getName();
|
||||
String[] nameParts = className.split("\\.");
|
||||
String directiveName = nameParts[nameParts.length-1];
|
||||
directiveName = directiveName.replaceAll("Directive$", "");
|
||||
directiveName = directiveName.substring(0, 1).toLowerCase() + directiveName.substring(1);
|
||||
return directiveName;
|
||||
}
|
||||
|
||||
protected String mergeToTemplate(Map<String, Object> map, Configuration config) {
|
||||
return new FreemarkerHelper().mergeMapToTemplate("directive-help.ftl", map, config);
|
||||
}
|
||||
|
||||
}
|
|
@ -25,7 +25,7 @@ import freemarker.template.TemplateModel;
|
|||
import freemarker.template.TemplateModelException;
|
||||
import freemarker.template.utility.DeepUnwrap;
|
||||
|
||||
public class DumpDataModelDirective implements VitroTemplateDirectiveModel {
|
||||
public class DumpDataModelDirective extends BaseTemplateDirectiveModel {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DumpDataModelDirective.class);
|
||||
|
||||
|
@ -61,8 +61,8 @@ public class DumpDataModelDirective implements VitroTemplateDirectiveModel {
|
|||
// Send the two lists of strings (variables and directives) to dump-datamodel.ftl.
|
||||
// That way, the directive dump won't be broken up into two pieces, for example.
|
||||
Object value = dm.get(var);
|
||||
if (value instanceof VitroTemplateDirectiveModel) {
|
||||
String help = ((VitroTemplateDirectiveModel) value).help(config);
|
||||
if (value instanceof BaseTemplateDirectiveModel) {
|
||||
String help = ((BaseTemplateDirectiveModel) value).help(config);
|
||||
directives.put(var, help);
|
||||
} else {
|
||||
models.put(var, value);
|
||||
|
@ -89,18 +89,22 @@ public class DumpDataModelDirective implements VitroTemplateDirectiveModel {
|
|||
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public String help(Configuration config) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String name = getDirectiveName();
|
||||
map.put("name", name);
|
||||
|
||||
map.put("usage", "Dump the contents of the template data model.");
|
||||
|
||||
map.put("comments", "Sequences (lists and arrays) are enclosed in square brackets. Hashes are enclosed in curly braces.");
|
||||
|
||||
List<String> examples = new ArrayList<String>();
|
||||
examples.add("<@dumpDataModel />");
|
||||
examples.add("<@" + name + " />");
|
||||
map.put("examples", examples);
|
||||
|
||||
return new FreemarkerHelper().mergeMapToTemplate("dump-directive-help.ftl", map, config);
|
||||
return mergeToTemplate(map, config);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ import freemarker.template.TemplateNumberModel;
|
|||
import freemarker.template.TemplateScalarModel;
|
||||
import freemarker.template.TemplateSequenceModel;
|
||||
|
||||
public class DumpDirective implements VitroTemplateDirectiveModel {
|
||||
public class DumpDirective extends BaseTemplateDirectiveModel {
|
||||
|
||||
private static final Log log = LogFactory.getLog(DumpDirective.class);
|
||||
|
||||
|
@ -116,6 +116,10 @@ public class DumpDirective implements VitroTemplateDirectiveModel {
|
|||
|
||||
public String help(Configuration config) {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
String name = getDirectiveName();
|
||||
map.put("name", name);
|
||||
|
||||
map.put("usage", "Dump the contents of a template variable.");
|
||||
|
||||
map.put("comments", "Sequences (lists and arrays) are enclosed in square brackets. Hashes are enclosed in curly braces.");
|
||||
|
@ -125,10 +129,10 @@ public class DumpDirective implements VitroTemplateDirectiveModel {
|
|||
map.put("params", params);
|
||||
|
||||
List<String> examples = new ArrayList<String>();
|
||||
examples.add("<@dump var=\"urls\" />");
|
||||
examples.add("<@" + name + " var=\"urls\" />");
|
||||
map.put("examples", examples);
|
||||
|
||||
return new FreemarkerHelper().mergeMapToTemplate("dump-directive-help.ftl", map, config);
|
||||
return mergeToTemplate(map, config);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,12 +0,0 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package edu.cornell.mannlib.vitro.webapp.web.directives;
|
||||
|
||||
import freemarker.template.Configuration;
|
||||
import freemarker.template.TemplateDirectiveModel;
|
||||
|
||||
public interface VitroTemplateDirectiveModel extends TemplateDirectiveModel {
|
||||
|
||||
public String help(Configuration config);
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue