Mercurial > public > html2wiki
annotate src/org/nwoca/ssdt/tools/html2wiki/ReflowTransformer.java @ 7:a634b4d554d4
Minor fixups >, random smilies :), etc. Fixed blockquote. Handle escaping brackets outside pre tag.
author | smith@nwoca.org |
---|---|
date | Thu, 27 Jan 2011 18:07:28 -0500 |
parents | 99f293bd507f |
children | c1d94c623854 |
rev | line source |
---|---|
6
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
1 package org.nwoca.ssdt.tools.html2wiki; |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
2 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
3 import java.util.regex.*; |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
4 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
5 class ReflowTransformer implements Transformer { |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
6 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
7 private Pattern[] patterns = { |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
8 Pattern.compile("(\\n<p>)(.*?)(\\n\\n|\\n<)", Pattern.MULTILINE + Pattern.DOTALL), |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
9 Pattern.compile("(<li>)(.*?)(</li>)", Pattern.MULTILINE + Pattern.DOTALL), |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
10 Pattern.compile("(<td>)([^<]*)(</td>)", Pattern.MULTILINE + Pattern.DOTALL) |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
11 }; |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
12 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
13 /** |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
14 * Default transformer refolows paragraphs, li's and td's. |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
15 * |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
16 */ |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
17 public ReflowTransformer() { |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
18 } |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
19 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
20 /** |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
21 * Create transformer with specific regexp. |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
22 * |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
23 * Regexp must provide three groups: (before)(textToReflow)(after). |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
24 * |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
25 * @param regexp |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
26 */ |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
27 public ReflowTransformer(String regexp) { |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
28 patterns = new Pattern[]{ |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
29 Pattern.compile(regexp, Pattern.MULTILINE + Pattern.DOTALL) |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
30 }; |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
31 } |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
32 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
33 public void apply(StringBuffer buffer) { |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
34 for (Pattern pattern : patterns) { |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
35 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
36 System.out.println(" Reflowing: " + pattern); |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
37 Matcher matcher = pattern.matcher(buffer); |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
38 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
39 int start = 0; |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
40 while (matcher.find(start)) { |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
41 String temp = matcher.group(2); |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
42 temp = temp.replaceAll(" \\n", " "); |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
43 buffer.replace(matcher.start(), matcher.end(), matcher.group(1) + temp + matcher.group(3)); |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
44 start = matcher.start() + matcher.group(1).length() + temp.length() + matcher.group(3).length() - 1; |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
45 } |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
46 } |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
47 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
48 } |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
49 |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
50 public String toString() { |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
51 return "Reflowing block tags"; |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
52 } |
99f293bd507f
Add "reflow" transformer to reflow paragraphs, list items, etc.
smith@nwoca.org
parents:
diff
changeset
|
53 } |