scruf

darn simple HTML5 doc generator
git clone git://git.ricketyspace.net/scruf.git
Log | Files | Refs | README

commit e3b2d9aa86d97154421e753d91856bc73a4be591
parent 1a886ec82f13a01253e5611aeb18e3c845e2668d
Author: rsiddharth <rsiddharth@ninthfloor.org>
Date:   Mon, 25 Jun 2012 22:10:31 +0530

Images.java && Links.java (bug fix) : regex modified to properly parse the respective mark-up when the marked-uped a a 
whole runs into more than one line. Before this modification the "marked-uped" lines which ran into two (or more) 
different lines were not properly parsed, by these parsers, and thus was not properly converted. With this fix, things 
_seem_ to work good.

Diffstat:
parsers/Images.java | 9++++++---
parsers/Links.java | 19++++++++++++++++---
2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/parsers/Images.java b/parsers/Images.java @@ -3,8 +3,11 @@ package scruf.parsers; import java.util.regex.*; public class Images implements Parser { + // set of strings to build the img tag + private String openTag = "<img src=\"$1\""; + private String closeTag = " />"; public String parse(String fileContent) { - Pattern pattern = Pattern.compile("\\{\\{(.+\\.(png|jpg))(\\|(.+))?\\}\\}"); + Pattern pattern = Pattern.compile("\\{\\{(.+\\.(png|jpg))(\\|(.+))?\\}\\}", Pattern.DOTALL); Matcher matcher = pattern.matcher(fileContent); StringBuffer sbuffer = new StringBuffer(); StringBuilder replacementString =new StringBuilder(); @@ -12,13 +15,13 @@ public class Images implements Parser { // empty the builder. replacementString.delete(0,replacementString.length()); // add the _img_ tag - replacementString.append("<img src=\"$1\""); + replacementString.append(openTag); // if the "title" is given add to the _img_ tag. if(matcher.group(3)!=null) { replacementString.append("alt=\"$4\" title=\"$4\""); } // close the _img_ tag. - replacementString.append(" />"); + replacementString.append(closeTag); matcher.appendReplacement(sbuffer,replacementString.toString()); } matcher.appendTail(sbuffer); diff --git a/parsers/Links.java b/parsers/Links.java @@ -3,13 +3,26 @@ package scruf.parsers; import java.util.regex.*; public class Links implements Parser { + // set of strings to build the html link + private String openTag = "<a href=\"$1\" target=\"_blank\">"; + private String closeTag = "</a>"; public String parse(String fileContent) { - Pattern pattern = Pattern.compile("\\[\\[(.+?)\\|(.+?)\\]\\]"); + Pattern pattern = Pattern.compile("\\[\\[(.+?)(\\|(.+?))\\]\\]", Pattern.DOTALL); Matcher matcher = pattern.matcher(fileContent); StringBuffer sbuffer = new StringBuffer(); - String replacementString = "<a href=\"$1\" target=\"_blank\">$2</a>"; + StringBuilder replacementString = new StringBuilder(); while(matcher.find()) { - matcher.appendReplacement(sbuffer,replacementString); + // delete text already there. + replacementString.delete(0,replacementString.length()); + // start the <a> tag. + replacementString.append(openTag); + // add link name, if given + if(matcher.group(3)!=null) { + replacementString.append(matcher.group(3)); + } + // close the <a> tag + replacementString.append(closeTag); + matcher.appendReplacement(sbuffer,replacementString.toString()); } matcher.appendTail(sbuffer); return sbuffer.toString();