Skip to content

Commit

Permalink
Support for jade template engine
Browse files Browse the repository at this point in the history
  • Loading branch information
mariuszs committed Nov 2, 2014
1 parent 73e7b78 commit 2810e64
Show file tree
Hide file tree
Showing 15 changed files with 566 additions and 1 deletion.
33 changes: 33 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@
<connectionUrl>scm:git:git://github.com/jbake-org/jbake-example-project-thymeleaf.git</connectionUrl>
</configuration>
</execution>
<execution>
<id>checkout-example-project-jade</id>
<phase>prepare-package</phase>
<goals>
<goal>checkout</goal>
</goals>
<configuration>
<checkoutDirectory>${project.build.directory}/example-project-jade</checkoutDirectory>
<!-- TODO: Move this project to jbake repo -->
<connectionUrl>scm:git:git://github.com/mariuszs/jbake-example-project-jade.git</connectionUrl>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
Expand Down Expand Up @@ -239,6 +251,21 @@
<attach>false</attach>
</configuration>
</execution>
<execution>
<id>zip-example-project-jade</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>example_project_jade</finalName>
<appendAssemblyId>false</appendAssemblyId>
<descriptors>
<descriptor>src/main/assembly/assembly-example-project-jade.xml</descriptor>
</descriptors>
<attach>false</attach>
</configuration>
</execution>
<execution>
<id>make-assembly</id>
<phase>package</phase>
Expand Down Expand Up @@ -372,6 +399,12 @@
<version>${thymeleaf.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>de.neuland-bfi</groupId>
<artifactId>jade4j</artifactId>
<version>0.4.0</version>
</dependency>

<!-- sl4j Logging -->
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
19 changes: 19 additions & 0 deletions src/main/assembly/assembly-example-project-jade.xml
Original file line number Diff line number Diff line change
@@ -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>
118 changes: 118 additions & 0 deletions src/main/java/org/jbake/template/JadeTemplateEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
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.Jade4J;
import de.neuland.jade4j.JadeConfiguration;
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.jbake.app.DBUtil;
import org.jbake.app.DocumentList;
import org.jbake.model.DocumentTypes;
import org.jbake.template.jade.DateUtils;

import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.*;

public class JadeTemplateEngine extends AbstractTemplateEngine {

private final JadeConfiguration jadeConfiguration;

public JadeTemplateEngine(final CompositeConfiguration config, ODatabaseDocumentTx db, File destination, File templatesPath) {
super(config, db, destination, templatesPath);

jadeConfiguration = new JadeConfiguration();
jadeConfiguration.setMode(Jade4J.Mode.XHTML);
jadeConfiguration.setPrettyPrint(true);
String basePath = templatesPath.getAbsolutePath() + File.separatorChar;
TemplateLoader loader = new FileTemplateLoader(
basePath,
config.getString("template.encoding"));
jadeConfiguration.setTemplateLoader(loader);
}

@Override
public void renderDocument(Map<String, Object> model, String templateName, Writer writer) throws RenderingException {
try {
JadeTemplate jadeTemplate = jadeConfiguration.getTemplate(templateName);
model.put("dates", new DateUtils());
jadeTemplate.process(wrap(model), writer);
} catch (IOException e) {
throw new RenderingException(e);
}
}

private JadeModel wrap(final Map<String, Object> model) {
return new JBakeVariablesMap(model);
}

private class JBakeVariablesMap extends JadeModel {

public JBakeVariablesMap(final Map<String, Object> model) {
super(model);
completeModel();
}

private void completeModel() {
put("db", db);
put("alltags", getAllTags());
put("tag_posts", getTagPosts());
put("published_date", new Date());
String[] documentTypes = DocumentTypes.getDocumentTypes();
for (String docType : documentTypes) {
put(docType + "s", DocumentList.wrap(DBUtil.query(db, "select * from " + docType + " order by date desc").iterator()));
put("published_" + docType + "s", DocumentList.wrap(DBUtil.query(db, "select * from " + docType + " where status='published' order by date desc").iterator()));
}
put("published_content", getPublishedContent());
put("all_content", getAllContent());
}

private Object getTagPosts() {
Object tagName = get("tag");
if (tagName != null) {
String tag = tagName.toString();
// fetch the tag posts from db
List<ODocument> query = DBUtil.query(db, "select * from post where status='published' where ? in tags order by date desc", tag);
return DocumentList.wrap(query.iterator());
} else {
return Collections.emptyList();
}
}

private Object getAllTags() {
List<ODocument> query = db.query(new OSQLSynchQuery<ODocument>("select tags from post where status='published'"));
Set<String> result = new HashSet<String>();
for (ODocument document : query) {
String[] tags = DBUtil.toStringArray(document.field("tags"));
Collections.addAll(result, tags);
}
return result;
}

private Object getPublishedContent() {
List<ODocument> publishedContent = new ArrayList<ODocument>();
String[] documentTypes = DocumentTypes.getDocumentTypes();
for (String docType : documentTypes) {
List<ODocument> query = db.query(new OSQLSynchQuery<ODocument>("select * from " + docType + " where status='published' order by date desc"));
publishedContent.addAll(query);
}
return DocumentList.wrap(publishedContent.iterator());
}

private Object getAllContent() {
List<ODocument> allContent = new ArrayList<ODocument>();
String[] documentTypes = DocumentTypes.getDocumentTypes();
for (String docType : documentTypes) {
List<ODocument> query = db.query(new OSQLSynchQuery<ODocument>("select * from " + docType + " order by date desc"));
allContent.addAll(query);
}
return DocumentList.wrap(allContent.iterator());
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/org/jbake/template/jade/DateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jbake.template.jade;


import org.apache.commons.lang3.StringEscapeUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtils {

public String format(Date date, String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(date);
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
org.jbake.template.FreemarkerTemplateEngine=ftl
org.jbake.template.GroovyTemplateEngine=groovy,gsp,gxml
org.jbake.template.ThymeleafTemplateEngine=thyme
org.jbake.template.ThymeleafTemplateEngine=thyme
org.jbake.template.JadeTemplateEngine=jade
2 changes: 2 additions & 0 deletions src/main/resources/default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ example.project.freemarker=example_project_freemarker.zip
example.project.groovy=example_project_groovy.zip
# zip file containing example project structure using thymeleaf templates
example.project.thymeleaf=example_project_thymeleaf.zip
# zip file containing example project structure using jade templates
example.project.jade=example_project_jade.zip
# default asciidoctor options
asciidoctor.attributes=source-highlighter=prettify
# should JBake config options be exported to Asciidoctor engine?
Expand Down
Loading

0 comments on commit 2810e64

Please sign in to comment.