-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
template: attempt to get pebble templates adapted to Undead
- Loading branch information
Showing
8 changed files
with
249 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/run/undead/javalin/example/view/UndeadCounterPebble.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package run.undead.javalin.example.view; | ||
|
||
import io.pebbletemplates.pebble.PebbleEngine; | ||
import run.undead.context.Context; | ||
import run.undead.event.UndeadEvent; | ||
import run.undead.pebble.PebbleTemplateAdaptor; | ||
import run.undead.template.UndeadTemplate; | ||
import run.undead.view.Meta; | ||
import run.undead.view.View; | ||
|
||
import java.util.Map; | ||
|
||
public class UndeadCounterPebble implements View { | ||
private Integer count; | ||
private PebbleTemplateAdaptor adaptor = new PebbleTemplateAdaptor(new PebbleEngine.Builder() | ||
.build()); | ||
|
||
public UndeadCounterPebble() { | ||
this.count = 0; | ||
} | ||
|
||
@Override | ||
public void mount(Context context, Map sessionData, Map params) { | ||
if (params.get("start") != null) { | ||
this.count = Integer.parseInt((String) params.get("start")); | ||
} else { | ||
this.count = 0; | ||
} | ||
} | ||
|
||
@Override | ||
public void handleEvent(Context context, UndeadEvent event) { | ||
if (event.type().equals("inc")) { | ||
this.count++; | ||
} else if (event.type().equals("dec") && this.count > 0) { | ||
this.count--; | ||
} | ||
} | ||
|
||
@Override | ||
public UndeadTemplate render(Meta meta) { | ||
try { | ||
return adaptor.render("count.html", Map.of("count", this.count)); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/run/undead/pebble/PebbleTemplateAdaptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package run.undead.pebble; | ||
|
||
import io.pebbletemplates.pebble.PebbleEngine; | ||
import io.pebbletemplates.pebble.template.PebbleTemplate; | ||
import run.undead.template.UndeadTemplate; | ||
|
||
import java.io.Writer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class PebbleTemplateAdaptor { | ||
|
||
private final PebbleEngine engine; | ||
|
||
public PebbleTemplateAdaptor() { | ||
this(new PebbleEngine.Builder().build()); | ||
} | ||
public PebbleTemplateAdaptor(PebbleEngine engine) { | ||
this.engine = engine; | ||
} | ||
|
||
public UndeadTemplate render(String template, Map<String, Object> data) throws Exception { | ||
PebbleTemplate compiledTemplate = engine.getTemplate(template); | ||
|
||
var writer = new StringTemplateWriter(); | ||
compiledTemplate.evaluate(writer, data); | ||
return new UndeadTemplate(writer.toStringTemplate(), false); | ||
} | ||
|
||
class StringTemplateWriter extends Writer { | ||
private final List<String> fragments = new ArrayList<>(); | ||
private final List<Object> values = new ArrayList<>(); | ||
private int writeCounts = 0; | ||
|
||
@Override | ||
public void write(char[] cbuf, int off, int len) { | ||
var data = new String(cbuf, off, len); | ||
// System.out.println("write:" + data); | ||
// all we can do is alternate between fragments and values | ||
if(writeCounts % 2 == 0) { | ||
fragments.add(new String(cbuf, off, len)); | ||
} else { | ||
values.add(new String(cbuf, off, len)); | ||
} | ||
writeCounts++; | ||
} | ||
|
||
@Override | ||
public void flush() { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// no-op | ||
} | ||
|
||
public StringTemplate toStringTemplate() { | ||
if(fragments.size() == values.size()) { | ||
throw new RuntimeException("template part needs default value; likely caused by a control statement like an if statement with zero output. try adding an else statement with a default or empty value"); | ||
} | ||
return StringTemplate.of(fragments, values); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return toStringTemplate().interpolate(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<div class="flex flex-col space-y-4 mx-4"> | ||
<div class="text-2xl">Zombie Count</div> | ||
<div class="flex items-center space-x-4"> | ||
<button class="btn btn-primary" | ||
{% if count <= 0 %} | ||
disabled | ||
{% else %} | ||
{% endif %} type="button" ud-click="dec">-</button> | ||
<span class="countdown font-mono text-6xl | ||
{% if count == 0 %} | ||
text-neutral | ||
{% elseif count <= 9 %} | ||
text-warning | ||
{% elseif count > 9 %} | ||
text-error | ||
{% endif %}"> | ||
<span style="--value:{{ count }};"></span> | ||
</span> | ||
<button class="btn btn-primary" type="button" ud-click="inc">+</button> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% if sub == "foo" %} | ||
<p>foo</p> | ||
{% elseif sub == "bar" %} | ||
<p>bar</p> | ||
{% else %} | ||
<p>baz</p> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Title</title> | ||
</head> | ||
<body> | ||
Hello {{name}}! | ||
|
||
{% for article in articles %} | ||
<h3>{{ article.title }}</h3> | ||
<p>{{ article.content }}</p> | ||
{% else %} | ||
<p> There are no articles. </p> | ||
{% endfor %} | ||
|
||
|
||
{% if category == "news" %} | ||
{{ news }} | ||
{% elseif category == "sports" %} | ||
{{ sports }} | ||
{% else %} | ||
<p>Please select a category</p> | ||
{% endif %} | ||
|
||
{% include "sub.html" %} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package run.undead.pebble; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
public class PebbleAdaptorTest { | ||
|
||
@Test | ||
public void testAdaptor() throws Exception { | ||
var adaptor = new PebbleTemplateAdaptor(); | ||
var template = adaptor.render("test.html", | ||
Map.of( | ||
"name", "<h1>Undead</h1>", | ||
"articles", List.of(), | ||
"sub", "foo" | ||
) | ||
); | ||
|
||
assertEquals(StringTemplate.STR.""" | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Title</title> | ||
</head> | ||
<body> | ||
Hello <h1>Undead</h1>! | ||
<p> There are no articles. </p> | ||
<p>Please select a category</p> | ||
<p>foo</p> | ||
</body> | ||
</html>""",template.toString()); | ||
System.out.println("parts:" + template.toParts()); | ||
var parts = new LinkedHashMap<>(); | ||
parts.put("0", "&lt;h1&gt;Undead&lt;/h1&gt;"); | ||
parts.put("1", "<p> There are no articles. </p>\n"); | ||
parts.put("2", "<p>Please select a category</p>\n"); | ||
parts.put("3", "<p>foo</p>\n"); | ||
parts.put("s", List.of( | ||
"<!DOCTYPE html>\n" + | ||
"<html lang=\"en\">\n" + | ||
"<head>\n" + | ||
" <meta charset=\"UTF-8\">\n" + | ||
" <title>Title</title>\n" + | ||
"</head>\n" + | ||
"<body>\n" + | ||
"Hello ", | ||
"!\n" + | ||
"\n", | ||
"\n" + | ||
"\n", | ||
"\n", | ||
"</body>\n" + | ||
"</html>" | ||
)); | ||
assertEquals(parts, template.toParts()); | ||
|
||
} | ||
} |