ここでいくつかのことをしたいようです-説明をアスタリスクで始まる行に入れますが、改行がある場合、または長さが50を超える場合は、別の行に入れます.
まず、ビュー モデルで説明を分割してから、テンプレートに渡します。これは、改行と長さに基づいて行われます。
次に、テンプレートを少し変更して、配列内の各項目を改行とアスタリスクで区切ります。
グループ ファイル test.stg は次のとおりです。
group test;
description(lines) ::= <<
*
* <lines; separator="\n* ">
*
>>
ビューモデルで使用している言語はわかりませんが、ここに Java をいくつか示します。
public static void main(String[] args) {
STGroup templates = new STGroupFile("test.stg");
String description = "This is a small description of the program, with a line-width of 50 chars max, so the line is split.\nFinal line.";
// we add two characters at the start of each line "* " so lines can now
// be only 48 chars in length
description = addLinebreaks(description, 48);
String lines[] = description.split("\\r?\\n");
ST descTemplate = templates.getInstanceOf("description");
for (String line : lines)
{
descTemplate.add("lines", line);
}
System.out.println(descTemplate.render());
}
// used to add line breaks if the length is greater than 50
// From this SO question: http://stackoverflow.com/questions/7528045/large-string-split-into-lines-with-maximum-length-in-java
public static String addLinebreaks(String input, int maxLineLength) {
StringTokenizer tok = new StringTokenizer(input, " ");
StringBuilder output = new StringBuilder(input.length());
int lineLen = 0;
while (tok.hasMoreTokens()) {
String word = tok.nextToken()+" ";
if (lineLen + word.length() > maxLineLength) {
output.append("\n");
lineLen = 0;
}
output.append(word);
lineLen += word.length();
}
return output.toString();
}
私が得た出力は次のとおりです。
*
* This is a small description of the program,
* with a line-width of 50 chars max, so the line
* is split.
* Final line.
*
これはあなたの例とは少し異なりますが、50 文字の制限を満たしています。要件に一致するまで、それをいじることができると思います。