Search in sources :

Example 1 with Template

use of org.eclipse.mylyn.wikitext.mediawiki.Template in project mylyn.docs by eclipse.

the class TemplateProcessor method normalize.

private Template normalize(Template template) {
    Template normalizedTemplate = new Template();
    normalizedTemplate.setName(template.getName());
    normalizedTemplate.setTemplateMarkup(normalizeTemplateMarkup(template.getTemplateContent()));
    return normalizedTemplate;
}
Also used : Template(org.eclipse.mylyn.wikitext.mediawiki.Template)

Example 2 with Template

use of org.eclipse.mylyn.wikitext.mediawiki.Template in project mylyn.docs by eclipse.

the class TemplateProcessor method processTemplates.

private String processTemplates(String markupContent, Set<String> usedTemplates) {
    StringBuilder processedMarkup = new StringBuilder();
    int lastIndex = 0;
    Matcher matcher = templatePattern.matcher(markupContent);
    while (matcher.find()) {
        int start = matcher.start();
        if (lastIndex < start) {
            processedMarkup.append(markupContent.substring(lastIndex, start));
        }
        String templateName = matcher.group(2);
        Template template = resolveTemplate(templateName);
        if (template != null) {
            String replacementText;
            if (usedTemplates.contains(templateName)) {
                StringBuilder sb = new StringBuilder();
                // $NON-NLS-1$
                sb.append("<span class=\"error\">");
                sb.append(// $NON-NLS-1$
                MessageFormat.format(// $NON-NLS-1$
                Messages.getString("TemplateProcessor_loopDetected"), template.getName()));
                // $NON-NLS-1$
                sb.append("</span>");
                replacementText = sb.toString();
            } else {
                String parameters = matcher.group(3);
                replacementText = processTemplate(template, parameters);
                // The replacementText might contain other templates. Add the current template to the set of used template and call recursively this function again:
                Set<String> templates = new HashSet<String>(usedTemplates);
                templates.add(templateName);
                replacementText = processTemplates(replacementText, templates);
            }
            replacementText = processTemplates(replacementText);
            processedMarkup.append(replacementText);
        }
        lastIndex = matcher.end();
    }
    if (lastIndex == 0) {
        return markupContent;
    }
    if (lastIndex < markupContent.length()) {
        processedMarkup.append(markupContent.substring(lastIndex));
    }
    return processedMarkup.toString();
}
Also used : Matcher(java.util.regex.Matcher) Template(org.eclipse.mylyn.wikitext.mediawiki.Template) HashSet(java.util.HashSet)

Example 3 with Template

use of org.eclipse.mylyn.wikitext.mediawiki.Template in project mylyn.docs by eclipse.

the class TemplateProcessor method resolveTemplate.

private Template resolveTemplate(String templateName) {
    if (!excludePatterns.isEmpty()) {
        for (Pattern p : excludePatterns) {
            if (p.matcher(templateName).matches()) {
                return null;
            }
        }
    }
    Template template = templateByName.get(templateName);
    if (template == null) {
        for (TemplateResolver resolver : mediaWikiLanguage.getTemplateProviders()) {
            template = resolver.resolveTemplate(templateName);
            if (template != null) {
                template = normalize(template);
                break;
            }
        }
        if (template == null) {
            template = new Template();
            template.setName(templateName);
            // $NON-NLS-1$
            template.setTemplateMarkup("");
        }
        templateByName.put(template.getName(), template);
    }
    return template;
}
Also used : Pattern(java.util.regex.Pattern) TemplateResolver(org.eclipse.mylyn.wikitext.mediawiki.TemplateResolver) Template(org.eclipse.mylyn.wikitext.mediawiki.Template)

Example 4 with Template

use of org.eclipse.mylyn.wikitext.mediawiki.Template in project mylyn.docs by eclipse.

the class TemplateProcessorTest method testBasicTemplateNamedParameter_EmptyDefaultValue.

public void testBasicTemplateNamedParameter_EmptyDefaultValue() {
    Template template = new Template();
    template.setName("test");
    template.setTemplateMarkup("_expanded{{{message|}}}_");
    markupLanguage.getTemplates().add(template);
    TemplateProcessor templateProcessor = new TemplateProcessor(markupLanguage);
    String markup = templateProcessor.processTemplates("one {{test}} two");
    assertEquals("one _expanded_ two", markup);
}
Also used : TemplateProcessor(org.eclipse.mylyn.wikitext.mediawiki.internal.TemplateProcessor) Template(org.eclipse.mylyn.wikitext.mediawiki.Template)

Example 5 with Template

use of org.eclipse.mylyn.wikitext.mediawiki.Template in project mylyn.docs by eclipse.

the class TemplateProcessorTest method testBasicTemplatePositionalParameter_DefaultValue.

public void testBasicTemplatePositionalParameter_DefaultValue() {
    Template template = new Template();
    template.setName("test");
    template.setTemplateMarkup("_expanded{{{1|first}}}and{{{2|second}}}_");
    markupLanguage.getTemplates().add(template);
    TemplateProcessor templateProcessor = new TemplateProcessor(markupLanguage);
    String markup = templateProcessor.processTemplates("one {{test}} two");
    assertEquals("one _expandedfirstandsecond_ two", markup);
}
Also used : TemplateProcessor(org.eclipse.mylyn.wikitext.mediawiki.internal.TemplateProcessor) Template(org.eclipse.mylyn.wikitext.mediawiki.Template)

Aggregations

Template (org.eclipse.mylyn.wikitext.mediawiki.Template)33 TemplateProcessor (org.eclipse.mylyn.wikitext.mediawiki.internal.TemplateProcessor)27 HashSet (java.util.HashSet)2 TemplateResolver (org.eclipse.mylyn.wikitext.mediawiki.TemplateResolver)2 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1