001package org.conqat.lib.commons.markup;
002
003/**
004 * Util class for handling markup (i.e., HTML tags or Markdown relevant symbols
005 * in String literals
006 */
007public class MarkupUtils {
008        /**
009         * Replaces HTML tags with corresponding entity names.
010         */
011        public static String escapeHtmlTags(String s) {
012                String formatted = s.replaceAll("</", "&lt;&frasl;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
013                return formatted;
014        }
015
016        /**
017         * Replaces characters that are meaningful in Markdown markup language with
018         * their corresponding entity numbers.
019         */
020        public static String escapeMarkdownRelevantSymbols(String s) {
021                String formatted = s.replaceAll("\\*", "&#42;").replaceAll("_", "&#95;").replaceAll("~", "&#126;")
022                                .replaceAll("\\[", "&#91;").replaceAll("]", "&#93;").replaceAll("!", "&#33;").replaceAll("`", "&#96;")
023                                .replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll("\\n", "&nbsp;");
024
025                // trick to prevent automatic linking of URLs in string literals
026                formatted = formatted.replaceAll("//", "//<span></span>");
027                formatted = formatted.replaceAll("www", "www<span></span>");
028                return formatted;
029        }
030}