use of com.laytonsmith.tools.docgen.DocGenTemplates.Generator in project CommandHelper by EngineHub.
the class SiteDeploy method getStandardGenerators.
private Map<String, Generator> getStandardGenerators() {
Map<String, Generator> g = new HashMap<>();
g.put("resourceBase", new Generator() {
@Override
public String generate(String... args) {
return SiteDeploy.this.resourceBase;
}
});
g.put("branding", new Generator() {
@Override
public String generate(String... args) {
return Implementation.GetServerType().getBranding();
}
});
g.put("siteRoot", new Generator() {
@Override
public String generate(String... args) {
return SiteDeploy.this.siteBase;
}
});
g.put("docsBase", new Generator() {
@Override
public String generate(String... args) {
return SiteDeploy.this.docsBase;
}
});
g.put("apiJsonVersion", new Generator() {
@Override
public String generate(String... args) throws GenerateException {
return apiJsonVersion;
}
});
/**
* The cacheBuster template is meant to make it easier to deal with caching of resources. The template allows
* you to specify the resource, and it creates a path to the resource using resourceBase, but it also appends a
* hash of the file, so that as the file changes, so does the hash (using a ?v=hash query string). Most
* resources live in /siteDeploy/resources/*, and so the shorthand is to use
* %%cacheBuster|path/to/resource.css%%. However, this isn't always correct, because resources can live all over
* the place. In that case, you should use the following format:
* %%cacheBuster|/absolute/path/to/resource.css|path/to/resource/in/html.css%%
*/
g.put("cacheBuster", new Generator() {
@Override
public String generate(String... args) {
String resourceLoc = SiteDeploy.this.resourceBase + args[0];
String loc = args[0];
if (!loc.startsWith("/")) {
loc = "/siteDeploy/resources/" + loc;
} else {
resourceLoc = SiteDeploy.this.resourceBase + args[1];
}
String hash = "0";
try {
InputStream in = SiteDeploy.class.getResourceAsStream(loc);
if (in == null) {
throw new RuntimeException("Could not find " + loc + " in resources folder for cacheBuster template");
}
hash = getLocalMD5(in);
} catch (IOException ex) {
Logger.getLogger(SiteDeploy.class.getName()).log(Level.SEVERE, null, ex);
}
return resourceLoc + "?v=" + hash;
}
});
final Generator learningTrailGen = new Generator() {
@Override
public String generate(String... args) {
String learning_trail = StreamUtils.GetString(SiteDeploy.class.getResourceAsStream("/siteDeploy/LearningTrail.json"));
List<Map<String, List<Map<String, String>>>> ret = new ArrayList<>();
@SuppressWarnings("unchecked") List<Map<String, List<Object>>> lt = (List<Map<String, List<Object>>>) JSONValue.parse(learning_trail);
for (Map<String, List<Object>> l : lt) {
for (Map.Entry<String, List<Object>> e : l.entrySet()) {
String category = e.getKey();
List<Map<String, String>> catInfo = new ArrayList<>();
for (Object ll : e.getValue()) {
Map<String, String> pageInfo = new LinkedHashMap<>();
String page = null;
String name = null;
if (ll instanceof String) {
name = page = (String) ll;
} else if (ll instanceof Map) {
@SuppressWarnings("unchecked") Map<String, String> p = (Map<String, String>) ll;
if (p.entrySet().size() != 1) {
throw new RuntimeException("Invalid JSON for learning trail");
}
for (Map.Entry<String, String> ee : p.entrySet()) {
page = ee.getKey();
name = ee.getValue();
}
} else {
throw new RuntimeException("Invalid JSON for learning trail");
}
assert page != null && name != null;
boolean exists;
if (page.contains(".")) {
// We can't really check this, because it might be a synthetic page, like
// api.json. So we just have to set it to true.
exists = true;
} else {
exists = SiteDeploy.class.getResourceAsStream("/docs/" + page) != null;
}
pageInfo.put("name", name);
pageInfo.put("page", page);
pageInfo.put("category", category);
pageInfo.put("exists", Boolean.toString(exists));
catInfo.add(pageInfo);
}
Map<String, List<Map<String, String>>> m = new HashMap<>();
m.put(category, catInfo);
ret.add(m);
}
}
return JSONValue.toJSONString(ret);
}
};
g.put("js_string_learning_trail", new Generator() {
@Override
public String generate(String... args) throws GenerateException {
String g = learningTrailGen.generate(args);
g = g.replaceAll("\\\\", "\\\\");
g = g.replaceAll("\"", "\\\\\"");
return g;
}
});
g.put("learning_trail", learningTrailGen);
/**
* If showTemplateCredit is false, then this will return "display: none;" otherwise, it will return an empty
* string.
*/
g.put("showTemplateCredit", new Generator() {
@Override
public String generate(String... args) throws GenerateException {
return showTemplateCredit ? "" : "display: none;";
}
});
return g;
}
Aggregations