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

GoogleCodeExporter opened this Issue Mar 21, 2015 · 4 comments


None yet
2 participants

GoogleCodeExporter commented Mar 21, 2015

What steps will reproduce the problem?
Apply the function on 

and param = param1;

What is the expected output? 
What do you see instead?

What version of the product are you using?
On what operating system?
Ubuntu 13.04

I haven't tested it with {{name|param=[[title|display Title]]}} but I guess it 
will return [[title| instead of [[title|display Title]]

Original issue reported on by on 21 Dec 2013 at 1:32



This comment has been minimized.

GoogleCodeExporter commented Mar 21, 2015

This issue was closed by revision r180.

Original comment by stop.squark on 22 Dec 2013 at 5:37

  • Changed state: Fixed

This comment has been minimized.

GoogleCodeExporter commented Mar 21, 2015

Reopened per
java_lib .

Original comment by stop.squark on 23 Dec 2013 at 12:40

  • Changed state: Started

This comment has been minimized.

GoogleCodeExporter commented Mar 21, 2015

There's many bugs in the functions for parsing templates. 

Original comment by on 13 Feb 2014 at 10:38



This comment has been minimized.


MER-C commented Apr 27, 2018

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)
            lastpipe = i;
        // deal with nesting
        String twochar = wikitext.substring(i, i + 2);
        if (twochar.equals("{{"))
        if (twochar.equals("}}"))
    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());
        ret.put(String.valueOf(argcount), wikitext.trim());
    return ret;

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.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.put("url", "");
    assertEquals("named parameter with equals", expected, ParserUtils.parseTemplate("{{test|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}}"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment