Skip to content

Instantly share code, notes, and snippets.

@hercis
Created January 22, 2014 22:01
Show Gist options
  • Select an option

  • Save hercis/8568261 to your computer and use it in GitHub Desktop.

Select an option

Save hercis/8568261 to your computer and use it in GitHub Desktop.
Converts AngularJS templates to JavaScript
package mx.arux.ng;
import static java.nio.file.StandardOpenOption.CREATE;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class NgTemplateCompiler {
private char quoteChar;
private String indentString;
private boolean useStrict;
public NgTemplateCompiler(char quoteChar, String indentString,
boolean useStrict) {
this.quoteChar = quoteChar;
this.indentString = indentString;
this.useStrict = useStrict;
}
public void compile(String dir, String outFile, String moduleName, String... options) {
Path baseDir = Paths.get(dir);
List<Path> files = getTemplateFiles(baseDir);
String out = compile(files, moduleName, baseDir);
try {
Files.write(Paths.get(outFile), out.getBytes(), CREATE);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private String compile(List<Path> files, String moduleName, Path baseDir) {
Map<String, String> modules = new HashMap<String, String>();
for (Path file : files) {
String name = getModuleName(file, baseDir);
String content = getContent(file);
modules.put("'" + name + "'", compileTemplate(content, name));
}
StringBuilder out = new StringBuilder();
out.append(join(modules.values(), "\n")).append("\n")
.append("angular.module('").append(moduleName).append("', [ ")
.append(join(modules.keySet(), ", ")).append(" ]);");
return out.toString();
}
private List<Path> getTemplateFiles(Path baseDir) {
List<Path> files = Collections.emptyList();
try {
NgTemplateFinder finder = new NgTemplateFinder("*.tpl.html");
Files.walkFileTree(baseDir, finder);
files = finder.done();
} catch (IOException e) {
throw new RuntimeException(e);
}
return files;
}
private String getContent(Path file) {
try {
return new String(Files.readAllBytes(file));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String compileTemplate(String template, String moduleName) {
String content = escapeContent(template);
String strict = useStrict ? indentString + quoteChar + "use strict"
+ quoteChar + ";\n" : "";
StringBuilder out = new StringBuilder();
out.append("angular.module(").append(quoteChar).append(moduleName)
.append(quoteChar).append(", []).run([ ").append(quoteChar)
.append("$templateCache").append(quoteChar)
.append(", function($templateCache) ").append("{\n")
.append(strict).append(indentString)
.append("$templateCache.put(").append(quoteChar)
.append(moduleName).append(quoteChar).append(",\n")
.append(indentString).append(indentString).append(quoteChar)
.append(content).append(quoteChar).append(");\n} ]);\n");
return out.toString();
}
private String escapeContent(String content) {
return content
.replace("\\", "\\\\")
.replace(String.valueOf(quoteChar), "\\" + quoteChar)
.replaceAll(
"\r?\n",
"\\\\n" + quoteChar + " +\n" + indentString
+ indentString + quoteChar);
}
private String getModuleName(Path file, Path baseDir) {
return baseDir.relativize(file).toString().replace('\\', '/');
}
private String join(Collection<String> data, String separator) {
Iterator<String> iter = data.iterator();
StringBuilder sb = new StringBuilder();
if (iter.hasNext()) {
sb.append(iter.next());
while (iter.hasNext()) {
sb.append(separator).append(iter.next());
}
}
return sb.toString();
}
public static void main(String[] args) throws IOException {
new NgTemplateCompiler('\'', " ", true).compile(
"src/main/javascript", "src/main/javascript/Templates.js",
"templates");
}
}
package mx.arux.ng;
import static java.nio.file.FileVisitResult.CONTINUE;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
public class NgTemplateFinder extends SimpleFileVisitor<Path> {
private final PathMatcher matcher;
private List<Path> files;
NgTemplateFinder(String pattern) {
matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
files = new ArrayList<>();
}
void find(Path file) {
Path name = file.getFileName();
if (name != null && matcher.matches(name)) {
files.add(file);
}
}
List<Path> done() {
return files;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
find(file);
return CONTINUE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment