On Tue, Sep 15, 2009 at 17:28, Vincent Massol <vincent(a)massol.net> wrote:
Hi,
I'm working on
http://jira.xwiki.org/jira/browse/XWIKI-4345 and in
order to implement the logic in contenview.vm I need to perform some
regex find() from velocity.
I'd like to propose introducing a new velocity tool for this:
#-# velocity.tools = regextool = org.xwiki.velocity.tools.RegexTool
Right now RegexTool has one method:
/**
* @param content the content to parse
* @param regex the regex to look for in the passed content
* @return empty list if the passed regex doesn't match the
content or several {@link RegexResult} objects
* containing the matched position and matched content
for all capturing groups, the first group
* representing the whole . The first object is
represents the entier pattern
*/
public List<RegexResult> find(String content, String regex)
{
List<RegexResult> result = new ArrayList<RegexResult>();
Matcher matcher = Pattern.compile(regex,
Pattern.MULTILINE).matcher(content);
if (matcher.find()) {
for (int i = 0; i < matcher.groupCount() + 1; i++) {
result.add(new RegexResult(matcher.start(i),
matcher.end(i), matcher.group(i)));
}
}
return result;
}
WDYT?
FYI here's what I currently have in contenview.vm:
##
----------------------------------------------------------------------------
## Remove the first rendered H1 or H2 if the following conditions are
met:
## - the document's title is set
## - the title compatibility flag is on
## - the title extracted from the rendered content is the same as the
set title
## We need to do this since we've changed how titles are used and we
need to
## ensure we don't break existing documents that have not yet migrated
to the
## new way.
##
-----------------------------------------------------------------------------
#set ($titleToDisplay = $tdoc.displayTitle)
#if ($cdoc.title && $cdoc.title.length() > 0)
## Look for first H1 or H2 either in lowercase or uppercase
#set ($regexResult = $regextool.find($renderedContent, "<[hH][12].*?
<span>(.*)</span></[hH][12]>"))
#if
(!$regexResult.isEmpty() &&
$regexResult.get(1).group.equals($titleToDisplay))
#if ($regexResult.get(0).end <
$mathtool.sub($renderedContent.length(), 1))
#set ($renderedContent = "${renderedContent.substring(0,
$regexResult.get(0).start)}$
{renderedContent.substring($regexResult.get(0).end)}")
#else
#set ($renderedContent = $renderedContent.substring(0,
$regexResult.get(0).start))
#end
#end
#end
Note that the other option is to introduce yet another API in Document
for this but it doesn't look right to me.
Here's my +1
Thanks
-Vincent
_______________________________________________
devs mailing list
devs(a)xwiki.org
http://lists.xwiki.org/mailman/listinfo/devs