Mercurial > public > html2wiki
view src/org/nwoca/ssdt/tools/html2wiki/ReflowTransformer.java @ 12:c1d94c623854
(none)
author | ferrall@nwoca.org |
---|---|
date | Fri, 28 Jan 2011 14:12:34 -0500 |
parents | 99f293bd507f |
children |
line wrap: on
line source
package org.nwoca.ssdt.tools.html2wiki; import java.util.regex.*; class ReflowTransformer implements Transformer { private Pattern[] patterns = { Pattern.compile("(\\n<p>)(.*?)(\\n\\n|\\n<)", Pattern.MULTILINE + Pattern.DOTALL)}; /** * Default transformer refolows paragraphs, li's and td's. * */ public ReflowTransformer() { } /** * Create transformer with specific regexp. * * Regexp must provide three groups: (before)(textToReflow)(after). * * @param regexp */ public ReflowTransformer(String regexp) { patterns = new Pattern[]{ Pattern.compile(regexp, Pattern.MULTILINE + Pattern.DOTALL) }; } public void apply(StringBuffer buffer) { for (Pattern pattern : patterns) { System.out.println(" Reflowing: " + pattern); Matcher matcher = pattern.matcher(buffer); int start = 0; while (matcher.find(start)) { String temp = matcher.group(2); temp = temp.replaceAll(" \\n", " "); buffer.replace(matcher.start(), matcher.end(), matcher.group(1) + temp + matcher.group(3)); start = matcher.start() + matcher.group(1).length() + temp.length() + matcher.group(3).length() - 1; } } } public String toString() { return "Reflowing block tags"; } }