Added Abbreviate renderers

This commit is contained in:
Erik C. Thauvin 2023-03-19 15:11:07 -07:00
parent 4dd5df1d61
commit 1aa2bc2681
8 changed files with 139 additions and 87 deletions

View file

@ -36,6 +36,7 @@ This project provides a collection of useful template renderers.
| Renderer | Description |
|:--------------------------------------------------------------------------------------------------------------------|:-----------------------------------------------------------------|
| [rife.render.Abbreviate](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Abbreviate) | Abbreviates a template value |
| [rife.render.formatCreditcard](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.FormatCreditCard) | Formats a template credit card number value to the last 4 digits |
| [rife.render.Mask](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Mask) | Masks characters of a template value |
| [rife.render.Normalize](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Normalize) | Normalizes a template value for inclusion in a URL path |

View file

@ -0,0 +1,61 @@
/*
* Copyright 2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package rife.render;
import rife.template.Template;
import rife.template.ValueRenderer;
import java.io.IOException;
import java.io.StringReader;
import java.util.Properties;
/**
* <p>Abbreviate a template value with ellipses.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.Abbreviate:valueId/--&gt;
* {{v render:rife.render.Abbreviate:valueId/}}
* </pre>
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @see <a href="https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Abbreviate">rife.render.Abbreviate</a>
* @since 1.0
*/
public class Abbreviate implements ValueRenderer {
/**
* {@inheritDoc}
*/
@Override
public String render(Template template, String valueId, String differentiator) {
var mark = "...";
var max = 0;
if (template.hasDefaultValue(valueId)) {
var properties = new Properties();
try {
properties.load(new StringReader(template.getDefaultValue(valueId)));
mark = properties.getProperty("mark", mark);
max = Integer.parseInt(properties.getProperty("max", String.valueOf(max)));
} catch (IOException | NumberFormatException ignore) {
// do nothing
}
}
return RenderUtils.abbreviate(template.getValueOrAttribute(differentiator), max, mark);
}
}

View file

