Make template remover more robust

marktraceur pointed out that my previous code will fail at
{{echo|{}} which is, unsurprisingly, valid wikitext
This commit is contained in:
YuviPanda 2013-04-11 00:19:09 +05:30
parent 125275548f
commit 91e0f8cff2

View file

@ -12,6 +12,10 @@ public class TemplateRemoveModifier extends PageModifier {
public static final String PARAM_TEMPLATE_NAME = "template"; public static final String PARAM_TEMPLATE_NAME = "template";
public static final Pattern PATTERN_TEMPLATE_OPEN = Pattern.compile("\\{\\{");
public static final Pattern PATTERN_TEMPLATE_CLOSE = Pattern.compile("\\}\\}");
public TemplateRemoveModifier(String templateName) { public TemplateRemoveModifier(String templateName) {
super(MODIFIER_NAME); super(MODIFIER_NAME);
try { try {
@ -39,13 +43,13 @@ public class TemplateRemoveModifier extends PageModifier {
Matcher matcher = templateStartPattern.matcher(pageContents); Matcher matcher = templateStartPattern.matcher(pageContents);
while(matcher.find()) { while(matcher.find()) {
int braceCount = 2; int braceCount = 1;
int startIndex = matcher.start(); int startIndex = matcher.start();
int curIndex = matcher.end(); int curIndex = matcher.end();
while(curIndex < pageContents.length()) { while(curIndex < pageContents.length()) {
if(pageContents.charAt(curIndex) == '{') { if(PATTERN_TEMPLATE_OPEN.matcher(pageContents).find(curIndex)) {
braceCount++; braceCount++;
} else if(pageContents.charAt(curIndex) == '}') { } else if(PATTERN_TEMPLATE_CLOSE.matcher(pageContents).find(curIndex)) {
braceCount--; braceCount--;
} }
curIndex++; curIndex++;