From 8ccac59d4a453ee55c9c3029837bf84e81a2b591 Mon Sep 17 00:00:00 2001 From: rjy7 Date: Thu, 29 Jul 2010 15:26:37 +0000 Subject: [PATCH] NIHVIVO-564 data model dump: add help for directives in the data model --- .../directives/DumpDataModelDirective.java | 31 +++++++++++++++---- .../webapp/web/directives/DumpDirective.java | 22 +++++++++++-- .../VitroTemplateDirectiveModel.java | 10 ++++++ webapp/web/css/dump.css | 4 +++ .../body/partials/dump/dump-datamodel.ftl | 13 +++++--- .../partials/dump/dump-directive-help.ftl | 29 +++++++++++++++++ 6 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/VitroTemplateDirectiveModel.java create mode 100644 webapp/web/templates/freemarker/body/partials/dump/dump-directive-help.ftl diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java index 6bbe404f8..3000b98e0 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDataModelDirective.java @@ -25,7 +25,7 @@ import freemarker.template.TemplateModel; import freemarker.template.TemplateModelException; import freemarker.template.utility.DeepUnwrap; -public class DumpDataModelDirective implements TemplateDirectiveModel { +public class DumpDataModelDirective implements VitroTemplateDirectiveModel { private static final Log log = LogFactory.getLog(DumpDataModelDirective.class); @@ -47,23 +47,28 @@ public class DumpDataModelDirective implements TemplateDirectiveModel { "The dumpDataModel directive doesn't allow nested content."); } + Configuration config = env.getConfiguration(); TemplateHashModel dataModel = env.getDataModel(); Map models = new HashMap(); - List directives = new ArrayList(); + Map directives = new HashMap(); Map dm = (Map) DeepUnwrap.permissiveUnwrap(dataModel); List varNames = new ArrayList(dm.keySet()); Collections.sort(varNames); for (String var : varNames) { + // RY Instead, push each var/directive through the template and return a string. + // The meat of dumpDirective will go in a helper. + // 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 TemplateDirectiveModel) { - directives.add((String) var); + if (value instanceof VitroTemplateDirectiveModel) { + String help = ((VitroTemplateDirectiveModel) value).help(config); + directives.put(var, help); } else { models.put(var, value); } } - - Configuration config = env.getConfiguration(); + String templateName = "dump-datamodel.ftl"; Map map = new HashMap(); @@ -84,4 +89,18 @@ public class DumpDataModelDirective implements TemplateDirectiveModel { } + @Override + public String help(Configuration config) { + Map map = new HashMap(); + 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 examples = new ArrayList(); + examples.add("<@dumpDataModel />"); + map.put("examples", examples); + + return new FreemarkerHelper().mergeMapToTemplate("dump-directive-help.ftl", map, config); + } + } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java index 77846011e..7bb51b496 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/DumpDirective.java @@ -4,7 +4,9 @@ package edu.cornell.mannlib.vitro.webapp.web.directives; import java.io.IOException; import java.io.Writer; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; @@ -17,7 +19,6 @@ import freemarker.template.SimpleScalar; import freemarker.template.TemplateBooleanModel; import freemarker.template.TemplateDateModel; import freemarker.template.TemplateDirectiveBody; -import freemarker.template.TemplateDirectiveModel; import freemarker.template.TemplateException; import freemarker.template.TemplateHashModel; import freemarker.template.TemplateModel; @@ -26,7 +27,7 @@ import freemarker.template.TemplateNumberModel; import freemarker.template.TemplateScalarModel; import freemarker.template.TemplateSequenceModel; -public class DumpDirective implements TemplateDirectiveModel { +public class DumpDirective implements VitroTemplateDirectiveModel { private static final Log log = LogFactory.getLog(DumpDirective.class); @@ -112,5 +113,22 @@ public class DumpDirective implements TemplateDirectiveModel { out.write(output); } + + public String help(Configuration config) { + Map map = new HashMap(); + 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."); + + Map params = new HashMap(); + params.put("var", "name of variable to dump"); + map.put("params", params); + + List examples = new ArrayList(); + examples.add("<@dump var=\"urls\" />"); + map.put("examples", examples); + + return new FreemarkerHelper().mergeMapToTemplate("dump-directive-help.ftl", map, config); + } } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/VitroTemplateDirectiveModel.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/VitroTemplateDirectiveModel.java new file mode 100644 index 000000000..3c63bb97d --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/web/directives/VitroTemplateDirectiveModel.java @@ -0,0 +1,10 @@ +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); + +} diff --git a/webapp/web/css/dump.css b/webapp/web/css/dump.css index a6fc173e3..734fca46d 100644 --- a/webapp/web/css/dump.css +++ b/webapp/web/css/dump.css @@ -12,3 +12,7 @@ .dump li { list-style-type: none; } + +.dump.directive h6 { + margin-top: 1em; +} diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-datamodel.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-datamodel.ftl index a4647ce5f..b300a68bb 100644 --- a/webapp/web/templates/freemarker/body/partials/dump/dump-datamodel.ftl +++ b/webapp/web/templates/freemarker/body/partials/dump/dump-datamodel.ftl @@ -4,9 +4,9 @@
-

Data Model Dump for Template ${containingTemplate}

+

Data Model Dump for Template ${containingTemplate}

-
Variables
+

VARIABLES

    <#list models?keys as key> @@ -14,10 +14,13 @@
-
Directives
+

DIRECTIVES

    - <#list directives as directive> -
  • ${directive}
  • + <#list directives?keys as directive> +
  • +

    Directive name: ${directive}

    + ${directives[directive]!"no help available for this directive"} +
diff --git a/webapp/web/templates/freemarker/body/partials/dump/dump-directive-help.ftl b/webapp/web/templates/freemarker/body/partials/dump/dump-directive-help.ftl new file mode 100644 index 000000000..793075081 --- /dev/null +++ b/webapp/web/templates/freemarker/body/partials/dump/dump-directive-help.ftl @@ -0,0 +1,29 @@ +<#-- $This file is distributed under the terms of the license in /doc/license.txt$ --> + +<#-- Template for dumping directive help --> + +

Usage: ${usage!}

+ +<#if comments??> +

Comments: ${comments}

+ + +
Parameters:
+<#if params?? && params?keys?has_content> +
    + <#list params?keys as param> +
  • ${param}: ${params[param]}
  • + +
+<#else> +

none

+ + +
Examples:
+<#if examples??> +
    + <#list examples as example> +
  • ${example}
  • + +
+ \ No newline at end of file