changeset 11:8f23347884c0

Deal with double dashes surrounded by words. ReplaceTransformer can now replace specific group.
author smith@nwoca.org
date Fri, 28 Jan 2011 13:57:25 -0500
parents 2fb5084b1564
children c1d94c623854
files src/org/nwoca/ssdt/tools/html2wiki/Html2Wiki.java src/org/nwoca/ssdt/tools/html2wiki/ReplaceTransformer.java
diffstat 2 files changed, 39 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/nwoca/ssdt/tools/html2wiki/Html2Wiki.java	Fri Jan 28 10:16:57 2011 -0500
+++ b/src/org/nwoca/ssdt/tools/html2wiki/Html2Wiki.java	Fri Jan 28 13:57:25 2011 -0500
@@ -102,6 +102,7 @@
         transformers.add(new ReplaceTransformer(">",">"));
         transformers.add(new ReplaceTransformer(""","\""));
         transformers.add(new ReplaceTransformer(":\\)",": )"));  // No smilies...
+        transformers.add(new ReplaceTransformer("(\\w)(--)(\\w)"," -- ",2));
 
     }
 
--- a/src/org/nwoca/ssdt/tools/html2wiki/ReplaceTransformer.java	Fri Jan 28 10:16:57 2011 -0500
+++ b/src/org/nwoca/ssdt/tools/html2wiki/ReplaceTransformer.java	Fri Jan 28 13:57:25 2011 -0500
@@ -6,19 +6,51 @@
     
     private Pattern replace;
     private String replacement;
-    
+    private int replaceGroup;
+
+    /**
+     * ReplaceTransform to replace the entire regex.
+     *
+     * @param regex
+     * @param replacement
+     */
     public ReplaceTransformer(String regex, String replacement) {
+        this(regex,replacement,0);
+    }
+    /**
+     * ReplaceTransform to replace a specific group in the Regex.
+     *
+     * @param regex
+     * @param replacement
+     * @param replaceGroup group to replace
+     */
+
+    public ReplaceTransformer(String regex, String replacement, int replaceGroup) {
         replace = Pattern.compile(regex);
         this.replacement = replacement;
-        
+        this.replaceGroup = replaceGroup;
     }
+
     public void apply(StringBuffer buffer) {
         Matcher matcher = replace.matcher(buffer);
         
-        boolean first = true;
-        while (matcher.find(first ? 0 : matcher.start() + replacement.length())) {
-            buffer.replace(matcher.start(),matcher.end(),replacement);
-            first = false; 
+        int start  = 0;
+        while (matcher.find(start)) {
+            if (replaceGroup == 0) {
+                buffer.replace(matcher.start(),matcher.end(),replacement);
+                start = matcher.start() + replacement.length();
+            } else {
+                String t = "";
+                for (int i = 1; i < matcher.groupCount() + 1 ; i++) {
+                    if (i != replaceGroup) {
+                        t += matcher.group(i);
+                    } else {
+                        t += replacement;
+                    }
+                }
+                buffer.replace(matcher.start(),matcher.end(),t);
+                start = matcher.start() + t.length();
+            }            
         }        
     }