From 8d638163a677d1ae0b831ca7a50f3365d6f2aafa Mon Sep 17 00:00:00 2001 From: Thad Hughes Date: Sat, 15 Oct 2011 18:37:31 -0700 Subject: [PATCH] go --- .../dictionary/parser/WikiFunction.java | 90 +++++++++++++++++++ .../dictionary/parser/WikiHeading.java | 28 ++++++ 2 files changed, 118 insertions(+) create mode 100644 src/com/hughes/android/dictionary/parser/WikiFunction.java create mode 100644 src/com/hughes/android/dictionary/parser/WikiHeading.java diff --git a/src/com/hughes/android/dictionary/parser/WikiFunction.java b/src/com/hughes/android/dictionary/parser/WikiFunction.java new file mode 100644 index 0000000..e52a470 --- /dev/null +++ b/src/com/hughes/android/dictionary/parser/WikiFunction.java @@ -0,0 +1,90 @@ +package com.hughes.android.dictionary.parser; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.sun.org.apache.bcel.internal.generic.NamedAndTyped; + +public class WikiFunction { + + public int start; + public int end; + public String name = ""; + public final List args = new ArrayList();; + public final Map namedArgs = new LinkedHashMap(); + + private static final Pattern functionEvent = Pattern.compile("\\{\\{|\\[\\[|\\}\\}|\\]\\]|=|\\|"); + + public static WikiFunction getFunction(String line) { + final int start = line.indexOf("{{"); + if (start == -1) { + return null; + } + final WikiFunction result = new WikiFunction(); + result.start = start; + + final Matcher matcher = functionEvent.matcher(line); + int depth = 1; + int end = start + 2; + int lastPipe = end; + int lastEquals = -1; + while (end < line.length() && matcher.find(end)) { + end = matcher.end(); + if (matcher.group().equals("{{") || matcher.group().equals("[[")) { + ++depth; + } else if (matcher.group().equals("}}") || matcher.group().equals("]]")) { + --depth; + if (depth == 0) { + break; + } + } else if (matcher.group().equals("|") && depth == 1) { + if (lastEquals != -1) { + result.namedArgs.put(line.substring(lastPipe, lastEquals), line.substring(lastEquals + 1, matcher.start())); + } else { + result.args.add(line.substring(lastPipe, matcher.start())); + } + lastPipe = matcher.end(); + lastEquals = -1; + } else if (matcher.group().equals("=") && depth == 1) { + lastEquals = matcher.start(); + } + } + if (depth > 0) { + System.err.println("Invalid function: " + line); + return null; + } + + if (lastEquals != -1) { + result.namedArgs.put(line.substring(lastPipe, lastEquals), line.substring(lastEquals + 1, matcher.start())); + } else { + result.args.add(line.substring(lastPipe, matcher.start())); + } + result.end = matcher.end(); + if (result.args.size() > 0) { + result.name = result.args.remove(0); + } else { + System.err.println("Funnction unnamed: " + line); + } + + return result; + } + + public String getArg(final int pos) { + return (pos < args.size()) ? args.get(pos) : null; + } + + public String getNamedArg(final String name) { + return namedArgs.get(name); + } + + public String replaceWith(final String line, final String sub) { + return line.substring(0, start) + sub + line.substring(end); + } + + + +} diff --git a/src/com/hughes/android/dictionary/parser/WikiHeading.java b/src/com/hughes/android/dictionary/parser/WikiHeading.java new file mode 100644 index 0000000..b8ca6f9 --- /dev/null +++ b/src/com/hughes/android/dictionary/parser/WikiHeading.java @@ -0,0 +1,28 @@ +package com.hughes.android.dictionary.parser; + +public class WikiHeading { + public final int depth; + public final String name; + + public WikiHeading(int depth, String name) { + this.depth = depth; + this.name = name; + } + + public static WikiHeading getHeading(String line) { + line = line.trim(); + if (!line.startsWith("=")) { + return null; + } + int i = 0; + for (; i < line.length() && line.charAt(i) == '='; ++i) { + } + final String prefix = line.substring(0, i); + if (!line.substring(i).endsWith(prefix) || line.charAt(line.length() - i - 1) == '=') { + System.err.println("Invalid heading: " + line); + return null; + } + return new WikiHeading(i, line.substring(i, line.length() - i).trim()); + } + +} -- 2.43.0