public static String trimLeadingAndTrailingSpaces(String markdown) { StringBuilder markdownSB = new StringBuilder(); StringBuilder currentLine = new StringBuilder(); String firstLineIndent = null; for (int i = 0; i < markdown.length(); i++) { char currentChar = markdown.charAt(i); if (currentChar == '\r' || currentChar == '\n') { String currentLineString = currentLine.toString(); if (firstLineIndent == null && !StringUtils.isEmpty(currentLineString)) { int firstNonWhitespaceIndex = StringUtils.indexOfAnyBut(currentLineString, " \t"); if (firstNonWhitespaceIndex != -1) firstLineIndent = currentLineString.substring(0, firstNonWhitespaceIndex); } if (firstLineIndent != null) currentLineString = StringUtils.removeStart(currentLineString, firstLineIndent); markdownSB.append(currentLineString).append(SystemUtils.LINE_SEPARATOR); currentLine.setLength(0); // skip the next line separator for \r\n cases if (currentChar == '\r' && markdown.length() > (i + 1) && markdown.charAt(i + 1) == '\n') { i++; } } else { currentLine.append(currentChar); } } markdownSB.append(currentLine); return markdownSB.toString(); }
/** * Take Wiki text of the form "|" or "| style='foo' |" and convert to and HTML <td> or <th> tag. * * @param text The text to be parsed. * @param tagType The HTML tag type, either "td" or "th". * @param markup The Wiki markup for the tag, either "|", "|+" or "!" */ protected void parseTableCell(String text, String tagType, String markup) throws ParserException { if (text == null) { throw new IllegalArgumentException("No text specified while parsing table cell"); } text = text.trim(); String openTagRaw = null; int pos = StringUtils.indexOfAnyBut(text, markup); if (pos != -1) { text = text.substring(pos); pos = text.indexOf('|'); if (pos != -1) { text = text.substring(0, pos); } openTagRaw = "<" + tagType + " " + text.trim() + ">"; } this.pushTag(tagType, openTagRaw); }