/
JadeTemplateEngine.java
155 lines (136 loc) · 6.29 KB
/
JadeTemplateEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package org.jbake.template;
import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;
import de.neuland.jade4j.JadeConfiguration;
import de.neuland.jade4j.exceptions.JadeCompilerException;
import de.neuland.jade4j.filter.CDATAFilter;
import de.neuland.jade4j.filter.CssFilter;
import de.neuland.jade4j.filter.Filter;
import de.neuland.jade4j.filter.JsFilter;
import de.neuland.jade4j.model.JadeModel;
import de.neuland.jade4j.template.FileTemplateLoader;
import de.neuland.jade4j.template.JadeTemplate;
import de.neuland.jade4j.template.TemplateLoader;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.lang.StringEscapeUtils;
import org.jbake.app.ContentStore;
import org.jbake.app.DBUtil;
import org.jbake.app.DocumentList;
import org.jbake.model.DocumentTypes;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* Renders pages using the <a href="http://jade.org/">Jade</a> template language.
*
* @author Aleksandar Vidakovic
*/
public class JadeTemplateEngine extends AbstractTemplateEngine {
private static final String FILTER_CDATA = "cdata";
private static final String FILTER_STYLE = "css";
private static final String FILTER_SCRIPT = "js";
private JadeConfiguration jadeConfiguration = new JadeConfiguration();
public JadeTemplateEngine(final CompositeConfiguration config, final ContentStore db, final File destination, final File templatesPath) {
super(config, db, destination, templatesPath);
TemplateLoader loader = new FileTemplateLoader(templatesPath.getAbsolutePath() + "/", "UTF-8");
jadeConfiguration.setTemplateLoader(loader);
jadeConfiguration.setFilter(FILTER_CDATA, new CDATAFilter());
jadeConfiguration.setFilter(FILTER_SCRIPT, new JsFilter());
jadeConfiguration.setFilter(FILTER_STYLE, new CssFilter());
jadeConfiguration.getSharedVariables().put("formatter", new FormatHelper());
}
@Override
public void renderDocument(Map<String, Object> model, String templateName, Writer writer) throws RenderingException {
try {
JadeTemplate template = jadeConfiguration.getTemplate(templateName);
renderTemplate(template, model, writer);
} catch (IOException e) {
throw new RenderingException(e);
}
}
public void renderTemplate(JadeTemplate template, Map<String, Object> model, Writer writer) throws JadeCompilerException {
JadeModel jadeModel = wrap(jadeConfiguration.getSharedVariables());
jadeModel.putAll(model);
template.process(jadeModel, writer);
}
private JadeModel wrap(final Map<String, Object> model) {
return new JadeModel(model) {
@Override
public Object get(final Object property) {
String key = property.toString();
if ("db".equals(key)) {
return db;
}
if ("published_posts".equals(key)) {
List<ODocument> query = db.getPublishedPosts();
return DocumentList.wrap(query.iterator());
}
if ("published_pages".equals(key)) {
List<ODocument> query = db.getPublishedPages();
return DocumentList.wrap(query.iterator());
}
if ("published_content".equals(key)) {
List<ODocument> publishedContent = new ArrayList<ODocument>();
String[] documentTypes = DocumentTypes.getDocumentTypes();
for (String docType : documentTypes) {
List<ODocument> query = db.getPublishedContent(docType);
publishedContent.addAll(query);
}
return DocumentList.wrap(publishedContent.iterator());
}
if ("all_content".equals(key)) {
List<ODocument> allContent = new ArrayList<ODocument>();
String[] documentTypes = DocumentTypes.getDocumentTypes();
for (String docType : documentTypes) {
List<ODocument> query = db.getAllContent(docType);
allContent.addAll(query);
}
return DocumentList.wrap(allContent.iterator());
}
if ("alltags".equals(key)) {
List<ODocument> query = db.getAllTagsFromPublishedPosts();
Set<String> result = new HashSet<String>();
for (ODocument document : query) {
String[] tags = DBUtil.toStringArray(document.field("tags"));
Collections.addAll(result, tags);
}
return result;
}
String[] documentTypes = DocumentTypes.getDocumentTypes();
for (String docType : documentTypes) {
if ((docType+"s").equals(key)) {
return DocumentList.wrap(db.getAllContent(docType).iterator());
}
}
if ("tag_posts".equals(key)) {
String tag = model.get("tag").toString();
// fetch the tag posts from db
List<ODocument> query = db.getPublishedPostsByTag(tag);
return DocumentList.wrap(query.iterator());
}
return super.get(property);
}
};
}
public static class FormatHelper {
private Map<String, SimpleDateFormat> formatters = new HashMap<String, SimpleDateFormat>();
public String format(Date date, String pattern) {
if(date!=null && pattern!=null) {
SimpleDateFormat df = formatters.get(pattern);
if(df==null) {
df = new SimpleDateFormat(pattern);
formatters.put(pattern, df);
}
return df.format(date);
} else {
return "";
}
}
public String escape(String s) {
return StringEscapeUtils.escapeHtml(s);
}
}
}