Skip to content

Commit

Permalink
Merge pull request jbake-org#593 from jonbullock/fix/246-pebble
Browse files Browse the repository at this point in the history
Restores Pebble template engine support
  • Loading branch information
jonbullock authored Feb 18, 2019
2 parents fdfdefc + 44b6a3d commit 5858220
Show file tree
Hide file tree
Showing 17 changed files with 448 additions and 1 deletion.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ ext {
jade4jVersion = '1.2.7'
mockitoVersion = '2.23.4'
jsoupVersion = '1.11.3'
pebbleVersion = '3.0.7'

isTravis = (System.getenv("TRAVIS") == "true")
isTravisPullRequest = (System.getenv("TRAVIS_PULL_REQUEST")) != "false"
Expand Down
1 change: 1 addition & 0 deletions jbake-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ dependencies {
compile "com.vladsch.flexmark:flexmark:$flexmarkVersion", optional
compile "com.vladsch.flexmark:flexmark-profile-pegdown:$flexmarkVersion", optional
compile "org.jsoup:jsoup:$jsoupVersion"
compile "io.pebbletemplates:pebble:$pebbleVersion", optional

// cli specific dependencies
compile "org.eclipse.jetty:jetty-server:$jettyServerVersion", optional
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.jbake.template;

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.extension.escaper.EscaperExtension;
import com.mitchellbosecke.pebble.loader.FileLoader;
import com.mitchellbosecke.pebble.loader.Loader;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import org.jbake.app.ContentStore;
import org.jbake.app.configuration.JBakeConfiguration;

import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

/**
* Renders pages using the <a href="https://pebbletemplates.io/">Pebble</a> template engine.
*
* @author Mitchell Bosecke
*/
public class PebbleTemplateEngine extends AbstractTemplateEngine {
private PebbleEngine engine;

public PebbleTemplateEngine(final JBakeConfiguration config, final ContentStore db) {
super(config, db);
initializeTemplateEngine();
}

private void initializeTemplateEngine() {
Loader loader = new FileLoader();
loader.setPrefix(config.getTemplateFolder().getAbsolutePath());

/*
* Turn off the autoescaper because I believe that we can assume all
* data is safe considering it is all statically generated.
*/
EscaperExtension escaper = new EscaperExtension();
escaper.setAutoEscaping(false);

engine = new PebbleEngine.Builder().loader(loader).extension(escaper).build();
}

@Override
public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer)
throws RenderingException {

PebbleTemplate template;
try {
template = engine.getTemplate(templateName);
template.evaluate(writer, wrap(model));
} catch (PebbleException e) {
throw new RenderingException(e);
} catch (IOException e) {
throw new RenderingException(e);
}

}

private Map<String, Object> wrap(final Map<String, Object> model) {
Map<String, Object> result = new HashMap<String, Object>(model) {

private static final long serialVersionUID = -5489285491728950547L;

@Override
public Object get(final Object property) {
String key = property.toString();
try {
return extractors.extractAndTransform(db, key, this, new TemplateEngineAdapter.NoopAdapter());
} catch(NoModelExtractorException e) {
// fallback to parent model
}

return super.get(property);
}
};

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ org.jbake.template.FreemarkerTemplateEngine=ftl
org.jbake.template.GroovyTemplateEngine=groovy,gsp,gxml
org.jbake.template.GroovyMarkupTemplateEngine=tpl
org.jbake.template.ThymeleafTemplateEngine=thyme,html
#org.jbake.template.PebbleTemplateEngine=pebble,peb
org.jbake.template.PebbleTemplateEngine=pebble,peb
org.jbake.template.JadeTemplateEngine=jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* The MIT License
*
* Copyright 2015 jdlee.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jbake.app.template;

/**
*
* @author jsb
*/
public class PebbleTemplateEngineRenderingTest extends AbstractTemplateEngineRenderingTest{

public PebbleTemplateEngineRenderingTest() {
super("pebbleTemplates", "pebble");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{% extends 'base.pebble' %}

{% block header %}Archive{% endblock %}

{% block primary %}

<div class="row-fluid marketing">
<div class="span12">
{% set last_month = null %}
{% for post in posts %}
{% if (last_month is not null) %}
{% if post.date | date("MMMM yyyy") != last_month %}
</ul>
<h4>{{ post.date | date("MMMM yyyy") }}</h4>
<ul>
{% endif %}
{% else %}
<h4>{{ post.date | date("MMMM yyyy") }}</h4>
<ul>
{% endif %}

<li>{{ post.date | date("dd") }} - <a href="{{ post.uri }}">{{ post.title }}</a></li>

{% set last_month = post.date | date("MMMM yyyy") %}
{% endfor %}
</div>
</div>

{% endblock %}
54 changes: 54 additions & 0 deletions jbake-core/src/test/resources/fixture/pebbleTemplates/base.pebble
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>{% block title %}JBake{% endblock %}</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">

<!-- CSS -->
<link href="{{ content.rootpath }}css/bootstrap.min.css" rel="stylesheet">
<link href="{{ content.rootpath }}css/asciidoctor.css" rel="stylesheet">
<link href="{{ content.rootpath }}css/base.css" rel="stylesheet">
<link href="{{ content.rootpath }}css/prettify.css" rel="stylesheet">

<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="/js/html5shiv.js"></script>
<![endif]-->

<!-- Fav and touch icons -->
<!--<link rel="apple-touch-icon-precomposed" sizes="144x144" href="../assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png">-->
<link rel="shortcut icon" href="../assets/ico/favicon.png">
</head>

<body onload="prettyPrint()">
<div id="wrap">

{% include 'menu.pebble' %}

<div class="container">
<div class="page-header">
{% block header %}{% endblock %}
</div>

{% block primary %}{% endblock %}
</div> <!-- end .container -->

</div><!-- end #wrap -->

<div id="push"></div>

{% include 'footer.pebble' %}

<!-- javascript -->
<script src="{{ content.rootpath }}js/jquery-1.11.1.min.js"></script>
<script src="{{ content.rootpath }}js/bootstrap.min.js"></script>
<script src="{{ content.rootpath }}js/prettify.js"></script>

</body>
</html>
25 changes: 25 additions & 0 deletions jbake-core/src/test/resources/fixture/pebbleTemplates/feed.pebble
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>JBake</title>
<link>{{ config.site_host }}</link>
<atom:link href="{{ config.site_host }}/{{ config.feed_file }}" rel="self" type="application/rss+xml" />
<description>My corner of the Internet</description>
<language>en-gb</language>
<pubDate>{{ published_date | date("EEE, d MMM yyyy HH:mm:ss Z") }}</pubDate>
<lastBuildDate>{{ published_date | date("EEE, d MMM yyyy HH:mm:ss Z") }}</lastBuildDate>

{% for post in posts %}
<item>
<title>{{ post.title }}</title>
<link>{{ config.site_host }}/{{ post.uri }}</link>
<pubDate>{{ post.date | date("EEE, d MMM yyyy HH:mm:ss Z") }}</pubDate>
<guid isPermaLink="false">{{ post.uri }}</guid>
<description>
{{ post.body }}
</description>
</item>
{% endfor %}

</channel>
</rss>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div id="footer">
<div class="container">
<p class="muted credit">&copy; 2014 | Mixed with <a href="http://getbootstrap.com/">Bootstrap v3.1.1</a> | Baked with <a href="http://jbake.org">JBake {{ version }}</a></p>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Jonathan Bullock</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="Jonathan Bullock">

<!-- Le styles -->
<link href="/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body {
padding-top: 20px;
padding-bottom: 40px;
}

/* Custom container */
.container-narrow {
margin: 0 auto;
max-width: 700px;
}
.container-narrow > hr {
margin: 30px 0;
}

/* Main marketing message and sign up button */
.jumbotron {
margin: 60px 0;
text-align: center;
}
.jumbotron h1 {
font-size: 72px;
line-height: 1;
}
.jumbotron .btn {
font-size: 21px;
padding: 14px 24px;
}

/* Supporting marketing content */
.marketing {
margin: 60px 0;
}
.marketing p + h4 {
margin-top: 28px;
}
</style>
<link href="/css/bootstrap-responsive.min.css" rel="stylesheet">
{% if content is not empty and content.og is not null %}
<meta property="og:description" content="{{ content.og.description }}"/>
{% endif %}

<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="/js/html5shiv.js"></script>
<![endif]-->

<!-- Fav and touch icons -->
<!--<link rel="apple-touch-icon-precomposed" sizes="144x144" href="../assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="../assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="../assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="../assets/ico/apple-touch-icon-57-precomposed.png">
<link rel="shortcut icon" href="../assets/ico/favicon.png">-->
</head>
<body>
<div class="container-narrow">

<div class="masthead">
<ul class="nav nav-pills pull-right">
<li><a href="/">Home</a></li>
<li><a href="/about.html">About</a></li>
<li><a href="/projects.html">Projects</a></li>
<li><a href="/feed.xml">Subscribe</a></li>
</ul>
<h3 class="muted">Jonathan Bullock</h3>
</div>

<hr>
18 changes: 18 additions & 0 deletions jbake-core/src/test/resources/fixture/pebbleTemplates/index.pebble
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends 'base.pebble' %}

{% block header %}Blog{% endblock %}

{% block primary %}

{% for post in published_posts %}
<h1><a href="{{ post.uri }}">{{ post.title }}</a></h1>
<p>{{ post.date | date("dd MMMM yyyy") }}</p>
<p>{{ post.body }}</p>
{% endfor %}

<span>{{ db.getPublishedPages().size() }}</span>
<hr>

<p>Older posts are available in the <a href="{{ content.rootpath }}archive.html">archive</a>.</p>

{% endblock %}
33 changes: 33 additions & 0 deletions jbake-core/src/test/resources/fixture/pebbleTemplates/menu.pebble
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!-- Fixed navbar -->
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="{{ content.rootpath }}">JBake</a>
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a href="{{ content.rootpath }}index.html">Home</a></li>
<li><a href="{{ content.rootpath }}about.html">About</a></li>
<li><a href="{{ content.rootpath }}{{ config.feed_file }}">Subscribe</a></li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Action</a></li>
<li><a href="#">Another action</a></li>
<li><a href="#">Something else here</a></li>
<li class="divider"></li>
<li class="dropdown-header">Nav header</li>
<li><a href="#">Separated link</a></li>
<li><a href="#">One more separated link</a></li>
</ul>
</li>
</ul>
</div><!--/.nav-collapse -->
</div>
</div>
Loading

0 comments on commit 5858220

Please sign in to comment.