diff options
Diffstat (limited to 'parsers/Paragraphs.java')
-rw-r--r-- | parsers/Paragraphs.java | 27 |
1 files changed, 19 insertions, 8 deletions
diff --git a/parsers/Paragraphs.java b/parsers/Paragraphs.java index 0d6fda6..295d14a 100644 --- a/parsers/Paragraphs.java +++ b/parsers/Paragraphs.java @@ -3,17 +3,28 @@ package scruf.parsers; import java.util.regex.*; public class Paragraphs implements Parser { - + private String paragraph = "<p>\n $5</p>\n"; public String parse(String fileContent) { - - Pattern pattern = Pattern.compile("((^.+$)\\n)+",Pattern.MULTILINE); + /** + * This regex contains two parts seperated by a '|'; + * the first part is regex for a html Heading (See Heading.java) + * and the second part is the regex for a paragraph. For an input, + * if the first part of the regex is matched, then it is necessarily + * a Heading, so, we ignore it; but if the second part of the regex is + * matched for an input, then it is a paragraph, so, we put the necessary + * tags in place. + */ + Pattern pattern = Pattern.compile("((\\={10,})\\n(.+?)\\n(\\2))|((^.+$\\n)+)",Pattern.MULTILINE); Matcher matcher = pattern.matcher(fileContent); - StringBuilder sbuilder = new StringBuilder(); + StringBuffer sbuffer = new StringBuffer(); while(matcher.find()) { - sbuilder.append("\n<p>\n"); - sbuilder.append(matcher.group()); - sbuilder.append("</p>\n"); + // group 1 contains the regex for the Heading, so + // if that is null, then it means that we have actually + // found a paragraph. + if(matcher.group(1)==null) + matcher.appendReplacement(sbuffer,paragraph); } - return sbuilder.toString(); + matcher.appendTail(sbuffer); + return sbuffer.toString(); } }
\ No newline at end of file |