view src/org/nwoca/ssdt/tools/html2wiki/ReflowTransformer.java @ 6:99f293bd507f

Add "reflow" transformer to reflow paragraphs, list items, etc.
author smith@nwoca.org
date Thu, 27 Jan 2011 16:37:27 -0500
parents
children c1d94c623854
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),
        Pattern.compile("(<li>)(.*?)(</li>)", Pattern.MULTILINE + Pattern.DOTALL),
        Pattern.compile("(<td>)([^<]*)(</td>)", 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";
    }
}