Skip to content
Browse files

First working version of Markdown2Html and Markdown2Substance converter

  • Loading branch information...
1 parent 2848e0e commit 967c176c64ddc881a6515e132aa97803d6d9a950 @oliver---- committed May 19, 2011
View
20 examples/Example1.markdown
@@ -3,7 +3,8 @@ Section1
Text Section 1
-##Section 2
+Section 2
+---------
Text Section 2
@@ -12,18 +13,25 @@ Text Section 2
* List level2: is not supported by this markdown parser
* List level1
-# Section 1
+Section 1
+=========
asdasd
-## Section 2
+Section 2
+---------
asdasdasd
-## Section 2
+ function bla() {
+ int blupp = 10;
+ }
+
+Section 2
+---------
asdasdas
-> asdköalskd
-> asölkasdölkasd
+> asdkalskd
+> aslkasdlkasd
> asdklajsdlkajs
View
65 src/org/ndogen/converter/IndentWriter.java
@@ -0,0 +1,65 @@
+package org.ndogen.converter;
+
+import java.io.PrintWriter;
+
+public class IndentWriter extends SimpleWriter {
+
+ private int indentSize = 2;
+
+ private boolean firstInLine = true;
+
+ private String indentString = "";
+ private int indentLevel = 0;
+
+ public IndentWriter(PrintWriter writer) {
+ super(writer);
+ }
+
+ public void setIndentSize(int indentSize) {
+ this.indentSize = indentSize;
+ }
+
+ @Override
+ public void print(String s) {
+ if(firstInLine) {
+ firstInLine = false;
+ super.print(indentString + s);
+ } else {
+ super.print(s);
+ }
+ }
+
+ @Override
+ public void println(String s) {
+ if(firstInLine) {
+ super.println(indentString + s);
+ } else {
+ super.println(s);
+ }
+ firstInLine = true;
+ }
+
+ @Override
+ public void println() {
+ super.println();
+ firstInLine = true;
+ }
+
+ public void indent() {
+ indentLevel++;
+ createIndentString();
+ }
+
+ public void dedent() {
+ indentLevel--;
+ createIndentString();
+ }
+
+ void createIndentString() {
+ StringBuilder sb = new StringBuilder();
+ for (int i = 0; i < indentLevel; i++) {
+ sb.append(" ");
+ }
+ indentString = sb.toString();
+ }
+}
View
198 src/org/ndogen/converter/Markdown2Html.java
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright 2011 Oliver Buchtala
*
* This file is part of ndogen.
@@ -18,27 +18,211 @@
*/
package org.ndogen.converter;
-import java.io.File;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.MalformedURLException;
+import java.net.URL;
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.tree.Tree;
+import org.ndogen.Fixture;
+import org.ndogen.TestUtils;
+import org.ndogen.antlr.DebugCharStream;
+import org.ndogen.markdown.MarkdownParser;
+import org.ndogen.markdown.MarkdownParser.document_return;
+import org.ndogen.markdown.MarkdownScanner;
import org.ndogen.watch.task.ContentProvider;
+/**
+ * Converts a Markdown source into html.
+ * This will not create a standalone html page.
+ * Use this in combination with a html template file.
+ */
public class Markdown2Html implements ContentProvider {
ContentProvider markdownProvider;
+// private TemplateProcessor templateProcessor;
public Markdown2Html(ContentProvider markdownProvider) {
super();
this.markdownProvider = markdownProvider;
+// templateProcessor = new TemplateProcessor();
}
- public void setTemplate(File file) {
-
- }
-
@Override
public String getContent() {
+
+ String input = markdownProvider.getContent();
+
+ CharStream lexerInput;
+ lexerInput = new ANTLRStringStream(input);
+ lexerInput = new DebugCharStream(lexerInput);
+ MarkdownScanner lexer = new MarkdownScanner(lexerInput);
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ MarkdownParser parser = new MarkdownParser(tokens);
- return "<html><h1>Not implemented yet</h1></html>";
+ try {
+ StringWriter sw = new StringWriter();
+ PrintWriter pw = new PrintWriter(sw);
+ IndentWriter out = new IndentWriter(pw);
+ document_return doc = parser.document();
+ Tree tree = (Tree) doc.getTree();
+ processTree(out, tree, new State());
+
+ return sw.toString();
+ } catch (RecognitionException e) {
+ e.printStackTrace();
+ return "<h1>Parser Error!</h1>";
+ }
+
+// return "<html><h1>Not implemented yet</h1></html>";
+ }
+
+ protected void processTree(IndentWriter out, Tree node, State state) {
+ int type = node.getType();
+ switch(type) {
+ case 0:
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ break;
+ case MarkdownParser.SECTION:
+ state.sectionLevel++;
+ String title = node.getChild(0).getText();
+ out.println(String.format("<h%d>%s</h%d>", state.sectionLevel, title, state.sectionLevel));
+ out.indent();
+ for (int i = 1; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ out.dedent();
+ state.sectionLevel--;
+ break;
+ case MarkdownParser.TEXT_BLOCK:
+ out.println("<p>");
+ out.indent();
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ out.dedent();
+ out.println();
+ out.println("</p>");
+ break;
+ case MarkdownParser.TEXT:
+ out.print(node.getText());
+ break;
+ case MarkdownParser.LIST:
+ out.println("<ul>");
+ out.indent();
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ out.dedent();
+ out.println("</ul>");
+ break;
+ case MarkdownParser.LIST_ITEM:
+ out.println("<li>");
+ out.indent();
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ out.dedent();
+ out.println();
+ out.println("</li>");
+ break;
+ case MarkdownParser.QUOTE_BLOCK:
+ out.println("<blockquote><p>");
+ out.indent();
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ out.dedent();
+ out.println();
+ out.println("</p></blockquote>");
+ break;
+ case MarkdownParser.CODE_BLOCK:
+ out.println("<pre>");
+ state.insertNL = true;
+ out.indent();
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ out.dedent();
+ state.insertNL = false;
+ out.println("</pre>");
+ break;
+ case MarkdownParser.NL:
+ if(state.breakLine) {
+ out.println("<br/>");
+ } else if (state.insertNL) {
+ out.println();
+ } else {
+ out.print(" ");
+ }
+ break;
+ case MarkdownParser.EL:
+ // skip this
+ break;
+ case MarkdownParser.EMPH:
+ out.print("<em>");
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ out.print("</em>");
+ break;
+ case MarkdownParser.STRONG:
+ out.print("<strong>");
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ out.print("</strong>");
+ break;
+ case MarkdownParser.INLINE_CODE:
+ out.print("<code>");
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processTree(out, node.getChild(i), state);
+ }
+ out.print("</code>");
+ break;
+ case MarkdownParser.LINK:
+ String text = node.getChild(0).getText();
+ String link = node.getChild(1).getText();
+ try {
+ URL url = new URL(link);
+ link = url.toString();
+ } catch (MalformedURLException e) {
+ System.err.println("Malformed Url: " + link);
+ out.print(text);
+ break;
+ }
+ out.print("<a href=\""+link+"\">"+text+"</a>");
+ break;
+ default:
+ System.out.println("Skipping unhandled node "+ node.getText());
+ // skip
+ }
}
+ public static class State {
+ int sectionLevel = 0;
+ boolean breakLine = false;
+ boolean insertNL = false;
+ }
+
+ public static void main(String[] args) {
+ Fixture fixture = TestUtils.getFixture("fixtures/markdown/parser", "links");
+ final String input = fixture.getInput();
+
+ Markdown2Html markdown2Html = new Markdown2Html(new ContentProvider() {
+ @Override
+ public String getContent() {
+ return input;
+ }
+ });
+
+ String html = markdown2Html.getContent();
+ System.out.println(html);
+ }
}
View
319 src/org/ndogen/converter/Markdown2Substance.java
@@ -18,17 +18,332 @@
*/
package org.ndogen.converter;
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Map;
+
+import org.antlr.runtime.ANTLRStringStream;
+import org.antlr.runtime.CharStream;
+import org.antlr.runtime.CommonTokenStream;
+import org.antlr.runtime.RecognitionException;
+import org.antlr.runtime.tree.Tree;
+import org.ndogen.Fixture;
+import org.ndogen.TestUtils;
+import org.ndogen.antlr.DebugCharStream;
+import org.ndogen.markdown.MarkdownParser;
+import org.ndogen.markdown.MarkdownParser.document_return;
+import org.ndogen.markdown.MarkdownScanner;
+import org.ndogen.substance.Code;
+import org.ndogen.substance.Creator;
+import org.ndogen.substance.Document;
+import org.ndogen.substance.NestedNode;
+import org.ndogen.substance.Quote;
+import org.ndogen.substance.Section;
+import org.ndogen.substance.SubstanceNode;
+import org.ndogen.substance.SubstanceUtils;
+import org.ndogen.substance.Text;
+import org.ndogen.util.FileUtils;
import org.ndogen.watch.task.ContentProvider;
public class Markdown2Substance implements ContentProvider {
ContentProvider markdownProvider;
+ String id = null;
+ String title = null;
+ String author = null;
+ Date createdAt = null;
+ Date modifiedAt = null;
+
+ public Markdown2Substance(ContentProvider markdownProvider) {
+ super();
+ this.markdownProvider = markdownProvider;
+ }
+
@Override
public String getContent() {
- String markdown = markdownProvider.getContent();
+ String input = markdownProvider.getContent();
+
+ CharStream lexerInput;
+ lexerInput = new ANTLRStringStream(input);
+ lexerInput = new DebugCharStream(lexerInput);
+ MarkdownScanner lexer = new MarkdownScanner(lexerInput);
+ CommonTokenStream tokens = new CommonTokenStream(lexer);
+ MarkdownParser parser = new MarkdownParser(tokens);
+
+ try {
+ document_return _doc = parser.document();
+ Tree tree = (Tree) _doc.getTree();
+ Document doc = new Document();
+ doc.setName(id);
+ doc.setCreator(author);
+ doc.setTitle(title);
+ doc.setCreated_at(createdAt);
+ doc.setUpdated_at(modifiedAt);
+
+ processTree(tree, new State(doc));
+
+ Map<String, SubstanceNode> nodeMap = new Creator(author, doc).create();
+
+ return FileUtils.objectToJson(nodeMap);
+ } catch (RecognitionException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private String extractHeader(String key, String text) {
+ int colonIdx = text.indexOf(":");
+ if(colonIdx<0) {
+ throw new RuntimeException("Illegal header line: " + text);
+ }
+ return text.substring(colonIdx).trim();
+ }
+
+ protected void processTree(Tree node, State state) {
+ int type = node.getType();
+ switch(type) {
+ case 0:
+ // if id and title are not set they have to be
+ // specified in the first paragraph
+ boolean firstIsHeader = false;
+ if( id==null
+ || title ==null
+ || author == null
+ || createdAt == null) {
+ firstIsHeader = true;
+
+ Tree header = node.getChild(0);
+
+ for (int i = 0; i < header.getChildCount(); i++) {
+ String text = header.getChild(i).getText();
+
+ if(author == null && text.toLowerCase().startsWith("author")) {
+ author = extractHeader("author", text);
+ }
+ else if(id == null && text.toLowerCase().startsWith("id")) {
+ id = extractHeader("id", text);
+ }
+ else if(title == null && text.toLowerCase().startsWith("title")) {
+ title = extractHeader("title", text);
+ }
+ else if(title == null && text.toLowerCase().startsWith("createAt")) {
+ String _createdAt = extractHeader("createAt", text);
+ try {
+ createdAt = new SimpleDateFormat().parse(_createdAt);
+ } catch (ParseException e) {
+ System.err.println("Could not parse createdAt date :" + createdAt);
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+ if(id==null
+ || title ==null
+ || author == null
+ || createdAt == null
+ || modifiedAt == null) {
+ throw new RuntimeException("Meta information must be set on command line or in a header paragraph of document");
+ }
+
+ Document doc = (Document) state.container;
+ doc.setName(id);
+ doc.setCreator(author);
+ doc.setTitle(title);
+ doc.setCreated_at(createdAt);
+ doc.setUpdated_at(modifiedAt);
+ for (int i = (firstIsHeader?1:0); i < node.getChildCount(); i++) {
+ processTree(node.getChild(i), state);
+ }
+ break;
+ case MarkdownParser.SECTION:
+ {
+ Section section = new Section();
+ String title = node.getChild(0).getText();
+ section.setName(title);
+ State _state = new State(section);
+ for (int i = 1; i < node.getChildCount(); i++) {
+ processTree(node.getChild(i), _state);
+ }
+ state.container.addChild(section);
+ break;
+ }
+ case MarkdownParser.TEXT_BLOCK:
+ {
+ Text text = new Text();
+ StringWriter sw = new StringWriter();
+ SimpleWriter out = new SimpleWriter(new PrintWriter(sw));
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processText(out, node.getChild(i), state);
+ }
+ text.setContent(sw.toString());
+ state.container.addChild(text);
+ break;
+ }
+ case MarkdownParser.LIST:
+ {
+ Text text = new Text();
+ StringWriter sw = new StringWriter();
+ SimpleWriter out = new SimpleWriter(new PrintWriter(sw));
+ processText(out, node, state);
+ text.setContent(sw.toString());
+ state.container.addChild(text);
+ break;
+ }
+ case MarkdownParser.QUOTE_BLOCK:
+ {
+ Quote quote = new Quote();
+ StringWriter sw = new StringWriter();
+ SimpleWriter out = new SimpleWriter(new PrintWriter(sw));
+ State _state = new State();
+ _state.breakLine = true;
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processText(out, node.getChild(i), _state);
+ }
+ quote.setContent(sw.toString());
+ state.container.addChild(quote);
+ break;
+ }
+ case MarkdownParser.CODE_BLOCK:
+ {
+ Code code = new Code();
+ StringWriter sw = new StringWriter();
+ SimpleWriter out = new SimpleWriter(new PrintWriter(sw));
+ State _state = new State();
+ _state.insertNL = true;
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processText(out, node.getChild(i), _state);
+ }
+ code.setContent(sw.toString());
+ state.container.addChild(code);
+ break;
+ }
+ default:
+ System.out.println("Skipping unhandled node: type="+ node.getType() + ", text=" + node.getText());
+ // skip
+ }
+ }
+
+ protected void processText(SimpleWriter out, Tree node, State state) {
+ int type = node.getType();
+ switch(type) {
+ case MarkdownParser.NL:
+ if(state.breakLine) {
+ out.println("<br/>");
+ } else if (state.insertNL) {
+ out.println();
+ } else {
+ out.print(" ");
+ }
+ break;
+ case MarkdownParser.EL:
+ // skip this
+ break;
+ case MarkdownParser.TEXT:
+ out.print(node.getText());
+ break;
+ case MarkdownParser.LIST:
+ out.println("<ul>");
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processText(out, node.getChild(i), state);
+ }
+ out.println("</ul>");
+ break;
+ case MarkdownParser.LIST_ITEM:
+ out.println("<li>");
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processText(out, node.getChild(i), state);
+ }
+ out.println();
+ out.println("</li>");
+ break;
+ case MarkdownParser.EMPH:
+ out.print("<em>");
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processText(out, node.getChild(i), state);
+ }
+ out.print("</em>");
+ break;
+ case MarkdownParser.STRONG:
+ out.print("<strong>");
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processText(out, node.getChild(i), state);
+ }
+ out.print("</strong>");
+ break;
+ case MarkdownParser.INLINE_CODE:
+ out.print("<code>");
+ for (int i = 0; i < node.getChildCount(); i++) {
+ processText(out, node.getChild(i), state);
+ }
+ out.print("</code>");
+ break;
+ case MarkdownParser.LINK:
+ String text = node.getChild(0).getText();
+ String link = node.getChild(1).getText();
+ try {
+ URL url = new URL(link);
+ link = url.toString();
+ } catch (MalformedURLException e) {
+ System.err.println("Malformed Url: " + link);
+ out.print(text);
+ break;
+ }
+ out.print("<a href=\""+link+"\">"+text+"</a>");
+ break;
+ default:
+ System.out.println("Skipping unhandled node: type="+ node.getType() + ", text=" + node.getText());
+ // skip
+ }
+ }
+
+ public static class State {
+ NestedNode container = null;
+
+ boolean insertNL = false;
+
+ boolean breakLine = false;
+
+ public State() {
+ }
+
+ public State(NestedNode container) {
+ this.container = container;
+ }
+ }
+
+ public static void main(String[] args) throws IOException {
+// String fixtureId = "two_sections";
+// Fixture fixture = TestUtils.getFixture("fixtures/markdown/parser", fixtureId);
+// File file = new File("fixtures/markdown/parser/"+fixtureId);
+// final String input = fixture.getInput();
+
+ String _file = "examples/Example1.markdown";
+ File file = new File(_file);
+ String name = "Example1";
+
+ final String input = FileUtils.readFile(_file);
+
+ Markdown2Substance markdown2Substance = new Markdown2Substance(new ContentProvider() {
+ @Override
+ public String getContent() {
+ return input;
+ }
+ });
+ markdown2Substance.id = name;
+ markdown2Substance.title = "Test: " + name;
+ markdown2Substance.author = "oliver";
+ markdown2Substance.createdAt = new Date(file.lastModified());
+ markdown2Substance.modifiedAt = new Date(file.lastModified());
+ String substanceJson = markdown2Substance.getContent();
- return null;
+ SubstanceUtils.push(substanceJson);
}
}
View
26 src/org/ndogen/converter/SimpleWriter.java
@@ -0,0 +1,26 @@
+package org.ndogen.converter;
+
+import java.io.PrintWriter;
+
+public class SimpleWriter {
+
+ protected PrintWriter writer;
+
+ public SimpleWriter(PrintWriter writer) {
+ super();
+ this.writer = writer;
+ }
+
+ public void print(String s) {
+ writer.print(s);
+ }
+
+ public void println(String s) {
+ writer.println(s);
+ }
+
+ public void println() {
+ writer.println();
+ }
+
+}
View
6 src/org/ndogen/markdown/MarkdownParser.g
@@ -1,4 +1,4 @@
-/**
+/*
* Copyright 2011 Oliver Buchtala
*
* This file is part of ndogen.
@@ -17,7 +17,7 @@
* along with ndogen. If not, see <http://www.gnu.org/licenses/>.
*/
-/**
+/*
* Note: this parser is currently only an approximation of the Markdown specification.
*/
parser grammar MarkdownParser;
@@ -73,7 +73,7 @@ document
EL*;
section1
- : TEXT NL RULER1 NL
+ : (TEXT NL RULER1 | H1 TEXT) NL
(EL* paragraph)+
({input.LA(4)==RULER2}?=>EL+ section2)* -> ^(SECTION TEXT paragraph+ section2*);
View
5 src/org/ndogen/substance/Code.java
@@ -21,4 +21,9 @@
public class Code extends Text {
+ public Code() {
+ type = new String[] {
+ "/type/code"
+ };
+ }
}
View
2 src/org/ndogen/substance/Document.java
@@ -87,7 +87,7 @@ public void addEntity(String entity) {
}
public void setCreator(String creator) {
- this.creator = creator;
+ this.creator = "/user/"+creator;
}
public void setCreated_at(Date createdAt) {
View
10 src/org/ndogen/substance/Quote.java
@@ -19,6 +19,16 @@
package org.ndogen.substance;
+import com.google.gson.annotations.Expose;
+
public class Quote extends Text {
+ @Expose
+ String author = " ";
+
+ public Quote() {
+ type = new String[] {
+ "/type/quote"
+ };
+ }
}
View
13 src/org/ndogen/util/FileUtils.java
@@ -31,6 +31,9 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
public class FileUtils {
public static String readFile(String file) throws IOException {
@@ -88,4 +91,14 @@ public static String getMD5(String s) throws NoSuchAlgorithmException {
}
return hexString.toString();
}
+
+ public static String objectToJson(Object o) {
+ GsonBuilder gsonBuilder = new GsonBuilder();
+ gsonBuilder.excludeFieldsWithoutExposeAnnotation();
+ gsonBuilder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
+ Gson gson = gsonBuilder.create();
+ String json = gson.toJson(o);
+
+ return json;
+ }
}
View
50 src/org/ndogen/util/SingleTemplateLoader.java
@@ -0,0 +1,50 @@
+package org.ndogen.util;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.Reader;
+
+import freemarker.cache.TemplateLoader;
+
+public class SingleTemplateLoader implements TemplateLoader {
+
+ File file;
+
+ public SingleTemplateLoader(File file) {
+ super();
+ this.file = file;
+
+ if(!file.exists()) {
+ throw new RuntimeException("Tmplate file does not exist: " + file);
+ }
+ }
+
+ @Override
+ public Object findTemplateSource(String name) throws IOException {
+ return file;
+ }
+
+ @Override
+ public long getLastModified(Object templateSource) {
+ if(templateSource != file) {
+ throw new RuntimeException("Incompatible template source");
+ }
+ return file.lastModified();
+ }
+
+ @Override
+ public Reader getReader(Object templateSource, String encoding)
+ throws IOException {
+ if(templateSource != file) {
+ throw new RuntimeException("Incompatible template source");
+ }
+ return new BufferedReader(new FileReader(file));
+ }
+
+ @Override
+ public void closeTemplateSource(Object templateSource) throws IOException {
+ }
+
+}
View
4 src/org/ndogen/watch/Run.java
@@ -25,7 +25,7 @@
import org.ndogen.converter.Markdown2Html;
import org.ndogen.watch.task.ContentProvider;
-import org.ndogen.watch.task.TemplatedProvider;
+import org.ndogen.watch.task.Template;
import org.ndogen.watch.task.WriteFile;
public class Run {
@@ -103,7 +103,7 @@ public static void main(String[] args) {
}
if(useTemplate) {
- input = new TemplatedProvider(input, templateFile);
+ input = new Template(input, templateFile);
}
Runnable processor = null;
View
31 .../ndogen/watch/task/TemplatedProvider.java → src/org/ndogen/watch/task/Template.java
@@ -18,40 +18,31 @@
package org.ndogen.watch.task;
import java.io.File;
-import java.io.IOException;
-import java.util.LinkedHashMap;
+
+import org.ndogen.util.DefaultTemplateLoader;
+import org.ndogen.util.SingleTemplateLoader;
import pI.generator.TemplateProcessor;
-public class TemplatedProvider implements ContentProvider {
+public class Template implements ContentProvider {
ContentProvider provider;
private TemplateProcessor templateProcessor;
private final String template;
- private LinkedHashMap<String, String> data;
- public TemplatedProvider(ContentProvider provider, String template) {
+ public Template(ContentProvider provider, String template) {
this.provider = provider;
+ templateProcessor = new TemplateProcessor();
+
File templateFile = new File(template);
- String templateDir = "";
- if(templateFile.isAbsolute()) {
- templateDir = templateFile.getParent();
- this.template = templateFile.getName();
- } else if(templateFile.exists()){
- try {
- templateDir = templateFile.getCanonicalFile().getParentFile().getAbsolutePath();
- } catch (IOException e) {
- e.printStackTrace();
- }
-// templateDir = templateFile.getParentFile().getAbsolutePath();
+ if(templateFile.exists()) {
+ templateProcessor.setTemplateLoader(new SingleTemplateLoader(templateFile));
this.template = templateFile.getName();
} else {
- templateDir = "templates";
- this.template = template;
+ templateProcessor.setTemplateLoader(new DefaultTemplateLoader());
+ this.template = template;
}
- data = new LinkedHashMap<String, String>();
- templateProcessor = new TemplateProcessor(templateDir, "", data);
}
@Override

0 comments on commit 967c176

Please sign in to comment.
Something went wrong with that request. Please try again.