Fix MarkdownRenderer to accept templates that are not built-in resources
package com.google.devtools.build.skydoc.rendering;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.ProviderInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.RuleInfo;
import com.google.devtools.build.skydoc.rendering.proto.StardocOutputProtos.UserDefinedFunctionInfo;
+import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
import java.io.StringWriter;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.MethodInvocationException;
@@ -31,7 +40,7 @@ import org.apache.velocity.runtime.resource.loader.JarResourceLoader;
* Produces skydoc output in markdown form.
*/
public class MarkdownRenderer {
-
+ // TODO(kendalllane): Refactor MarkdownRenderer to take in something other than filepaths.
private final String headerTemplateFilename;
private final String ruleTemplateFilename;
private final String providerTemplateFilename;
@@ -65,9 +74,9 @@ public class MarkdownRenderer {
*/
public String renderMarkdownHeader() throws IOException {
StringWriter stringWriter = new StringWriter();
+ Reader reader = readerFromPath(headerTemplateFilename);
try {
- velocityEngine.mergeTemplate(
- headerTemplateFilename, "UTF-8", new VelocityContext(), stringWriter);
+ velocityEngine.evaluate(new VelocityContext(), stringWriter, headerTemplateFilename, reader);
} catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
throw new IOException(e);
}
@@ -85,8 +94,9 @@ public class MarkdownRenderer {
context.put("ruleInfo", ruleInfo);
StringWriter stringWriter = new StringWriter();
+ Reader reader = readerFromPath(ruleTemplateFilename);
try {
- velocityEngine.mergeTemplate(ruleTemplateFilename, "UTF-8", context, stringWriter);
+ velocityEngine.evaluate(context, stringWriter, ruleTemplateFilename, reader);
} catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
throw new IOException(e);
}
@@ -104,8 +114,9 @@ public class MarkdownRenderer {
context.put("providerInfo", providerInfo);
StringWriter stringWriter = new StringWriter();
+ Reader reader = readerFromPath(providerTemplateFilename);
try {
- velocityEngine.mergeTemplate(providerTemplateFilename, "UTF-8", context, stringWriter);
+ velocityEngine.evaluate(context, stringWriter, providerTemplateFilename, reader);
} catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
throw new IOException(e);
}
@@ -122,11 +133,30 @@ public class MarkdownRenderer {
context.put("funcInfo", functionInfo);
StringWriter stringWriter = new StringWriter();
+ Reader reader = readerFromPath(functionTemplateFilename);
try {
- velocityEngine.mergeTemplate(functionTemplateFilename, "UTF-8", context, stringWriter);
+ velocityEngine.evaluate(context, stringWriter, functionTemplateFilename, reader);
} catch (ResourceNotFoundException | ParseErrorException | MethodInvocationException e) {
throw new IOException(e);
}
return stringWriter.toString();
}
+
+ /**
+ * Returns a reader from the given path.
+ *
+ * @param filePath The given path, either a filesystem path or a java Resource
+ */
+ private static Reader readerFromPath(String filePath) throws IOException {
+ if (Files.exists(Paths.get(filePath))) {
+ Path path = Paths.get(filePath);
+ return Files.newBufferedReader(path);
+ }
+
+ InputStream inputStream = MarkdownRenderer.class.getClassLoader().getResourceAsStream(filePath);
+ if (inputStream == null) {
+ throw new FileNotFoundException(filePath + " was not found as a resource.");
+ }
+ return new InputStreamReader(inputStream, UTF_8);
+ }
}
