Reputation: 666
I am using the following code and I am trying to print the following code into HTML:
import com.atlassian.jira.component.ComponentAccessor
import java.text.SimpleDateFormat
import com.opensymphony.util.TextUtils
import com.atlassian.jira.issue.comments.*
import org.w3c.dom.*;
import javax.xml.parsers.*;
import groovy.xml.*
import grrovy.util.*;
import org.xml.sax.InputSource;
import java.io.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.Document;
import org.w3c.dom.DOMException;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
def commentManager = ComponentAccessor.getCommentManager()
Comment comment = commentManager.getLastComment(issue)
if(comment != null) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MMM/yy HH:mm", Locale.ENGLISH)
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
//the line below retrieves {color:#de350b}duchesse{color}
def body = comment.body
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
String html = "<html><body><h1></h1><h1>"+body+"</h1></body></html>";
System.out.println(html); // original
Document doc2 = Jsoup.parse(html); // pretty print HTML
System.out.println(doc2.toString());
return doc2
}
My output is under the form: {color:#de350b}duchesse{color} but I would like the output to be in real displayable HTML which in this case means that only "duchesse" should be displayed in red instead of {color:#de350b}duchesse{color}.
How can I fix this?
Upvotes: 2
Views: 141
Reputation: 2941
You can use regular expression with two capturing groups. The first one will match the color and the second one will get the message. Then you can replace whole matched text with
<font color="......."> ... </font>
.
So after def body = comment.body
use this code:
Pattern p = Pattern.compile("\\{color:(#......)\\}(.*)\\{color\\}");
Matcher m = p.matcher(body);
if (m.find()) {
String color = m.group(1);
String content = m.group(2);
body = body.replace(m.group(0), "<font color=\"" + color + "\">" + content + "</font>");
}
Upvotes: 1