// define the rules that convert tweets to HTML
StringFilter filter = StringFilter.builder()
.addRule("http://[A-Za-z0-9_\\-\\~\\.\\%\\?\\#\\@/]+", m -> {
String url = m.group(0);
return "<a href=\""
+ escapeHtml4(url) +
"\">" + escapeHtml4(url) + "</a>";
})
.addRule("((?:^|\\s)@)([A-Za-z0-9_]+)", m -> {
String prefix = m.group(1);
String user = m.group(2);
return prefix + "<a href=\"http://twitter.com/"
+ escapeHtml4(user) +
"\">" + escapeHtml4(user) + "</a>";
})
.addRule("((?:^|\\s))(#[A-Za-z0-9_]+)", m -> {
String prefix = m.group(1);
String hashtag = m.group(2);
return prefix + "<a href=\"http://twitter.com/search?q="
+ escapeHtml4(hashtag)
+ "\">"
+ escapeHtml4(hashtag)
+ "</a>";
})
.defaultRule(StringEscapeUtils::escapeHtml4)
.build();
// convert a tweet to HTML
String html = filter.filter("Hi, @kazuho");
The module is a regexp-based string filter, that can merge multiple conversion rules for converting strings. The primary target of the module is to convert inline markups (such as the tweets of Twitter) to HTML.
This library was uploaded on maven central.
See search.maven.org for more details.
- Java 8+
The MIT License (MIT)
Copyright © 2016 Tokuhiro Matsuno, http://64p.org/ <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
- Original perl implementation String::Filter