Mercurial > public > html2wiki
comparison 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 |
comparison
equal
deleted
inserted
replaced
5:d34f4d408ef9 | 6:99f293bd507f |
---|---|
1 package org.nwoca.ssdt.tools.html2wiki; | |
2 | |
3 import java.util.regex.*; | |
4 | |
5 class ReflowTransformer implements Transformer { | |
6 | |
7 private Pattern[] patterns = { | |
8 Pattern.compile("(\\n<p>)(.*?)(\\n\\n|\\n<)", Pattern.MULTILINE + Pattern.DOTALL), | |
9 Pattern.compile("(<li>)(.*?)(</li>)", Pattern.MULTILINE + Pattern.DOTALL), | |
10 Pattern.compile("(<td>)([^<]*)(</td>)", Pattern.MULTILINE + Pattern.DOTALL) | |
11 }; | |
12 | |
13 /** | |
14 * Default transformer refolows paragraphs, li's and td's. | |
15 * | |
16 */ | |
17 public ReflowTransformer() { | |
18 } | |
19 | |
20 /** | |
21 * Create transformer with specific regexp. | |
22 * | |
23 * Regexp must provide three groups: (before)(textToReflow)(after). | |
24 * | |
25 * @param regexp | |
26 */ | |
27 public ReflowTransformer(String regexp) { | |
28 patterns = new Pattern[]{ | |
29 Pattern.compile(regexp, Pattern.MULTILINE + Pattern.DOTALL) | |
30 }; | |
31 } | |
32 | |
33 public void apply(StringBuffer buffer) { | |
34 for (Pattern pattern : patterns) { | |
35 | |
36 System.out.println(" Reflowing: " + pattern); | |
37 Matcher matcher = pattern.matcher(buffer); | |
38 | |
39 int start = 0; | |
40 while (matcher.find(start)) { | |
41 String temp = matcher.group(2); | |
42 temp = temp.replaceAll(" \\n", " "); | |
43 buffer.replace(matcher.start(), matcher.end(), matcher.group(1) + temp + matcher.group(3)); | |
44 start = matcher.start() + matcher.group(1).length() + temp.length() + matcher.group(3).length() - 1; | |
45 } | |
46 } | |
47 | |
48 } | |
49 | |
50 public String toString() { | |
51 return "Reflowing block tags"; | |
52 } | |
53 } |