[stringtemplate-interest] Output string lengths..
John Snyders
jjsnyders at rcn.com
Wed Nov 29 11:01:48 PST 2006
Output string lengths..Here is a crazy thought.
If you could iterate over the characters in a string then the template could
do the padding.
I created a function "chars" which returns an iterator for its string
argument.
For example
$s1$
$chars(s1); separator="-"$
will produce
SOMETHING
S-O-M-E-T-H-I-N-G
when s1="SOMETHING"
With these attributes added in the controller:
test.setAttribute("s1", "SOMETHING");
test.setAttribute("s2", "NOTHING");
test.setAttribute("space40", " ");
This template
|$chars(s1),chars(space40): { ch, sp | $ch;null={$sp$}$}$|
|$chars(s2),chars(space40): { ch, sp | $ch;null={$sp$}$}$|
produces this output
|SOMETHING |
|NOTHING |
(forgive the proportional font of this email)
Does the chars function have any other use? Well it could be used to do what
the CSS pseudo element ":first-letter" does. For example:
<span class="fancy">$first(chars(s1))$</span>$rest(chars(s1))$
produces:
<span class="fancy">S</span>OMETHING
This gives some stylistic control over individual letters to the template.
What do people think, is the chars function useful?
As for padding, I'm not sure I like this solution better than the pad
option.
-John
Here is the code just incase someone wants to play with this:
Added to ASTExpr.java
private static class StringIterator implements Iterator
{
private String str;
private int index;
public StringIterator(String str) {
this.str = str;
index = 0;
}
public boolean hasNext() {
return index < str.length();
}
public Object next() {
String next = String.valueOf(str.charAt(index));
index++;
return next;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
public Object chars(Object attribute) {
if ( attribute==null ) {
return null;
}
Object chars = null;
if ( attribute instanceof String ) {
String str = (String)attribute;
chars = new StringIterator(str);
}
else if ( attribute instanceof StringTemplate )
{
StringTemplate st = (StringTemplate)attribute;
String str = st.toString();
chars = new StringIterator(str);
}
return chars;
}
Add this to eval.g near trunc
| "chars" a=singleFunctionArg {value=chunk.chars(a);}
Add this to action.g near trunc
| "chars"
-----Original Message-----
From: stringtemplate-interest-bounces at antlr.org
[mailto:stringtemplate-interest-bounces at antlr.org]On Behalf Of Hill, Robert
Sent: Wednesday, November 22, 2006 2:27 AM
To: StringTemplate
Subject: [stringtemplate-interest] Output string lengths..
Is there anyway I can make each string in a list be rendered at a minimum
length?
Id like some things to line up in columns, and want to avoid any sort of
formatting occuring in the code, before I stuff the strings into the
templates.
Is this a job for a custom format renderer, or is there a way to do this
directly within string template?
Ta!
Regards,
Rob
Robert Hill
Information Engineer
E UKIMEA DWP ACU, Hallamshire Business Park, 100 Napier St,
Sheffield. S11 8HD
email: rhill03 at eds.com
Office: +44 114 291 1928
Mobile: +44 7903 185 516
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061129/11fbcdca/attachment.html
More information about the stringtemplate-interest
mailing list