Skip to content

Commit

Permalink
improved handling of = in wiki context
Browse files Browse the repository at this point in the history
  • Loading branch information
javalc6 committed Jun 7, 2023
1 parent ba95bef commit 7a3190c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
13 changes: 7 additions & 6 deletions wiki/TemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ private String invocation_body(WikiScanner sh, WikiPage wp, Frame parent) {
if (idx != -1) {//parameter present
sh.setPointer(pointer0);//retract scanner at start of identifier
sh.moveAfter(":");//move after : to get parameter
StringBuilder param = new StringBuilder();
template_body(sh, param, sh::getStringWithoutBracketsBar, wp, parent);
parameter = param.toString();

String param = sh.getStringParameter(null);
parameter = param == null ? "" : parseParameter(param, wp, parent);

while (sh.getCharInCharSet("|") != null) {//ignore any further parameter(s)
sh.getStringParameter(null);
}
Expand All @@ -203,10 +204,10 @@ private String invocation_body(WikiScanner sh, WikiPage wp, Frame parent) {
if (idx != -1) {//first parameter present
sh.setPointer(pointer0);//retract scanner at start of identifier
sh.moveAfter(":");//move after : to get parameter
StringBuilder param = new StringBuilder();
template_body(sh, param, sh::getStringWithoutBracketsBar, wp, parent);

ArrayList<String> parameters = new ArrayList<>();
parameters.add(param.toString().trim());//first parameter
String param = sh.getStringParameter(null);
parameters.add(param == null ? "" : param.trim());

Character ch;
while ((ch = sh.getCharInCharSet("|")) != null) {//twin
Expand Down
4 changes: 3 additions & 1 deletion wiki/parserfunctions/Switch.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import wiki.TemplateParser;
import wiki.tools.WikiPage;
import static wiki.tools.Utilities.findValidEqualSign;
import info.bliki.extensions.scribunto.template.Frame;

//reference: https://www.mediawiki.org/wiki/Special:MyLanguage/Help:Extension:ParserFunctions
Expand All @@ -44,7 +45,8 @@ public String evaluate(WikiPage wp, ArrayList<String> parameters, Frame parent)
boolean fallthrough = false;
for (int i = 1; i < parameters.size(); i++) {
String value = parameters.get(i);
int idx = value.indexOf("=");
int idx = findValidEqualSign(value);

String left = tp.parseParameter(idx != -1 ? value.substring(0, idx).trim() : value, wp, parent);
if (idx != -1) {
if ("#default".equals(left)) {
Expand Down
76 changes: 68 additions & 8 deletions wiki/tools/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,77 @@ else if (lang_code.startsWith("de"))
}
}

public static int findValidEqualSign(String parameter) {//find position of = sign outside html tag
if (parameter == null || parameter.isEmpty()) return -1;
int idx = parameter.indexOf("=");
if (idx == -1)
return idx;
int idx2 = parameter.indexOf("<");
if ((idx2 == -1) || (idx < idx2))
return idx;
public static int findValidEqualSign(String str) {//find valid position of = sign
if (str == null || str.isEmpty()) return -1;
int len = str.length();
int noc = 2; //number of open curls
int i = 0;
while (i < len) {
char ch = str.charAt(i);
switch (ch) {
case '{':
boolean found = false;
while (++i < len && ((ch = str.charAt(i)) == '{')) {
noc++; found = true;
}
if (found)
noc++;
continue;
case '}':
boolean found2 = false;
while (noc > 0 && (++i < len) && ((ch = str.charAt(i)) == '}')) {
noc--; found2 = true;
}
if (found2) {
if (noc > 0)
noc--;
if (noc == 0)
return -1;
continue;
}
if (noc > 0) continue;
break;
case '<':
if (str.startsWith("<nowiki>", i)) {
int end = str.indexOf("</nowiki>", i + 8);
if (end != -1) {
i = end + 9;//go after tag "</nowiki>"
continue;
}
} else if (str.startsWith("<code>", i)) {
int end = str.indexOf("</code>", i + 6);
if (end != -1) {
i = end + 7;//go after tag "</code>"
continue;
}
} else if (str.startsWith("<math>", i)) {
int end = str.indexOf("</math>", i + 6);
if (end != -1) {
i = end + 7;//go after tag "</math>"
continue;
}
}
int end = str.indexOf(">", i);
if (end != -1) {
i = end + 1;// go after ">"
continue;
}
break;
case '|':
if (noc < 3)
return -1;
break;
case '=':
if (noc < 3)
return i;
break;
}
i++;
}
return -1;
}


public static boolean checkInteger(String str) {//check that string str is an unsigned integer number
for (char c : str.trim().toCharArray()) {
if (!Character.isDigit(c)) {
Expand Down

0 comments on commit 7a3190c

Please sign in to comment.