Skip to content

Commit

Permalink
Merge 21b15ca into efd6427
Browse files Browse the repository at this point in the history
  • Loading branch information
melix committed Jan 30, 2014
2 parents efd6427 + 21b15ca commit adb825d
Show file tree
Hide file tree
Showing 14 changed files with 622 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<groovy.version>2.2.1</groovy.version>
<slf4j.version>1.7.5</slf4j.version>
<logback.version>1.0.9</logback.version>
<thymeleaf.version>2.1.2.RELEASE</thymeleaf.version>
</properties>

<build>
Expand Down Expand Up @@ -255,6 +256,12 @@
<artifactId>groovy-all</artifactId>
<version>${groovy.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${thymeleaf.version}</version>
<optional>true</optional>
</dependency>
<!-- sl4j Logging -->
<dependency>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/jbake/app/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -112,6 +113,7 @@ public void renderIndex(String indexFile) {
sb.append("Rendering index [").append(outputFile).append("]...");
Map<String, Object> model = new HashMap<String, Object>();
model.put("renderer", renderingEngine);
model.put("content", Collections.singletonMap("type","index"));

try {
Writer out = createWriter(outputFile);
Expand All @@ -138,6 +140,7 @@ public void renderSitemap(String sitemapFile) {
sb.append("Rendering sitemap [").append(outputFile).append("]... ");

Map<String, Object> model = new HashMap<String, Object>();
model.put("content", Collections.singletonMap("type","sitemap"));

try {
Writer out = createWriter(outputFile);
Expand All @@ -161,6 +164,7 @@ public void renderFeed(String feedFile) {
sb.append("Rendering feed [").append(outputFile).append("]... ");
Map<String, Object> model = new HashMap<String, Object>();
model.put("renderer", renderingEngine);
model.put("content", Collections.singletonMap("type","feed"));

try {
Writer out = createWriter(outputFile);
Expand All @@ -185,6 +189,7 @@ public void renderArchive(String archiveFile) {
sb.append("Rendering archive [").append(outputFile).append("]... ");
Map<String, Object> model = new HashMap<String, Object>();
model.put("renderer", renderingEngine);
model.put("content", Collections.singletonMap("type","archive"));

try {
Writer out = createWriter(outputFile);
Expand All @@ -209,6 +214,7 @@ public void renderTags(Set<String> tags, String tagPath) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("renderer", renderingEngine);
model.put("tag", tag);
model.put("content", Collections.singletonMap("type","tag"));

tag = tag.trim().replace(" ", "-");
File outputFile = new File(destination.getPath() + File.separator + tagPath + File.separator + tag + config.getString("output.extension"));
Expand Down
130 changes: 130 additions & 0 deletions src/main/java/org/jbake/template/ThymeleafTemplateEngine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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 org.apache.commons.configuration.CompositeConfiguration;
import org.jbake.app.DBUtil;
import org.jbake.app.DocumentList;
import org.jbake.model.DocumentTypes;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.VariablesMap;
import org.thymeleaf.templateresolver.FileTemplateResolver;

import java.io.File;
import java.io.Writer;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;

/**
* <p>A template engine which renders pages using Thymeleaf.</p>
*
* <p>This template engine is not recommanded for large sites because the whole model
* is loaded into memory due to Thymeleaf internal limitations.</p>
*
* <p>The default rendering mode is "HTML5", but it is possible to use another mode
* for each document type, by adding a key in the configuration, for example:</p>
* <p/>
* <code>
* template.feed.thymeleaf.mode=XML
* </code>
*
* @author Cédric Champeau
*/
public class ThymeleafTemplateEngine extends AbstractTemplateEngine {
private final ReentrantLock lock = new ReentrantLock();

private TemplateEngine templateEngine;
private FileTemplateResolver templateResolver;

public ThymeleafTemplateEngine(final CompositeConfiguration config, final ODatabaseDocumentTx db, final File destination, final File templatesPath) {
super(config, db, destination, templatesPath);
initializeTemplateEngine();
}

private void initializeTemplateEngine() {
templateResolver = new FileTemplateResolver();
templateResolver.setPrefix(templatesPath.getAbsolutePath() + File.separatorChar);
templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
}

@Override
public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException {
String localeString = config.getString("thymeleaf.locale");
Locale locale = localeString != null ? Locale.forLanguageTag(localeString) : Locale.getDefault();
Context context = new Context(locale, wrap(model));
lock.lock();
try {
@SuppressWarnings("unchecked")
Map<String, Object> config = (Map<String, Object>) model.get("config");
@SuppressWarnings("unchecked")
Map<String, Object> content = (Map<String, Object>) model.get("content");
String mode = "HTML5";
if (config != null && content != null) {
String key = "template_" + content.get("type") + "_thymeleaf_mode";
String configMode = (String) config.get(key);
if (configMode != null) {
mode = configMode;
}
}
templateResolver.setTemplateMode(mode);
templateEngine.process(templateName, context, writer);
} finally {
lock.unlock();
}
}

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

private class JBakeVariablesMap extends VariablesMap<String, Object> {

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()));
}
}

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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
org.jbake.template.FreemarkerTemplateEngine=ftl
org.jbake.template.GroovyTemplateEngine=groovy,gsp,gxml
org.jbake.template.GroovyTemplateEngine=groovy,gsp,gxml
org.jbake.template.ThymeleafTemplateEngine=thyme

0 comments on commit adb825d

Please sign in to comment.