Skip to content

Commit

Permalink
feat: added br\n filter + suffix for copyright and fixed table issues
Browse files Browse the repository at this point in the history
  • Loading branch information
melistik committed Jul 30, 2020
1 parent b7da885 commit 2c062b9
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class CopyrightLine implements TemplateLine {
final String name;
Integer year;
String url;
String suffix;

@Getter(AccessLevel.PRIVATE)
EmailTemplateBuilder.EmailTemplateConfigBuilder builder;
Expand All @@ -32,6 +33,11 @@ public CopyrightLine url(String url) {
return this;
}

public CopyrightLine suffix(String suffix) {
this.suffix = suffix;
return this;
}

@Override
public TemplateLineType getType() {
return TemplateLineType.COPYRIGHT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.mitchellbosecke.pebble.PebbleEngine;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.extension.AbstractExtension;
import com.mitchellbosecke.pebble.extension.Filter;
import com.mitchellbosecke.pebble.template.EvaluationContext;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
import io.rocketbase.mail.config.TbConfiguration;
import io.rocketbase.mail.model.HtmlTextEmail;
Expand All @@ -23,13 +26,40 @@ public final class EmailTemplateBuilder {
private static final PebbleEngine ENGINE = new PebbleEngine.Builder()
.strictVariables(false)
.autoEscaping(false)
.extension(new PebbleEmailExtension())
.build();

public static EmailTemplateConfigBuilder builder() {
return new EmailTemplateConfigBuilder();
}


public static class PebbleEmailExtension extends AbstractExtension {
@Override
public Map<String, Filter> getFilters() {
Map<String, Filter> filters = new HashMap<>();
filters.put("br", new BrFilter());
return filters;
}

public static final class BrFilter implements Filter {

@Override
public Object apply(Object o, Map<String, Object> map, PebbleTemplate pebbleTemplate, EvaluationContext evaluationContext, int i) throws PebbleException {
if (o == null) {
return null;
}
return o.toString().replace("\n", "<br>");
}

@Override
public List<String> getArgumentNames() {
return null;
}
}
}


@SneakyThrows
static HtmlTextEmail build(TbConfiguration configuration, Header header, List<TemplateLine> contentLines, List<TemplateLine> footerLines) throws PebbleException {
PebbleTemplate htmlTemplate = ENGINE.getTemplate("templates/email/layout.html");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,6 @@
font-size: {{ c.table.heading.size | default('12px') }};
}

.table_footer {
padding-top: 15px;
border-top: {{ c.table.border.size | default('1px') }} solid {{ c.table.border.color | default('#EAEAEC') }};
}

body {
background-color: {{ c.body.background | default('#F4F4F7') }};
color: {{ c.text.color | default('#51545E') }};
Expand Down Expand Up @@ -338,7 +333,7 @@
<hr {% if line.margin is not empty %}style="margin: {{ line.margin}}" {% endif %}>

{% elseif line.type == 'TEXT' %}
<p class="f-fallback" style="{% if line.alignment is not empty %}text-align: {{ line.alignment.value }}; {% endif %}{% if line.fontWeight is not empty %}font-weight: {{ line.fontWeight.value }}{% endif %}; {% if line.fontStyle is not empty %}font-style: {{ line.fontStyle.value }}{% endif %}; {% if line.textDecoration is not empty %}text-decoration: {{ line.textDecoration.value }}{% endif %}; color: {{ line.color | default(c.text.color) | default('#51545E') }}; font-size: {{ line.fontSize | default(c.text.size) | default('16px') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};">{% if line.linkUrl is not empty %}<a target="_blank" href="{{- line.linkUrl | raw -}}" style="color: {{ line.color | default(c.text.linkColor) | default('#3869D4') }};">{% endif %}{{ line.text | escape }}{% if line.linkUrl is not empty %}</a>{% endif %}</p>
<p class="f-fallback" style="{% if line.alignment is not empty %}text-align: {{ line.alignment.value }}; {% endif %}{% if line.fontWeight is not empty %}font-weight: {{ line.fontWeight.value }}{% endif %}; {% if line.fontStyle is not empty %}font-style: {{ line.fontStyle.value }}{% endif %}; {% if line.textDecoration is not empty %}text-decoration: {{ line.textDecoration.value }}{% endif %}; color: {{ line.color | default(c.text.color) | default('#51545E') }}; font-size: {{ line.fontSize | default(c.text.size) | default('16px') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};">{% if line.linkUrl is not empty %}<a target="_blank" href="{{- line.linkUrl | raw -}}" style="color: {{ line.color | default(c.text.linkColor) | default('#3869D4') }};">{% endif %}{{ line.text | escape | br | raw }}{% if line.linkUrl is not empty %}</a>{% endif %}</p>

{% elseif line.type == 'HTML' %}
<p class="f-fallback" style="color: {{ c.text.color | default('#51545E') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};">{{ line.html | raw }}</p>
Expand Down Expand Up @@ -399,7 +394,7 @@
<tr>
{% for col in head %}
<th class="table_heading" align="{{- line.header[loop.index].alignment.value | default('left') -}}" {% if line.header[loop.index].width is not empty %}width="{{- line.header[loop.index].width -}}" {% endif %} colspan="{{- line.header[loop.index].colspan | default('1') -}}" style="border-bottom: {{ c.table.border.size | default('1px') }} solid {{ c.table.border.color | default('#EAEAEC') }}; color: {{ c.font.table.color | default('#333333') }}; font-family: {{ c.font.family | default("'Nunito Sans', Helvetica, Arial, sans-serif") | raw }}; font-size: {{ c.font.table.size | default('16px') }}; padding-bottom: 8px;">
<p class="f-fallback" style="{% if col.alignment is not empty %}text-align: {{ col.alignment.value }}; {% endif %}{% if col.fontWeight is not empty %}font-weight: {{ col.fontWeight.value }}{% endif %}; {% if col.fontStyle is not empty %}font-style: {{ col.fontStyle.value }}{% endif %}; {% if col.textDecoration is not empty %}text-decoration: {{ col.textDecoration.value }}{% endif %}; color: {{ c.table.heading.color | default('#85878E') }}; font-size: {{ c.table.heading.size | default('12px') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: 0;">{{ col | escape }}</p>
<p class="f-fallback" style="{% if col.alignment is not empty %}text-align: {{ col.alignment.value }}; {% endif %}{% if col.fontWeight is not empty %}font-weight: {{ col.fontWeight.value }}{% endif %}; {% if col.fontStyle is not empty %}font-style: {{ col.fontStyle.value }}{% endif %}; {% if col.textDecoration is not empty %}text-decoration: {{ col.textDecoration.value }}{% endif %}; color: {{ c.table.heading.color | default('#85878E') }}; font-size: {{ c.table.heading.size | default('12px') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: 0;">{{ col | escape | br | raw }}</p>
</th>
{% endfor %}
</tr>
Expand All @@ -409,15 +404,15 @@
{% for col in item %}
<td class="table_item" align="{{- line.item[loop.index].alignment.value | default('left') -}}" {% if line.item[loop.index].width is not empty %}width="{{- line.item[loop.index].width -}}" {% endif %} colspan="{{- line.item[loop.index].colspan | default('1') -}}" style="color: {{ c.table.item.color | default('#51545E') }}; font-family: {{ c.font.family | default("'Nunito Sans', Helvetica, Arial, sans-serif") | raw }}; font-size: {{ c.table.item.size | default('15px') }}; line-height: {{ c.table.item.lineHeight | default('18px') }}; padding: 10px 0; word-break: break-word;">
{% if col.type is empty %}
<span class="f-fallback" style="{% if line.item[loop.index].alignment is not empty %}text-align: {{ line.item[loop.index].alignment.value }};{% endif %}{% if line.item[loop.index].fontWeight is not empty %}font-weight: {{ line.item[loop.index].fontWeight.value }}{% endif %};{% if line.item[loop.index].fontStyle is not empty %}font-style: {{ line.item[loop.index].fontStyle.value }}{% endif %};{% if line.item[loop.index].textDecoration is not empty %}text-decoration: {{ line.item[loop.index].textDecoration.value }}{% endif %};">{% if line.item[loop.index].numberFormat is empty %}{{ col | escape }}{% else %}{{ col | numberformat(line.item[loop.index].numberFormat) }}{% endif %}</span>
<span class="f-fallback" style="{% if line.item[loop.index].alignment is not empty %}text-align: {{ line.item[loop.index].alignment.value }};{% endif %}{% if line.item[loop.index].fontWeight is not empty %}font-weight: {{ line.item[loop.index].fontWeight.value }}{% endif %};{% if line.item[loop.index].fontStyle is not empty %}font-style: {{ line.item[loop.index].fontStyle.value }}{% endif %};{% if line.item[loop.index].textDecoration is not empty %}text-decoration: {{ line.item[loop.index].textDecoration.value }}{% endif %};">{% if line.item[loop.index].numberFormat is empty %}{{ col | escape | br | raw }}{% else %}{{ col | numberformat(line.item[loop.index].numberFormat) }}{% endif %}</span>

{% elseif col.type == 'LINK' %}
<a class="f-fallback" style="{% if line.item[loop.index].alignment is not empty %}text-align: {{ line.item[loop.index].alignment.value }}; {% endif %}{% if line.item[loop.index].fontWeight is not empty %}font-weight: {{ line.item[loop.index].fontWeight.value }}{% endif %}; {% if line.item[loop.index].fontStyle is not empty %}font-style: {{ line.item[loop.index].fontStyle.value }}{% endif %}; {% if line.item[loop.index].textDecoration is not empty %}text-decoration: {{ line.item[loop.index].textDecoration.value }}{% endif %}; color: {{ c.text.linkColor | default('#3869D4') }};" href="{{- col.linkUrl | raw -}}">{{ col.text | escape }}</a>

{% elseif col.type == 'IMAGE' %}
{% if col.image.linkUrl is not empty %}<a target="_blank" href="{{- col.image.linkUrl | raw -}}" style="color: {{ c.text.linkColor | default('#3869D4') }};">{% endif %}
<img border="0" vspace="0" hspace="0" src="{{- line.src | raw -}}" alt="{{- col.image.alt | default('') | escape -}}" title="{{- col.image.title | escape -}}" {% if col.image.width is not empty %}width="{{- col.image.width -}}" {% endif %} {% if col.image.height is not empty %}height="{{- col.image.height -}}" {% endif %} style="border: none;">
{% if col.image.linkUrl is not empty %}</a>{% endif %}
{% if col.linkUrl is not empty %}<a target="_blank" href="{{- col.linkUrl | raw -}}" style="color: {{ c.text.linkColor | default('#3869D4') }};">{% endif %}
<img border="0" vspace="0" hspace="0" src="{{- col.src | raw -}}" alt="{{- col.alt | default('') | escape -}}" title="{{- col.title | escape -}}" {% if col.width is not empty %}width="{{- col.width -}}" {% endif %} {% if col.height is not empty %}height="{{- col.height -}}" {% endif %} style="border: none;">
{% if col.linkUrl is not empty %}</a>{% endif %}

{% endif %}
</td>
Expand All @@ -426,9 +421,10 @@
{% endfor %}
{% for footer in line.footerRows %}
<tr>
{% set firstLoop = loop.first %}{% set singleLoop = (loop.length <= 1) %}
{% for col in footer %}
<td class="table_footer" align="{{- line.footer[loop.index].alignment.value | default('left') -}}" {% if line.footer[loop.index].width is not empty %}width="{{- line.footer[loop.index].width -}}" {% endif %} colspan="{{- line.footer[loop.index].colspan | default('1') -}}" style="border-top: {{ c.table.border.size | default('1px') }} solid {{ c.table.border.color | default('#EAEAEC') }}; color: {{ c.font.table.color | default('#333333') }}; font-family: {{ c.font.family | default("'Nunito Sans', Helvetica, Arial, sans-serif") | raw }}; font-size: {{ c.font.table.size | default('16px') }}; padding-top: 15px; word-break: break-word;">
<p class="f-fallback" style="{% if line.footer[loop.index].alignment is not empty %}text-align: {{ line.footer[loop.index].alignment.value }}; {% endif %}{% if line.footer[loop.index].fontWeight is not empty %}font-weight: {{ line.footer[loop.index].fontWeight.value }}{% endif %}; {% if line.footer[loop.index].fontStyle is not empty %}font-style: {{ line.footer[loop.index].fontStyle.value }}{% endif %}; {% if line.footer[loop.index].textDecoration is not empty %}text-decoration: {{ line.footer[loop.index].textDecoration.value }}{% endif %}; color: {{ c.text.color | default('#51545E') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};">{% if line.item[loop.index].numberFormat is empty %}{{ col | escape }}{% else %}{{ col | numberformat(line.item[loop.index].numberFormat) }}{% endif %}</p>
<td class="table_footer" align="{{- line.footer[loop.index].alignment.value | default('left') -}}" {% if line.footer[loop.index].width is not empty %}width="{{- line.footer[loop.index].width -}}" {% endif %} colspan="{{- line.footer[loop.index].colspan | default('1') -}}" style="{% if firstLoop %}border-top: {{ c.table.border.size | default('1px') }} solid {{ c.table.border.color | default('#EAEAEC') }};padding-top: 15px;{% else %}padding-top: 10px;{% endif %} color: {{ c.font.table.color | default('#333333') }}; font-family: {{ c.font.family | default("'Nunito Sans', Helvetica, Arial, sans-serif") | raw }}; font-size: {{ c.font.table.size | default('16px') }}; word-break: break-word;">
<p class="f-fallback" style="{% if line.footer[loop.index].alignment is not empty %}text-align: {{ line.footer[loop.index].alignment.value }}; {% endif %}{% if line.footer[loop.index].fontWeight is not empty %}font-weight: {{ line.footer[loop.index].fontWeight.value }}{% endif %}; {% if line.footer[loop.index].fontStyle is not empty %}font-style: {{ line.footer[loop.index].fontStyle.value }}{% endif %}; {% if line.footer[loop.index].textDecoration is not empty %}text-decoration: {{ line.footer[loop.index].textDecoration.value }}{% endif %}; color: {{ c.text.color | default('#51545E') }}; {% if singleLoop %}line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};{% else %}line-height: 1.3; margin: .2em 0;{% endif %}">{% if line.item[loop.index].numberFormat is empty %}{{ col | escape | br | raw }}{% else %}{{ col | numberformat(line.item[loop.index].numberFormat) }}{% endif %}</p>
</td>
{% endfor %}
</tr>
Expand Down Expand Up @@ -458,10 +454,10 @@
<hr {% if line.margin is not empty %}style="margin: {{ line.margin}}" {% endif %}>

{% elseif line.type == 'COPYRIGHT' %}
<p class="f-fallback sub" style="color: {{ c.footer.color | default('#6B6E76') }}; font-size: {{ c.font.sub.size | default('13px') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};">&copy;{{ line.year }} {% if line.url is not empty %}<a href="{{- line.url | raw -}}" style="color: {{ c.footer.link.color | default('#6B6E76') }}; text-decoration: {{ c.footer.link.textDecoration | default('underline') }};">{% endif %}{{ line.name | escape }}{% if line.url is not empty %}</a>{% endif %}</p>
<p class="f-fallback sub" style="color: {{ c.footer.color | default('#6B6E76') }}; font-size: {{ c.font.sub.size | default('13px') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};">&copy;{{ line.year }} {% if line.url is not empty %}<a href="{{- line.url | raw -}}" style="color: {{ c.footer.link.color | default('#6B6E76') }}; text-decoration: {{ c.footer.link.textDecoration | default('underline') }};">{% endif %}{{ line.name | escape }}{% if line.url is not empty %}</a>{% endif %}{% if line.suffix is not empty %}{{ line.suffix }}{% endif %}</p>

{% elseif line.type == 'TEXT' %}
<p class="f-fallback sub" style="{% if line.alignment is not empty %}text-align: {{ line.alignment.value }}; {% endif %}{% if line.fontWeight is not empty %}font-weight: {{ line.fontWeight.value }}{% endif %}; {% if line.fontStyle is not empty %}font-style: {{ line.fontStyle.value }}{% endif %}; {% if line.textDecoration is not empty %}text-decoration: {{ line.textDecoration.value }}{% endif %}; color: {{ c.footer.color | default('#6B6E76') }}; font-size: {{ line.fontSize | default(c.font.sub.size) | default('13px') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};">{% if line.linkUrl is not empty %}<a target="_blank" href="{{- line.linkUrl | raw -}}" style="color: {{ c.footer.link.color | default('#6B6E76') }}; text-decoration: {{ c.footer.link.textDecoration | default('underline') }};">{% endif %}{{ line.text | escape }}{% if line.linkUrl is not empty %}</a>{% endif %}</p>
<p class="f-fallback sub" style="{% if line.alignment is not empty %}text-align: {{ line.alignment.value }}; {% endif %}{% if line.fontWeight is not empty %}font-weight: {{ line.fontWeight.value }}{% endif %}; {% if line.fontStyle is not empty %}font-style: {{ line.fontStyle.value }}{% endif %}; {% if line.textDecoration is not empty %}text-decoration: {{ line.textDecoration.value }}{% endif %}; color: {{ c.footer.color | default('#6B6E76') }}; font-size: {{ line.fontSize | default(c.font.sub.size) | default('13px') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};">{% if line.linkUrl is not empty %}<a target="_blank" href="{{- line.linkUrl | raw -}}" style="color: {{ c.footer.link.color | default('#6B6E76') }}; text-decoration: {{ c.footer.link.textDecoration | default('underline') }};">{% endif %}{{ line.text | br | raw }}{% if line.linkUrl is not empty %}</a>{% endif %}</p>

{% elseif line.type == 'HTML' %}
<p class="f-fallback sub" style="color: {{ c.footer.color | default('#6B6E76') }}; font-size: {{ c.font.sub.size | default('13px') }}; line-height: {{ c.text.lineHeight | default('1.625') }}; margin: {{ c.text.margin | default('.4em 0 1.1875em') }};">{{ line.html | raw }}</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ TABLE IS NOT YET AVAILABLE ON TXT-EMAIL

{% elseif line.type == 'COPYRIGHT' %}

©{{ line.year }} {{line.name }}{% if line.url is not empty %} -> {{ line.url | raw }}{% endif %}
©{{ line.year }} {{line.name }}{% if line.url is not empty %} -> {{ line.url | raw }}{% endif %}{% if line.suffix is not empty %}{{ line.suffix }}{% endif %}

{% elseif line.type == 'TEXT' %}

Expand Down
Loading

0 comments on commit 2c062b9

Please sign in to comment.