Added capitalizeWords renderer

This commit is contained in:
Erik C. Thauvin 2024-11-15 11:47:40 -08:00
parent daadd0bc4a
commit 99f6afe4fb
Signed by: erik
GPG key ID: 776702A6A2DA330E
4 changed files with 103 additions and 9 deletions

View file

@ -51,15 +51,16 @@ This project provides a collection of useful template renderers.
## Text Renderers ## Text Renderers
| Renderer | Description | | Renderer | Description |
|:------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------| |:------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------|
| [rife.render.Capitalize](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Capitalize) | Capitalizes a template value | | [rife.render.Capitalize](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Capitalize) | Capitalizes a template value |
| [rife.render.Lowercase](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Lowercase) | Converts a template value to lowercase | | [rife.render.CapitalizeWords](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.CapitalizeWords) | Capitalizes words of a template value |
| [rife.render.Rot13](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Rot13) | Translates a template value to/from ROT13 | | [rife.render.Lowercase](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Lowercase) | Converts a template value to lowercase |
| [rife.render.SwapCase](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.SwapCase) | Swaps case of a template value | | [rife.render.Rot13](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Rot13) | Translates a template value to/from ROT13 |
| [rife.render.Trim](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Trim) | Removes leading and trailing whitespace from a template value | | [rife.render.SwapCase](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.SwapCase) | Swaps case of a template value |
| [rife.render.Uncapitalize](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Uncapitalize) | Uncapitalizes a template value | | [rife.render.Trim](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Trim) | Removes leading and trailing whitespace from a template value |
| [rife.render.Uppercase](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Uppercase) | Converts a template value to uppercase | | [rife.render.Uncapitalize](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Uncapitalize) | Uncapitalizes a template value |
| [rife.render.Uppercase](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Uppercase) | Converts a template value to uppercase |
## Documentation ## Documentation

View file

@ -0,0 +1,50 @@
/*
* Copyright 2023-2024 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;
/**
* <p>Capitalizes words of a template value.</p>
*
* <p>Usage:</p>
*
* <pre>
* &lt;!--v render:rife.render.CapitalizeWords:valueId/--&gt;
* {{v render:rife.render.CapitalizeWords: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.CapitalizeWords">rife.render.CapitalizeWords</a>
* @since 1.2
*/
public class CapitalizeWords implements ValueRenderer {
/**
* Returns the template value by capitalizing it.
*
* @param template the template containing the value to be rendered
* @param valueId the identifier of the value to render
* @param differentiator a string used to differentiate the rendering
* @return the capitalized and encoded value
*/
@Override
public String render(Template template, String valueId, String differentiator) {
return template.getEncoder().encode(RenderUtils.capitalizeWords(template.getValueOrAttribute(differentiator)));
}
}

View file

@ -124,6 +124,38 @@ public final class RenderUtils {
return String.format("@%03d", beats); return String.format("@%03d", beats);
} }
/**
* Returns a {@code String} with the first letter of each word capitalized.
*
* @param src the source {@code String}
* @return the capitalized {@code String}
*/
public static String capitalizeWords(String src) {
if (src == null || src.isBlank()) {
return src;
}
var result = new StringBuilder();
var capitalizeNext = true;
for (var i = 0; i < src.length(); i++) {
char c = src.charAt(i);
if (Character.isWhitespace(c)) {
capitalizeNext = true;
result.append(c);
} else {
if (capitalizeNext) {
result.append(Character.toUpperCase(c));
} else {
result.append(Character.toLowerCase(c));
}
capitalizeNext = false;
}
}
return result.toString();
}
/** /**
* <p>Encodes the source {@code String} to the specified encoding.</p> * <p>Encodes the source {@code String} to the specified encoding.</p>
* *

View file

@ -40,6 +40,17 @@ class TestRenderUtils {
assertThat(RenderUtils.abbreviate("", 10, "")).as("").isEmpty(); assertThat(RenderUtils.abbreviate("", 10, "")).as("").isEmpty();
} }
@Test
void testCapitalizeWords() {
assertThat(RenderUtils.capitalizeWords("hello world")).isEqualTo("Hello World");
assertThat(RenderUtils.capitalizeWords("java programming")).isEqualTo("Java Programming");
assertThat(RenderUtils.capitalizeWords("TEST")).isEqualTo("Test");
assertThat(RenderUtils.capitalizeWords("multiple spaces")).isEqualTo("Multiple Spaces");
assertThat(RenderUtils.capitalizeWords("white\t\fspaces")).isEqualTo("White\t\fSpaces");
assertThat(RenderUtils.capitalizeWords("")).isEmpty();
assertThat(RenderUtils.capitalizeWords(null)).isNull();
}
@Test @Test
void testEncode() { void testEncode() {
var p = new Properties(); var p = new Properties();