Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d2b2c8afd9a5b0c1131fd7b02de2cabb7ceca27c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*******************************************************************************
 * Copyright (c) 2010, 2012 David Green and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     David Green - initial API and implementation
 *     Jeremie Bresson - Bug 379783
 *******************************************************************************/

package org.eclipse.mylyn.internal.wikitext.mediawiki.core;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.mylyn.wikitext.mediawiki.core.Template;
import org.eclipse.mylyn.wikitext.mediawiki.core.TemplateResolver;

public class TemplateProcessor {

	private static final Pattern templatePattern = Pattern.compile("(?:^|(?<!\\{))(\\{\\{(#?[a-zA-Z0-9_ :]+)\\s*(\\|[^\\}]*)?\\}\\})"); //$NON-NLS-1$

	private static final Pattern templateParameterPattern = Pattern.compile("\\{\\{\\{([a-zA-Z0-9]+)\\}\\}\\}"); //$NON-NLS-1$

	private static final Pattern parameterSpec = Pattern.compile("\\|\\s*([^\\|=]+)(?:\\s*=\\s*(([^|]*)))?"); //$NON-NLS-1$

	private static final Pattern includeOnlyPattern = Pattern.compile(".*?<includeonly>(.*?)</includeonly>.*", //$NON-NLS-1$
			Pattern.DOTALL);

	private static final Pattern noIncludePattern = Pattern.compile("<noinclude>(.*?)</noinclude>", Pattern.DOTALL); //$NON-NLS-1$

	private final AbstractMediaWikiLanguage mediaWikiLanguage;

	private final Map<String, Template> templateByName = new HashMap<String, Template>();

	private final List<Pattern> excludePatterns = new ArrayList<Pattern>();

	public TemplateProcessor(AbstractMediaWikiLanguage abstractMediaWikiLanguage) {
		this.mediaWikiLanguage = abstractMediaWikiLanguage;

		for (Template template : mediaWikiLanguage.getTemplates()) {
			templateByName.put(template.getName(), normalize(template));
		}
		String templateExcludes = abstractMediaWikiLanguage.getTemplateExcludes();
		if (templateExcludes != null) {
			String[] split = templateExcludes.split("\\s*,\\s*"); //$NON-NLS-1$
			for (String exclude : split) {
				String pattern = exclude.replaceAll("([^a-zA-Z:\\*])", "\\\\$1").replaceAll("\\*", ".*?"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
				excludePatterns.add(Pattern.compile(pattern, Pattern.CASE_INSENSITIVE));
			}
		}
	}

	public String processTemplates(String markupContent) {
		return processTemplates(markupContent, Collections.<String> emptySet());
	}

	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();
					sb.append("<span class=\"error\">"); //$NON-NLS-1$
					sb.append(MessageFormat.format(
							Messages.getString("TemplateProcessor_loopDetected"), template.getName())); //$NON-NLS-1$
					sb.append("</span>"); //$NON-NLS-1$
					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();
	}

	private String processTemplate(Template template, String parametersText) {
		if (template.getTemplateMarkup() == null) {
			return ""; //$NON-NLS-1$
		}
		String macro = template.getTemplateContent();

		List<Parameter> parameters = processParameters(parametersText);

		StringBuilder processedMarkup = new StringBuilder();
		int lastIndex = 0;
		Matcher matcher = templateParameterPattern.matcher(macro);
		while (matcher.find()) {
			int start = matcher.start();
			if (lastIndex < start) {
				processedMarkup.append(macro.substring(lastIndex, start));
			}
			String parameterName = matcher.group(1);
			String parameterValue = null;
			try {
				int parameterIndex = Integer.parseInt(parameterName);
				if (parameterIndex <= parameters.size() && parameterIndex > 0) {
					parameterValue = parameters.get(parameterIndex - 1).value;
				}
			} catch (NumberFormatException e) {
				for (Parameter param : parameters) {
					if (parameterName.equalsIgnoreCase(param.name)) {
						parameterValue = param.value;
						break;
					}
				}
			}
			if (parameterValue != null) {
				processedMarkup.append(parameterValue);
			}

			lastIndex = matcher.end();
		}
		if (lastIndex == 0) {
			return macro;
		}
		if (lastIndex < macro.length()) {
			processedMarkup.append(macro.substring(lastIndex));
		}
		return processedMarkup.toString();
	}

	private List<Parameter> processParameters(String parametersText) {
		List<Parameter> parameters = new ArrayList<TemplateProcessor.Parameter>();
		if (parametersText != null && parametersText.length() > 0) {
			Matcher matcher = parameterSpec.matcher(parametersText);
			while (matcher.find()) {
				String nameOrValue = matcher.group(1);
				String value = matcher.group(2);
				Parameter parameter = new Parameter();
				if (value != null) {
					parameter.name = nameOrValue;
					parameter.value = value;
				} else {
					parameter.value = nameOrValue;
				}
				parameters.add(parameter);
			}
		}
		return parameters;
	}

	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);
				template.setTemplateMarkup(""); //$NON-NLS-1$
			}
			templateByName.put(template.getName(), template);
		}
		return template;
	}

	private Template normalize(Template template) {
		Template normalizedTemplate = new Template();
		normalizedTemplate.setName(template.getName());
		normalizedTemplate.setTemplateMarkup(normalizeTemplateMarkup(template.getTemplateContent()));

		return normalizedTemplate;
	}

	private String normalizeTemplateMarkup(String templateMarkup) {
		Matcher matcher = includeOnlyPattern.matcher(templateMarkup);
		if (matcher.matches()) {
			return matcher.group(1);
		}
		matcher = noIncludePattern.matcher(templateMarkup);
		return matcher.replaceAll(""); //$NON-NLS-1$
	}

	private static class Parameter {
		String name;

		String value;
	}
}

Back to the top