1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary.parser;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
21 public final class WikiTokenizer {
23 public interface Callback {
24 void onPlainText(final String text);
25 void onMarkup(WikiTokenizer wikiTokenizer);
26 void onWikiLink(WikiTokenizer wikiTokenizer);
27 void onNewline(WikiTokenizer wikiTokenizer);
28 void onFunction(final WikiTokenizer tokenizer, String functionName, List<String> functionPositionArgs,
29 Map<String, String> functionNamedArgs);
30 void onHeading(WikiTokenizer wikiTokenizer);
31 void onListItem(WikiTokenizer wikiTokenizer);
32 void onComment(WikiTokenizer wikiTokenizer);
33 void onHtml(WikiTokenizer wikiTokenizer);
36 public static class DoNothingCallback implements Callback {
39 public void onPlainText(String text) {
43 public void onMarkup(WikiTokenizer wikiTokenizer) {
47 public void onWikiLink(WikiTokenizer wikiTokenizer) {
51 public void onNewline(WikiTokenizer wikiTokenizer) {
55 public void onFunction(WikiTokenizer tokenizer, String functionName,
56 List<String> functionPositionArgs, Map<String, String> functionNamedArgs) {
60 public void onHeading(WikiTokenizer wikiTokenizer) {
64 public void onListItem(WikiTokenizer wikiTokenizer) {
68 public void onComment(WikiTokenizer wikiTokenizer) {
72 public void onHtml(WikiTokenizer wikiTokenizer) {
76 //private static final Pattern wikiTokenEvent = Pattern.compile("($)", Pattern.MULTILINE);
77 private static final Pattern wikiTokenEvent = Pattern.compile("(" +
80 "\\||" + // Need the | because we might have to find unescaped pipes
81 "=|" + // Need the = because we might have to find unescaped =
87 "$)", Pattern.MULTILINE);
88 private static final String listChars = "*#:;";
91 final String wikiText;
92 final Matcher matcher;
94 boolean justReturnedNewline = true;
95 int lastLineStart = 0;
99 final List<String> errors = new ArrayList<>();
100 final List<String> tokenStack = new ArrayList<>();
103 private String headingWikiText;
104 private int headingDepth;
105 private int listPrefixEnd;
106 private boolean isPlainText;
107 private boolean isMarkup;
108 private boolean isComment;
109 private boolean isFunction;
110 private boolean isWikiLink;
111 private boolean isHtml;
112 private int firstUnescapedPipePos;
114 private int lastUnescapedPipePos;
115 private int lastUnescapedEqualsPos;
116 private final List<String> positionArgs = new ArrayList<>();
117 private final Map<String,String> namedArgs = new LinkedHashMap<>();
120 public WikiTokenizer(final String wikiText) {
121 this(wikiText, true);
124 public WikiTokenizer(String wikiText, final boolean isNewline) {
125 wikiText = wikiText.replace('\u2028', '\n');
126 wikiText = wikiText.replace('\u0085', '\n');
127 this.wikiText = wikiText;
128 this.matcher = wikiTokenEvent.matcher(wikiText);
129 justReturnedNewline = isNewline;
132 private void clear() {
136 headingWikiText = null;
146 firstUnescapedPipePos = -1;
147 lastUnescapedPipePos = -1;
148 lastUnescapedEqualsPos = -1;
149 positionArgs.clear();
153 private static final Pattern POSSIBLE_WIKI_TEXT = Pattern.compile(
164 public static void dispatch(final String wikiText, final boolean isNewline, final Callback callback) {
166 if (!POSSIBLE_WIKI_TEXT.matcher(wikiText).find()) {
167 callback.onPlainText(wikiText);
169 final WikiTokenizer tokenizer = new WikiTokenizer(wikiText, isNewline);
170 while (tokenizer.nextToken() != null) {
171 if (tokenizer.isPlainText()) {
172 callback.onPlainText(tokenizer.token());
173 } else if (tokenizer.isMarkup()) {
174 callback.onMarkup(tokenizer);
175 } else if (tokenizer.isWikiLink()) {
176 callback.onWikiLink(tokenizer);
177 } else if (tokenizer.isNewline()) {
178 callback.onNewline(tokenizer);
179 } else if (tokenizer.isFunction()) {
180 callback.onFunction(tokenizer, tokenizer.functionName(), tokenizer.functionPositionArgs(), tokenizer.functionNamedArgs());
181 } else if (tokenizer.isHeading()) {
182 callback.onHeading(tokenizer);
183 } else if (tokenizer.isListItem()) {
184 callback.onListItem(tokenizer);
185 } else if (tokenizer.isComment()) {
186 callback.onComment(tokenizer);
187 } else if (tokenizer.isHtml()) {
188 callback.onHtml(tokenizer);
189 } else if (!tokenizer.errors.isEmpty()) {
190 // Log was already printed....
192 throw new IllegalStateException("Unknown wiki state: " + tokenizer.token());
198 public List<String> errors() {
202 public boolean isNewline() {
203 return justReturnedNewline;
206 public void returnToLineStart() {
207 end = start = lastLineStart;
208 justReturnedNewline = true;
211 public boolean isHeading() {
212 return headingWikiText != null;
215 public String headingWikiText() {
217 return headingWikiText;
220 public int headingDepth() {
225 public boolean isMarkup() {
229 public boolean isComment() {
233 public boolean isListItem() {
234 return listPrefixEnd != -1;
237 public String listItemPrefix() {
239 return wikiText.substring(start, listPrefixEnd);
242 public static String getListTag(char c) {
249 public String listItemWikiText() {
251 return wikiText.substring(listPrefixEnd, end);
254 public boolean isFunction() {
258 public String functionName() {
261 if (firstUnescapedPipePos != -1) {
262 return trimNewlines(wikiText.substring(start + 2, firstUnescapedPipePos).trim());
264 final int safeEnd = Math.max(start + 2, end - 2);
265 return trimNewlines(wikiText.substring(start + 2, safeEnd).trim());
268 public List<String> functionPositionArgs() {
272 public Map<String, String> functionNamedArgs() {
276 public boolean isPlainText() {
280 public boolean isWikiLink() {
284 public String wikiLinkText() {
287 if (lastUnescapedPipePos != -1) {
288 return trimNewlines(wikiText.substring(lastUnescapedPipePos + 1, end - 2));
290 assert start + 2 < wikiText.length() && end >= 2: wikiText;
291 return trimNewlines(wikiText.substring(start + 2, end - 2));
294 public String wikiLinkDest() {
297 if (firstUnescapedPipePos != -1) {
298 return trimNewlines(wikiText.substring(start + 2, firstUnescapedPipePos));
303 public boolean isHtml() {
307 public boolean remainderStartsWith(final String prefix) {
308 return wikiText.startsWith(prefix, start);
311 public void nextLine() {
312 final int oldStart = start;
313 while(nextToken() != null && !isNewline()) {}
321 public WikiTokenizer nextToken() {
326 if (justReturnedNewline) {
327 lastLineStart = start;
332 final int len = wikiText.length();
337 // Eat a newline if we're looking at one:
338 final boolean atNewline = wikiText.charAt(end) == '\n' || wikiText.charAt(end) == '\u2028' || wikiText.charAt(end) == '\u2029';
340 justReturnedNewline = true;
345 if (justReturnedNewline) {
346 justReturnedNewline = false;
348 final char firstChar = wikiText.charAt(end);
349 if (firstChar == '=') {
350 final int headerStart = end;
352 while (++end < len && wikiText.charAt(end) == '=') {}
353 final int headerTitleStart = end;
354 headingDepth = headerTitleStart - headerStart;
357 final int nextNewline = safeIndexOf(wikiText, end, "\n", "\n");
358 final int closingEquals = escapedFindEnd(end, "=");
359 if (wikiText.charAt(closingEquals - 1) == '=') {
360 end = closingEquals - 1;
365 final int headerTitleEnd = end;
366 headingWikiText = wikiText.substring(headerTitleStart, headerTitleEnd);
368 while (end < len && ++end < len && wikiText.charAt(end) == '=') {}
369 final int headerEnd = end;
370 if (headerEnd - headerTitleEnd != headingDepth) {
371 errors.add("Mismatched header depth: " + token());
375 if (listChars.indexOf(firstChar) != -1) {
376 while (++end < len && listChars.indexOf(wikiText.charAt(end)) != -1) {}
378 end = escapedFindEnd(start, "\n");
383 if (wikiText.startsWith("'''", start)) {
389 if (wikiText.startsWith("''", start)) {
395 if (wikiText.startsWith("[[", start)) {
396 end = escapedFindEnd(start + 2, "]]");
397 isWikiLink = errors.isEmpty();
401 if (wikiText.startsWith("{{", start)) {
402 end = escapedFindEnd(start + 2, "}}");
403 isFunction = errors.isEmpty();
407 if (wikiText.startsWith("<pre>", start)) {
408 end = safeIndexOf(wikiText, start, "</pre>", "\n");
413 if (wikiText.startsWith("<ref>", start)) {
414 end = safeIndexOf(wikiText, start, "</ref>", "\n");
419 if (wikiText.startsWith("<math>", start)) {
420 end = safeIndexOf(wikiText, start, "</math>", "\n");
425 if (wikiText.startsWith("<!--", start)) {
427 end = safeIndexOf(wikiText, start, "-->", "\n");
431 if (wikiText.startsWith("}}", start) || wikiText.startsWith("]]", start)) {
432 errors.add("Close without open!");
437 if (wikiText.charAt(start) == '|' || wikiText.charAt(start) == '=') {
444 if (this.matcher.find(start)) {
445 end = this.matcher.start(1);
448 // stumbled over a new type of newline?
449 // Or matcher is out of sync with checks above
450 errors.add("Empty group: " + this.matcher.group() + " char: " + (int)wikiText.charAt(end));
452 throw new RuntimeException("matcher not in sync with code, or new type of newline, errors :" + errors);
457 end = wikiText.length();
461 if (!errors.isEmpty()) {
462 System.err.println("Errors: " + errors + ", token=" + token());
468 public String token() {
469 final String token = wikiText.substring(start, end);
470 assert token.equals("\n") || !token.endsWith("\n") : "token='" + token + "'";
474 static final String[] patterns = { "\n", "{{", "}}", "[[", "]]", "[", "]", "|", "=", "<!--" };
475 private int escapedFindEnd(final int start, final String toFind) {
476 assert tokenStack.isEmpty();
478 final boolean insideFunction = toFind.equals("}}");
481 int firstNewline = -1;
482 int[] nextMatch = new int[patterns.length];
483 Arrays.fill(nextMatch, -2);
484 int singleBrackets = 0;
485 while (end < wikiText.length()) {
486 // Manual replacement for matcher.find(end),
487 // because Java regexp is a ridiculously slow implementation.
488 // Initialize to always match the end.
490 for (int i = 0; i < nextMatch.length; ++i) {
491 if (nextMatch[i] <= end) {
492 nextMatch[i] = wikiText.indexOf(patterns[i], end);
493 if (nextMatch[i] == -1) nextMatch[i] = i > 0 ? 0x7fffffff : wikiText.length();
495 if (nextMatch[i] < nextMatch[matchIdx]) {
500 int matchStart = nextMatch[matchIdx];
501 String matchText = patterns[matchIdx];
502 int matchEnd = matchStart + matchText.length();
505 matchEnd = matchStart;
508 assert matchEnd > end || matchText.length() == 0: "Group=" + matchText;
509 if (matchText.length() == 0) {
510 assert matchStart == wikiText.length() || wikiText.charAt(matchStart) == '\n' : wikiText + ", " + matchStart;
511 if (firstNewline == -1) {
512 firstNewline = matchEnd;
514 if (tokenStack.isEmpty() && toFind.equals("\n")) {
518 } else if (tokenStack.isEmpty() && matchText.equals(toFind)) {
519 // The normal return....
520 if (insideFunction) {
521 addFunctionArg(insideFunction, matchStart);
524 } else if (matchText.equals("[")) {
526 } else if (matchText.equals("]")) {
527 if (singleBrackets > 0) singleBrackets--;
528 } else if (matchText.equals("[[") || matchText.equals("{{")) {
529 tokenStack.add(matchText);
530 } else if (matchText.equals("]]") || matchText.equals("}}")) {
531 if (tokenStack.size() > 0) {
532 final String removed = tokenStack.remove(tokenStack.size() - 1);
533 if (removed.equals("{{") && !matchText.equals("}}")) {
534 if (singleBrackets >= 2) { // assume this is really two closing single ]
536 tokenStack.add(removed);
538 errors.add("Unmatched {{ error: " + wikiText.substring(start, matchEnd));
539 return safeIndexOf(wikiText, start, "\n", "\n");
541 } else if (removed.equals("[[") && !matchText.equals("]]")) {
542 errors.add("Unmatched [[ error: " + wikiText.substring(start, matchEnd));
543 return safeIndexOf(wikiText, start, "\n", "\n");
546 errors.add("Pop too many " + matchText + " error: " + wikiText.substring(start, matchEnd).replace("\n", "\\\\n"));
547 // If we were looking for a newline
548 return safeIndexOf(wikiText, start, "\n", "\n");
550 } else if (matchText.equals("|")) {
551 if (tokenStack.isEmpty()) {
552 addFunctionArg(insideFunction, matchStart);
554 } else if (matchText.equals("=")) {
555 if (tokenStack.isEmpty()) {
556 lastUnescapedEqualsPos = matchStart;
558 // Do nothing. These can match spuriously, and if it's not the thing
559 // we're looking for, keep on going.
560 } else if (matchText.equals("<!--")) {
561 end = wikiText.indexOf("-->", matchStart);
563 errors.add("Unmatched <!-- error: " + wikiText.substring(start));
564 return safeIndexOf(wikiText, start, "\n", "\n");
566 } else if (matchText.equals("''") || (matchText.startsWith("<") && matchText.endsWith(">"))) {
569 assert false : "Match text='" + matchText + "'";
570 throw new IllegalStateException();
573 // Inside the while loop. Just go forward.
574 end = Math.max(end, matchEnd);
576 if (toFind.equals("\n") && tokenStack.isEmpty()) {
577 // We were looking for the end, we got it.
580 errors.add("Couldn't find: " + (toFind.equals("\n") ? "newline" : toFind) + ", "+ wikiText.substring(start));
581 if (firstNewline != -1) {
587 private void addFunctionArg(final boolean insideFunction, final int matchStart) {
588 if (firstUnescapedPipePos == -1) {
589 firstUnescapedPipePos = lastUnescapedPipePos = matchStart;
590 } else if (insideFunction) {
591 if (lastUnescapedEqualsPos > lastUnescapedPipePos) {
592 final String key = wikiText.substring(lastUnescapedPipePos + 1, lastUnescapedEqualsPos);
593 final String value = wikiText.substring(lastUnescapedEqualsPos + 1, matchStart);
594 namedArgs.put(trimNewlines(key), trimNewlines(value));
596 final String value = wikiText.substring(lastUnescapedPipePos + 1, matchStart);
597 positionArgs.add(trimNewlines(value));
600 lastUnescapedPipePos = matchStart;
603 static String trimNewlines(String s) {
604 while (s.startsWith("\n")) {
607 while (s.endsWith("\n")) {
608 s = s.substring(0, s.length() - 1);
610 return s.replace('\n', ' ');
613 static int safeIndexOf(final String s, final int start, final String target, final String backup) {
614 int close = s.indexOf(target, start);
616 // Don't step over a \n.
617 return close + (target.equals("\n") ? 0 : target.length());
619 close = s.indexOf(backup, start);
621 return close + (backup.equals("\n") ? 0 : backup.length());
626 public static String toPlainText(final String wikiText) {
627 final WikiTokenizer wikiTokenizer = new WikiTokenizer(wikiText);
628 final StringBuilder builder = new StringBuilder();
629 while (wikiTokenizer.nextToken() != null) {
630 if (wikiTokenizer.isPlainText()) {
631 builder.append(wikiTokenizer.token());
632 } else if (wikiTokenizer.isWikiLink()) {
633 builder.append(wikiTokenizer.wikiLinkText());
634 } else if (wikiTokenizer.isNewline()) {
635 builder.append("\n");
636 } else if (wikiTokenizer.isFunction()) {
637 builder.append(wikiTokenizer.token());
640 return builder.toString();
643 public static StringBuilder appendFunction(final StringBuilder builder, final String name, List<String> args,
644 final Map<String, String> namedArgs) {
645 builder.append(name);
646 for (final String arg : args) {
647 builder.append("|").append(arg);
649 for (final Map.Entry<String, String> entry : namedArgs.entrySet()) {
650 builder.append("|").append(entry.getKey()).append("=").append(entry.getValue());