You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.1 KiB

package de.tvo.tools;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Schneidele
* @version 1.0
*/
public class HtmlUtil {
public HtmlUtil() {
}
public static void main(String[] args) {
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Liste zurück.
* @see #createListWithBold(String) Liste mit Fettschrift
*/
public static String createList(String source) {
return source.replaceFirst("-", "<li>") + "</li>";
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Liste und Fettschrift zurück.
* @see #createList(String) Liste ohne Fettschrift
* @see #createBoldText(String) Paragraph mit Fettschrift
*/
public static String createListWithBold(String source) {
if (source.contains(" **")) {
return source.replaceFirst("-", "<li>").replace(" **", " <b>").replace("** ", "</b> ") + "</li>";
} else {
final String regex = "\\*\\*(.*)\\*\\*";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(source);
if (matcher.find()) {
return source.replaceFirst("-", "<li>").replace(matcher.group(0), "<b>" + matcher.group(1) + "</b>") + "</li>";
} else {
return source.replaceFirst("-", "<li>") + "</li>";
}
}
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Paragraph zurück.
*/
public static String createParagraph(String source) {
return "<p>" + source + "</p>";
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Header zurück.<br>
* "#" --> "h1"<br>
* "##" --> "h2"<br>
* "###" --> "h3"<br>
* "####" --> "h4"<br>
* "#####" --> "h5"<br>
* "######" --> "h6"
*/
public static String createHeader(String source) {
int hashCounter = 0;
for (int i = 0; i < source.length(); i++) {
if (source.substring(i, i+1).equals("#")) {
hashCounter++;
}
}
if (hashCounter == 1) {
return source.replaceFirst("#", "<h1>") + "</h1><hr>";
} else if (hashCounter == 2) {
return source.replaceFirst("##", "<h2>") + "</h2><hr>";
} else if (hashCounter == 3) {
return source.replaceFirst("###", "<h3>") + "</h3>";
} else if (hashCounter == 4) {
return source.replaceFirst("####", "<h4>") + "</h4>";
} else if (hashCounter == 5) {
return source.replaceFirst("#####", "<h5>") + "</h5>";
} else {
return source.replaceFirst("######", "<h6>") + "</h6>";
}
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt eine "Breakline" "_______" über die gesamte Seite zurück.
*/
public static String createBreakLine(String source) {
return source.replace("___", "<hr>");
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Fettschrift zurück.
*/
public static String createBoldText(String source) {
if (source.contains(" **")) {
return source.replace(" **", " <b>").replace("** ", "</b> ");
} else {
final String regex = "\\*\\*(.*)\\*\\*";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(source);
if (matcher.find()) {
return source.replace(matcher.group(0), "<b>" + matcher.group(1) + "</b>");
} else {
return source;
}
}
}
/**
* @author Schneidele
* @param source String zum umwandeln
* @return Gibt umgewandelten String mit HTML Tags als Bild zurück.
*/
public static String createImage(String source) {
final String regex = ".*\\((.*)\\).*";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(source);
if (matcher.find()) {
return "<img src=\"https://git.de.geis-group.net/risc/wiki_glog_gtms/-/wikis/" + matcher.group(1) + "\" alt=\"Bild\">";
} else {
return "<p style=\"color:red\"> <b>[!ERROR!]</b> Bild konnte nicht geladen werden </p>";
}
}
}