]> gitweb.fperrin.net Git - DictionaryPC.git/commitdiff
go
authorThad Hughes <thad.hughes@gmail.com>
Sun, 16 Oct 2011 01:37:31 +0000 (18:37 -0700)
committerThad Hughes <thad.hughes@gmail.com>
Tue, 13 Dec 2011 18:39:43 +0000 (10:39 -0800)
src/com/hughes/android/dictionary/parser/WikiFunction.java [new file with mode: 0644]
src/com/hughes/android/dictionary/parser/WikiHeading.java [new file with mode: 0644]

diff --git a/src/com/hughes/android/dictionary/parser/WikiFunction.java b/src/com/hughes/android/dictionary/parser/WikiFunction.java
new file mode 100644 (file)
index 0000000..e52a470
--- /dev/null
@@ -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<String> args = new ArrayList<String>();;
+  public final Map<String,String> namedArgs = new LinkedHashMap<String, String>();
+  
+  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 (file)
index 0000000..b8ca6f9
--- /dev/null
@@ -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());
+  }
+  
+}