@ -25,7 +25,7 @@ import java.io.StringReader;
import java.util.Properties;
/**
* <p>Masks characters of a template value.
* <p>Masks characters of a template value.</p>
*
* <p>Usage:</p>
*

View file

@ -17,7 +17,6 @@
package rife.render;
import rife.tools.Localization;
import rife.tools.StringUtils;
import java.io.IOException;
@ -33,14 +32,40 @@ import java.util.concurrent.TimeUnit;
/**
* Collection of utility-type methods commonly used by the renderers.
*
* @author <a href="https://erik.thauvin.net/">Erik C. Thauvin</a>
* @since 1.0
*/
public final class RenderUtils {
private static final String DEFAULT_USER_AGENT = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0";
private static final String DEFAULT_USER_AGENT =
"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/111.0";
private RenderUtils() {
// no-op
}
/**
* Abbreviates a String to the given length using a replacement marker.
*
* @param src the source String
* @param max the maximum length of the resulting String
* @param marker the String used as a replacement marker
* @return the abbreviated String
*/
public static String abbreviate(String src, int max, String marker) {
if (src == null || src.isBlank()) {
return src;
}
var len = src.length();
if (len <= max || max < 0) {
return src;
}
return src.substring(0, max - marker.length()) + marker;
}
/**
* Returns the Swatch Internet (.beat) Time for the give date-time.
*
@ -49,7 +74,8 @@ public final class RenderUtils {
*/
public static String beatTime(ZonedDateTime zonedDateTime) {
var zdt = zonedDateTime.withZoneSameInstant(ZoneId.of("UTC+01:00"));
var beats = (int) ((zdt.get(ChronoField.SECOND_OF_MINUTE) + (zdt.get(ChronoField.MINUTE_OF_HOUR) * 60) + (zdt.get(ChronoField.HOUR_OF_DAY) * 3600)) / 86.4);
var beats = (int) ((zdt.get(ChronoField.SECOND_OF_MINUTE) + (zdt.get(ChronoField.MINUTE_OF_HOUR) * 60) +
(zdt.get(ChronoField.HOUR_OF_DAY) * 3600)) / 86.4);
return String.format("@%03d", beats);
}
@ -81,6 +107,29 @@ public final class RenderUtils {
return sb.toString();
}
/**
* Fetches the content (body) of a URL.
*
* @param url the URL string.
* @param defaultContent the default content to return if none fetched.
* @return the url content, or empty.
*/
public static String fetchUrl(String url, String defaultContent) {
try {
var connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
var code = connection.getResponseCode();
if (code >= 200 && code <= 399) {
try (var inputStream = connection.getInputStream()) {
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
}
} catch (IOException ignore) {
// do nothing
}
return defaultContent;
}
/**
* Returns the last 4 digits a credit card number. The number must satisfy the Luhn algorithm.
* Non-digits are stripped from the number.
@ -204,23 +253,8 @@ public final class RenderUtils {
if (src == null || src.isBlank()) {
return src;
}
var svg = src;
try {
var connection = (HttpURLConnection) new URL(
String.format("https://api.qrserver.com/v1/create-qr-code/?format=svg&size=%s&data=%s", size,
StringUtils.encodeUrl(src.trim())))
.openConnection();
connection.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
if (validResponseCode(connection.getResponseCode())) {
try (var inputStream = connection.getInputStream()) {
svg = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
}
} catch (IOException ignore) {
// do nothing
}
return svg;
return fetchUrl(String.format("https://api.qrserver.com/v1/create-qr-code/?format=svg&size=%s&data=%s", size,
StringUtils.encodeUrl(src.trim())), src);
}
/**
@ -274,22 +308,8 @@ public final class RenderUtils {
if (url == null || url.isBlank() || !url.matches("^[Hh][Tt][Tt][Pp][Ss]?://\\w.*")) {
return url;
}
var shorten = url;
try {
var connection = (HttpURLConnection) new URL(
String.format("https://is.gd/create.php?format=simple&url=%s", StringUtils.encodeUrl(url.trim())))
.openConnection();
connection.setRequestProperty("User-Agent", DEFAULT_USER_AGENT);
if (validResponseCode(connection.getResponseCode())) {
try (var inputStream = connection.getInputStream()) {
shorten = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
}
}
} catch (IOException ignore) {
// do nothing
}
return shorten;
return fetchUrl(String.format("https://is.gd/create.php?format=simple&url=%s",
StringUtils.encodeUrl(url.trim())), url);
}
/**
@ -351,43 +371,6 @@ public final class RenderUtils {
return sb.toString();
}
/**
* Converts the given String to a quoted-printable string.
*
* @param src the source String
* @return the quoted-printable String
*/
public static String toQuotedPrintable(String src) {
if (src == null || src.isEmpty()) {
return src;
}
var len = src.length();
var buff = new StringBuilder(len);
char c;
String hex;
for (var i = 0; i < len; i++) {
c = src.charAt(i);
if (((c > 47) && (c < 58)) || ((c > 64) && (c < 91)) || ((c > 96) && (c < 123))) {
buff.append(c);
} else {
hex = Integer.toString(c, 16);
buff.append('=');
if (hex.length() == 1) {
buff.append('0');
}
buff.append(hex.toUpperCase(Localization.getLocale()));
}
}
return buff.toString();
}
/**
* Returns the formatted server uptime.
*
@ -439,14 +422,4 @@ public final class RenderUtils {
return sb.toString();
}
/**
* Checks whether the specified HTTP response code is valid.
*
* @param code the HTTP response code.
* @return {@code true} if the response code is valid.
*/
public static boolean validResponseCode(int code) {
return code >= 200 && code <= 399;
}
}

View file

@ -23,6 +23,18 @@ import rife.template.TemplateFactory;
import static org.assertj.core.api.Assertions.assertThat;
class TestFormat {
@Test
void testAbbreviate() {
var t = TemplateFactory.HTML.get("abbreviate");
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
System.out.println(t.getContent());
assertThat(t.getContent()).as("max=12").endsWith("").hasSize(12);
t = TemplateFactory.TXT.get("abbreviate");
t.setAttribute(TestCase.FOO, TestCase.SAMPLE_TEXT);
assertThat(t.getContent()).as("max=8").endsWith("...").hasSize(8);
}
@Test
void testFormatCreditCard() {
var t = TemplateFactory.TXT.get("formatCreditCard");

View file

@ -0,0 +1,4 @@
<!--v render:rife.render.Abbreviate:foo-->
mark=…
max=12
<!--/v-->

View file

@ -0,0 +1,3 @@
{{v render:rife.render.Abbreviate:foo}}
max=8
{{/v}}

View file

@ -1,3 +1 @@
<!--v render:rife.render.DateTimeIso-->
tz=UTC
<!--/v-->
<!--v render:rife.render.DateTimeIso-->tz=UTC<!--/v-->