-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug function getTemplateParam #45
Comments
Original comment by
|
Original comment by
|
Original comment by Attachments: |
I had a stab at this for unrelated reasons and didn't get very far. As usual, it struggles with nested templates. I don't want to reimplement the MediaWiki parser, so I'll throw this into the too hard basket until the API improves. I'll leave what I wrote below: public static LinkedHashMap<String, String> parseTemplate(String wikitext)
{
LinkedHashMap<String, String> ret = new LinkedHashMap<>();
if (!wikitext.contains("{{") || !wikitext.contains("}}"))
return ret;
// naive parsing to check for no argument templates
int tempstart = wikitext.indexOf("{{") + 2;
int maybeend = wikitext.indexOf("}}", tempstart);
String maybetemplate = wikitext.substring(tempstart, maybeend).trim();
if (!maybetemplate.contains("|"))
{
ret.put("[name]", maybetemplate);
return ret;
}
// parse the template name
int argstart = wikitext.indexOf('|', tempstart);
ret.put("[name]", wikitext.substring(tempstart, argstart).trim());
// Now check for arguments. Templates may be nested.
int nestinglevel = 0;
int lastpipe = argstart;
int argcount = 1;
for (int i = argstart + 1; i < wikitext.length() - 1; i++)
{
// nestinglevel == -1 -> end of template
// otherwise end of argument
if ((wikitext.charAt(i) == '|' && nestinglevel == 0))
{
String argument = wikitext.substring(lastpipe + 1, i);
ret.putAll(parseTemplateParameter(argument, argcount));
if (nestinglevel == -1)
break;
lastpipe = i;
argcount++;
}
// deal with nesting
String twochar = wikitext.substring(i, i + 2);
if (twochar.equals("{{"))
nestinglevel++;
if (twochar.equals("}}"))
nestinglevel--;
}
return ret;
}
public static Map<String, String> parseTemplateParameter(String wikitext, int argcount)
{
Map<String, String> ret = new HashMap<>();
int equals = wikitext.indexOf('=');
if (equals > -1)
ret.put(wikitext.substring(0, equals).trim(), wikitext.substring(equals + 1).trim());
else
ret.put(String.valueOf(argcount), wikitext.trim());
return ret;
}
@Test
public void parseTemplate()
{
assertTrue("not a template (1)", ParserUtils.parseTemplate("blah").isEmpty());
assertTrue("not a template (2)", ParserUtils.parseTemplate("{{blah").isEmpty());
assertTrue("not a template (3)", ParserUtils.parseTemplate("blah}}").isEmpty());
LinkedHashMap<String, String> expected = new LinkedHashMap<>();
expected.put("[name]", "test");
assertEquals("no argument template", expected, ParserUtils.parseTemplate(" {{ test }} "));
assertEquals("no argument template with extras", expected, ParserUtils.parseTemplate("{{test}} | {{other|junk}}"));
expected.put("1", "unnamed");
assertEquals("one unnamed argument", expected, ParserUtils.parseTemplate("{{test|unnamed}}"));
expected.put("2", "blah");
assertEquals("two unnamed arguments", expected, ParserUtils.parseTemplate("{{test | unnamed | blah }}"));
expected.remove("1");
expected.remove("2");
expected.put("named", "argument");
assertEquals("one named argument", expected, ParserUtils.parseTemplate("{{test| named = argument }}"));
expected.put("second", "arg");
assertEquals("two named arguments", expected, ParserUtils.parseTemplate("{{test|named=argument|second=arg}}"));
expected.remove("named");
expected.remove("second");
expected.put("url", "http://example.com/index.jsp?param=yes");
assertEquals("named parameter with equals", expected, ParserUtils.parseTemplate("{{test|url=http://example.com/index.jsp?param=yes}}"));
expected.remove("url");
expected.put("1", "{{nested}}");
assertEquals("nested template, unnamed", expected, ParserUtils.parseTemplate("{{test|{{nested}}}}"));
expected.put("nest", "{{nested}}");
expected.put("other", "arg");
assertEquals("nested template no args", expected, ParserUtils.parseTemplate("{{test|nest={{nested}}|other=arg}}"));
expected.put("nest", "{{nested|args}}");
assertEquals("nested template no args", expected, ParserUtils.parseTemplate("{{test|nest={{nested|args}}|other=arg}}"));
expected.put("nest", "{{nested|a=arg1|b=arg2}}");
assertEquals("nested template no args", expected, ParserUtils.parseTemplate("{{test|nest={{nested|a=arg1|b=arg2}}|other=arg}}"));
} |
Original issue reported on code.google.com by
rabahmer...@gmail.com
on 21 Dec 2013 at 1:32Attachments:
The text was updated successfully, but these errors were encountered: