create new tag
, view all tags

Feature Proposal: Add TRUNCATE variable


Often I want to truncate string values. Currently only formatted search has a truncate options for topic, summary and formfield. It would be useful if this feature is available more widely.

While we are at it,

Description and Documentation

  • A TRUNCATE variable with these parameters
    • (default) string
    • length - length of the text after truncation
    • where - "right" (default), "middle", "left"; default "right"
    • substitute - substitution text, for example ...


%TRUNCATE{"His plays are written mostly in blank verse and include comedies, historical plays, the Greek and Roman plays, enigmatic comedies, the great tragedies, and the group of tragicomedies with which he ended his career." length="15" where="middle" substitute="..."}%
should result in: His play...career.



-- Contributors: ArthurClemens - 14 Mar 2008


There is an internal function that generates a "summary" of a string (e.g. by filtering HTML and adding ellipsis). Isn't this closer to what you want?

There are a lot more string operations than just this - why do you think this one is specifically useful? I can't think of a case where I could have used it, myself. I was under the impression that the de facto strategy was to leverage SpreadSheetPlugin to provide this sort of operation. You could even use Javascript str.substr(0,15)+"..."+str.substr(str.length-15)

-- CrawfordCurrie - 15 Mar 2008

It is the same usage as formatted search' $topic(15, ...), but then also useful for other output from variables. I was first thinking of extending FORMFIELD with truncation, but that may be too limited.

I have a concrete usage example. I have a list of recent comments to blog posts. I want to show links to the comment topic's parent (the blog post). I could simply use $parent(25, ...), but I want to show the blog post title, not the topic name.

"name~'*Comments' AND (parent.name/(form.name='BlogPostForm')).value"
format="   * <a href=\"%SCRIPTURL{view}%/$web.$parent?version=$rev#BlogPostPrompt\">$percntFORMFIELD{$quotTitle$quot topic=$quot$parent$quot}$percnt</a> ($count(.*?(\#CommentDate).*))<br /><span class='twikiGrayText twikiSmall'>$date - <nop>$wikiname</span>"

The post titles are really long, too long for a side bar. In this case it would really help to be able to truncate, preferably in the middle, as we are used on Mac OS X.

Another use case is the listing of attachment names in a table. Long names mess up the table.

-- ArthurClemens - 15 Mar 2008

A possible alternative is to add a FORMAT{"" format=""} Var, that is the begining of the separation of ResultSets and formating.

-- SvenDowideit - 15 Mar 2008

I fully support this feature request, it is versatile. I think it should go into the core.

-- PeterThoeny - 16 Mar 2008

Arthur & Crawford - how about the FORMAT operator suggestion?

-- SvenDowideit - 18 Mar 2008

FORMAT might do what I want, and more:

  • append
  • prepend
  • delete (range)
  • replace
  • trim (truncate)
  • number formatting (decimal dots, fractional delimiter)
  • uppercase, lowercase, titlecase, sentencecase

But the syntax is a bit tricky:

%FORMAT{"my text" format="append" string=" with appended text"}%
%FORMAT{"my text" format="prepend" string="May I introduce: "}%
%FORMAT{"my text" format="replace" target="my" string="your" }%
%FORMAT{"my my text" format="replace" where="all" target="my" string="your" }%
%FORMAT{"my text" format="delete" from="3" to="" }%
%FORMAT{"my text" format="titlecase"}%

-- ArthurClemens - 26 Mar 2008

I think what Sven has in mind is more in line with the format parameter of SEARCH and other vars. So you would write:

%FORMAT{"my text" format="append" string=" with appended text"}% %FORMAT{"my text" format="$text with appended text"}%
%FORMAT{"my text" format="prepend" string="May I introduce: "}% %FORMAT{"my text" format="May I introduce $text"}%
%FORMAT{"my text" format="replace" target="my" string="your" }% %FORMAT{"my text" format="$s/your/my/g"}%
%FORMAT{"my text" format="delete" from="3" to="" }% =%FORMAT{"my text" format="$summary(3,...)"}%
%FORMAT{"my text" format="$substr(3,6)"}%

or something like that, anyway. As you can see, this is drifting rapidly towards the sort of language supported by SpreadSheetPlugin, which is why I raised it.

If you do want something like this in the core, then the actual language and operators used in format strings needs really careful thought (it's another template language). Perl variable interpolation in strings might be a good model, but has limitations.

-- CrawfordCurrie - 28 Mar 2008

Would this allow something like

%TRUNCATE{"%INCLUDE{Main.WebHome}%" length="15" where="middle" substitute="..."}% 
where vars are used in place of a text?

-- TravisBarker - 29 Mar 2008

yes, Crawford has identified what I was reflexivly suggesting - basically, ResultSets and the matching ExtractAndCentralizeFormattingRefactor would mean that any TML that uses the format="" specifier would get acess to the much more powerful format. and then %FORMAT would just be a TML accessor to that functionalith when you aren't already using TML..

-- SvenDowideit - 29 Mar 2008

I have no use for the feature as of this moment, but I can imagine cases where this would be a very useful tool.

-- TravisBarker - 29 Mar 2008

I have no use for the feature as of this moment, but I can imagine cases where this would be a very useful tool.

-- TravisBarker - 29 Mar 2008

Edit | Attach | Watch | Print version | History: r11 < r10 < r9 < r8 < r7 | Backlinks | Raw View | Raw edit | More topic actions
Topic revision: r11 - 2008-03-29 - TravisBarker
  • Learn about TWiki  
  • Download TWiki
This site is powered by the TWiki collaboration platform Powered by Perl Hosted by OICcam.com Ideas, requests, problems regarding TWiki? Send feedback. Ask community in the support forum.
Copyright © 1999-2017 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.