Skip to content

Commit

Permalink
Added capitalizeWords renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
ethauvin committed Nov 15, 2024
1 parent daadd0b commit 99f6afe
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ This project provides a collection of useful template renderers.

## Text Renderers

| Renderer | Description |
|:------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------|
| [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.Rot13](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Rot13) | Translates a template value to/from ROT13 |
| [rife.render.SwapCase](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.SwapCase) | Swaps case of 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.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 |
| Renderer | Description |
|:------------------------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------|
| [rife.render.Capitalize](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Capitalize) | Capitalizes a template value |
| [rife.render.CapitalizeWords](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.CapitalizeWords) | Capitalizes words of 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.Rot13](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.Rot13) | Translates a template value to/from ROT13 |
| [rife.render.SwapCase](https://github.com/rife2/rife2-template-renderers/wiki/rife.render.SwapCase) | Swaps case of 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.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

Expand Down
50 changes: 50 additions & 0 deletions src/main/java/rife/render/CapitalizeWords.java
Original file line number Diff line number Diff line change
@@ -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)));
}
}
32 changes: 32 additions & 0 deletions src/main/java/rife/render/RenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ public static String beatTime(ZonedDateTime zonedDateTime) {
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>
*
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/rife/render/TestRenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ void testAbbreviate() {
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
void testEncode() {
var p = new Properties();
Expand Down

0 comments on commit 99f6afe

Please sign in to comment.