Reputation: 3547
I need to transform strings one into another. An example is:
#Fashion #Helmet #Woman #Scifi [  ](http://bit.ly/P1omax) Rob Goodwin
That has to be transformed into the HTML code:
#Fashion #Helmet #Woman #Scifi<a href="http://bit.ly/P1omax"><img src="http://24.media.tumblr.com/tumblr_mbv08xkdzy1qfzis2o1_1280.gif" /></a>
Is there a Java library that does that?
UPDATE
It looks very similar to Markdown. But processing it with markdownj produces a messy HTML:
<h1>Fashion #Helmet #Woman #Scifi</h1>
<p><a href="a href="http://24.media.tumblr.com/tumblr_mbv08xkdzy1qfzis2o1_1280.gif">http://24.media.tumblr.com/tumblr_mbv08xkdzy1qfzis2o1_1280.gif</a"> <img src="<a href="http://bit.ly/P1omax">http://bit.ly/P1omax</a>" alt="t</a> " />
Rob Goodwin</p>
UPDATE 2
All three Java libraries that allow to transform Markdown into HTML have problems recognising images inside links.
The approach that I decided to use is a two-step approach:
It is not an ideal solution, but it works.
Upvotes: 0
Views: 1280
Reputation: 2643
You could use regular expressions to extract the URLs from the text, and then concatenate into a template output string.
There are many examples of the regex needed to extract URLS, this for example.
Using the above method, you could write something like:
String[] split = yourInput.split("[ ;
String[] urls = pullLinks(yourInput)
String output = split[0] + "<a href=\"" + urls[1] + "\"><img src=\"" + urls[0] + "\" /></a>";
Upvotes: 1
Reputation: 2531
Is the inputstring markdown? Yes it works in http://daringfireball.net/projects/markdown/dingus which generates
<h1>Fashion #Helmet #Woman #Scifi <a href="http://bit.ly/P1omax"> <img src="http://24.media.tumblr.com/tumblr_mbv08xkdzy1qfzis2o1_1280.gif" alt="t" title="" /> </a> Rob Goodwin</h1>
Therefore: http://code.google.com/p/markdownj/ Or: http://en.wikipedia.org/wiki/List_of_Markdown_implementations
https://github.com/sirthias/pegdown looks ok. they claim:
[pegdown] fully passes the original Markdown test suite
Upvotes: 2
Reputation: 431
Your best bet would be to look at using XSLT. Java includes XSLT as part of the JAXP library and this will allow you to define pattern matching and transformation rules, using XSL, and apply them to input text. Eclipse has an excellent tool to allow you to build and test XSL.
Upvotes: 0