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

Open
GoogleCodeExporter opened this Issue Mar 21, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@GoogleCodeExporter

GoogleCodeExporter commented Mar 21, 2015

hi,
What steps will reproduce the problem?
Apply the function on 

"{{name|parm1={{name2|param=somthing}}}}"
and param = param1;

What is the expected output? 
{{name2|param=somthing}}
What do you see instead?
{{name2

What version of the product are you using?
0.29
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 code.google.com by rabahmer...@gmail.com on 21 Dec 2013 at 1:32

Attachments:

@GoogleCodeExporter

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
@GoogleCodeExporter

This comment has been minimized.

GoogleCodeExporter commented Mar 21, 2015

Reopened per 
https://en.wikipedia.org/w/index.php?title=User_talk:MER-C&oldid=587253357#wiki-
java_lib .

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

  • Changed state: Started
@GoogleCodeExporter

This comment has been minimized.

GoogleCodeExporter commented Mar 21, 2015

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

Original comment by rabahmer...@gmail.com on 13 Feb 2014 at 10:38

Attachments:

@MER-C

This comment has been minimized.

Owner

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)
                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}}"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment