The title of this article is no mistake. Anyone who has looked at the FreshMarker Random plugin recently will probably have discovered the new sentence
and paragraph
built-ins. These two bilt-ins support the library with the generation of the famous Lorem Ipsum pseudo-texts. Without delving deep into history, the Lorem Ipsum texts are a corruption of a Cicero text and serve as dummy texts.
Dummy texts are required if the appearance of a layout is to be checked. It is usually necessary for the text to adhere to word, sentence and paragraph lengths that people also use. Dummy texts that only consist of words of length five, for example, are completely unsuitable.
A Lorem Impsum dummy texts can be implemented quite easily.
private static final String[] WORDS = { "a", "ab", "accusamus", "accusantium", // ... };
First, the words from the Lorem Ipsum text are required. In this case, they are available as a String
array.
private List<String> words(Random random, int words) { List<String> result = new LinkedList<>(); for (int i = 0; i < words; i++) { result.add(WORDS[random.nextInt(WORDS.length)]); } String word = result.removeFirst(); result.addFirst(word.substring(0, 1).toUpperCase() + word.substring(1)); return result; }
A single sentence consists of a list of words. These are selected from the list of words in the words method and inserted into the results list. At the end, the first letter of the first word is converted to uppercase. The length of the word list is varied using the words
parameter.
public String sentence(Random random, int min, int max) { if (min == 0 && max == 0) { return ""; } int words = max == 0 ? min : min + random.nextInt(max - min); return String.join(" ", words(random, words)) + "."; }
For the sentence, the word list is put together in the sentence
method and the full stop is added at the end. The length of the sentence is chosen randomly between the parameters min
and max
.
private List<String> sentences(Random random, int sentenceCount) { List<String> result = new LinkedList<>(); for (int i = 0; i < sentenceCount; i++) { result.add(sentence(random, 3, 6)); } return result; }
A single paragraph consists of a list of sentences. This list is created in the sentences
method from random sentences. The number of sentences is between three and six words.
public String paragraph(Random random, int sentences) { return String.join(" ", sentences(random, sentences)); }
For the paragraph, the list of sentences is simply concatenated. The number of sentences can be varied using the sentences
parameter.
builtIns.put(BUILDER.of("sentence"), (x, y, e) -> new TemplateString(LOREM_IPSUM.sentence(((TemplateRandom) x).random(), 3, 9))); builtIns.put(BUILDER.of("paragraph"), (x, y, e) -> new TemplateString(LOREM_IPSUM.paragraph(((TemplateRandom) x).random(), 3)));
In order for dummy texts to appear in FreshMarker, the two random built-ins sentence
and paragraph
are required. The built-in sentence
creates a random sentence with a length of between three and six words. The built-in paragraph
creates a random paragraph consisting of three sentences.
The blind texts Bult-Ins can be found in the current FreshMarker Random version on Maven Central.
Ex quis ut ut voluptatem autem officia. Occaecati non mollitia ipsum dignissimos eligendi tempore. Dolor tenetur illo in inventore.
Facere quae qui iure. Consequatur distinctio ut quasi. Iure aut laudantium quas eum.
Aut similique sit reiciendis. Reiciendis sit quia qui. Explicabo a eos veniam cum.