Skip to content

Commit

Permalink
Merged #166.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbullock committed Oct 4, 2015
2 parents bd9449f + 354c2f2 commit a8afa78
Show file tree
Hide file tree
Showing 15 changed files with 571 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Expand Up @@ -426,6 +426,12 @@
<version>${thymeleaf.extras.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>de.neuland-bfi</groupId>
<artifactId>jade4j</artifactId>
<version>0.4.2</version>
<optional>true</optional>
</dependency>
<!-- sl4j Logging -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
19 changes: 19 additions & 0 deletions src/main/assembly/assembly-example-project-jade.xml
@@ -0,0 +1,19 @@
<assembly>
<id>base</id>
<includeBaseDirectory>false</includeBaseDirectory>
<!-- Generates a zip package containing the needed files -->
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/example-project-jade/</directory>
<outputDirectory>/</outputDirectory>
<excludes>
<exclude>.git</exclude>
<exclude>README.md</exclude>
<exclude>LICENSE</exclude>
</excludes>
</fileSet>
</fileSets>
</assembly>
155 changes: 155 additions & 0 deletions src/main/java/org/jbake/template/JadeTemplateEngine.java
@@ -0,0 +1,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);
}
}
}
Expand Up @@ -2,4 +2,6 @@ org.jbake.template.FreemarkerTemplateEngine=ftl
org.jbake.template.GroovyTemplateEngine=groovy,gsp,gxml
org.jbake.template.GroovyMarkupTemplateEngine=tpl
org.jbake.template.ThymeleafTemplateEngine=thyme
org.jbake.template.PebbleTemplateEngine=pebble,peb
org.jbake.template.PebbleTemplateEngine=pebble,peb
org.jbake.template.ThymeleafTemplateEngine=thyme,html
org.jbake.template.JadeTemplateEngine=jade

0 comments on commit a8afa78

Please sign in to comment.