Skip to content
Snippets Groups Projects
Commit c29d5a17 authored by gnaegi's avatar gnaegi
Browse files

OO-246 implement url formatter to parse for url's in texts and replace them with links

parent 8ea20ffc
No related branches found
No related tags found
No related merge requests found
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
<p class="o_item_info">$info.getInfos()#if($info.isModified()), <span class="o_item_info_mod">$info.getModifier()</span>#end</p> <p class="o_item_info">$info.getInfos()#if($info.isModified()), <span class="o_item_info_mod">$info.getModifier()</span>#end</p>
<p></p> <p></p>
#if($info.getMessage()) #if($info.getMessage())
<p>$info.getMessage()</p> <p>$r.formatURLsAsLinks($info.getMessage())</p>
#end #end
</div> </div>
#end #end
......
...@@ -595,6 +595,17 @@ public class VelocityRenderDecorator { ...@@ -595,6 +595,17 @@ public class VelocityRenderDecorator {
return Formatter.formatLatexFormulas(htmlFragment); return Formatter.formatLatexFormulas(htmlFragment);
} }
/**
* Search in given text fragment for URL's and surround them with clickable
* HTML link objects.
*
* @param textFragment
* @return text with clickable links
*/
public static String formatURLsAsLinks(String textFragment) {
return Formatter.formatURLsAsLinks(textFragment);
}
/** /**
* Strips all HTML tags from the source string. * Strips all HTML tags from the source string.
* *
......
...@@ -35,9 +35,12 @@ import java.util.Date; ...@@ -35,9 +35,12 @@ import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.Locale; import java.util.Locale;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringEscapeUtils;
import org.olat.core.commons.chiefcontrollers.BaseChiefController; import org.olat.core.commons.chiefcontrollers.BaseChiefController;
import org.olat.core.helpers.Settings;
import org.olat.core.logging.LogDelegator; import org.olat.core.logging.LogDelegator;
/** /**
...@@ -515,6 +518,55 @@ public class Formatter extends LogDelegator { ...@@ -515,6 +518,55 @@ public class Formatter extends LogDelegator {
return htmlFragment; return htmlFragment;
} }
// Pattern to find URL's in text
private static final Pattern urlPattern = Pattern.compile("((http[s]*://|www\\.)[-A-Za-z0-9+&@#/%?=~_|!:,\\.;]+[-A-Za-z0-9+&@#/%=~_|]*)");
/**
* Search in given text fragment for URL's and surround them with clickable
* HTML link objects.
*
* @param textFragment
* @return text with clickable links
*/
public static String formatURLsAsLinks(String textFragment) {
Matcher matcher = urlPattern.matcher(textFragment);
StringBuffer sb = new StringBuffer();
int pos = 0;
while (matcher.find()) {
// Add text since last match and set end of current patch as new end
// of this match
sb.append(textFragment.substring(pos, matcher.start()));
pos = matcher.end();
// The URL is in group1, the other groups are ignored
String url = matcher.group(1);
// Fix URL's without protocol, assume http
if (!url.startsWith("http")) {
url = "http://" + url;
}
// Fix URL's at end of a sentence
if (url.endsWith(",") || url.endsWith(".")) {
url = url.substring(0, url.length()-1);
pos--;
}
sb.append("<a href=\"");
sb.append(url);
sb.append("\"");
// OpenOLAT URL's are opened in same window, all other URL's in separate window
if (!url.startsWith(Settings.getServerContextPathURI())) {
sb.append(" target=\"_blank\" class=\"b_link_extern\"");
}
sb.append(">");
sb.append(url);
sb.append("</a>");
}
// Add rest of text
sb.append(textFragment.substring(pos));
//
return sb.toString();
}
/** /**
* Round a double value to a double value with given number of * Round a double value to a double value with given number of
* figures after comma * figures after comma
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment