|
| 1 | +package moi; |
| 2 | + |
| 3 | +import org.apache.commons.text.StringEscapeUtils; |
| 4 | + |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.regex.Matcher; |
| 7 | +import java.util.regex.Pattern; |
| 8 | + |
| 9 | +/** |
| 10 | + * @author Andre Greiner-Petter |
| 11 | + */ |
| 12 | +public class CLIConverter { |
| 13 | + private CLIConverter(){} |
| 14 | + |
| 15 | + public static final char FUNCTION_APPLY = '\u2061'; |
| 16 | + public static final char INVISIBLE_TIMES = '\u2062'; |
| 17 | + |
| 18 | + private static final Pattern ANY_PATTERN = Pattern.compile("([a-z]+)([(:])"); |
| 19 | + |
| 20 | + /** |
| 21 | + * Converts the string formatted equation to MathML formatted equation. |
| 22 | + * @param str string formatted equation |
| 23 | + * @return MathML formatted equation |
| 24 | + */ |
| 25 | + public static String stringToMML(String str){ |
| 26 | + StringBuilder sb = new StringBuilder(); |
| 27 | + stringToMML(sb, str); |
| 28 | + return sb.toString(); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Converts the string math equation to MathML and fills the given StringBuilder |
| 33 | + * with the MathML format. {@link #stringToMML(String)} might be more convenient. |
| 34 | + * @param sb StringBuilder will be filled with MathML formatted {@param str} |
| 35 | + * @param str the math equation in string format that will be formatted to MathML |
| 36 | + */ |
| 37 | + public static void stringToMML(StringBuilder sb, String str) { |
| 38 | + LinkedList<String> stack = new LinkedList<>(); |
| 39 | + |
| 40 | + if ( str.isEmpty() ) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + Matcher any = ANY_PATTERN.matcher(str); |
| 45 | + while ( any.find() ) { |
| 46 | + StringBuffer restBuffer = new StringBuffer(); |
| 47 | + any.appendReplacement(restBuffer, ""); |
| 48 | + String prev = restBuffer.toString(); |
| 49 | + |
| 50 | + if ( !stack.isEmpty() && stack.getLast().equals(":mtext") ){ |
| 51 | + prev = prev.substring(0, Math.max(0,prev.length()-1)); |
| 52 | + |
| 53 | + if ( prev.contains("(") || prev.contains(")") ) { |
| 54 | + String tmp = ""; |
| 55 | + LinkedList<String> open = new LinkedList<>(); |
| 56 | + for ( int i = 0; i < prev.length(); i++) { |
| 57 | + if ( (prev.charAt(i)+"").equals("(") ) { |
| 58 | + open.addLast("("); |
| 59 | + } |
| 60 | + else if ( (prev.charAt(i)+"").equals(")") ) { |
| 61 | + if ( open.isEmpty() ) { |
| 62 | + break; |
| 63 | + } |
| 64 | + open.removeLast(); |
| 65 | + } |
| 66 | + tmp += prev.charAt(i); |
| 67 | + } |
| 68 | + |
| 69 | + prev = prev.substring(tmp.length()); |
| 70 | + sb.append(cleanContent(tmp)); |
| 71 | + closeTag(sb, "mtext"); |
| 72 | + stack.removeLast(); |
| 73 | + } else { |
| 74 | + sb.append(cleanContent(prev)); |
| 75 | + closeTag(sb, "mtext"); |
| 76 | + stack.removeLast(); |
| 77 | + prev = ""; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if ( prev.length()>0 ) { |
| 82 | + int startIndex = 0; |
| 83 | + String buffer = ""; |
| 84 | + |
| 85 | + if ( !stack.isEmpty() && stack.getLast().startsWith(":") ){ |
| 86 | + if ( stack.getLast().startsWith(":") && prev.matches("^,(?:[^,)].*|$)") ) { |
| 87 | + // empty mo... very special case |
| 88 | + String tag = stack.removeLast(); |
| 89 | + closeTag(sb, tag.substring(1)); |
| 90 | + prev = ""; |
| 91 | + } else { |
| 92 | + startIndex++; |
| 93 | + buffer += prev.charAt(0); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + for ( int i = startIndex; i < prev.length(); i++) { |
| 98 | + String e = ""+prev.charAt(i); |
| 99 | + if ( e.equals(")") ) { |
| 100 | + if ( !stack.isEmpty() ){ |
| 101 | + String lastTag = stack.removeLast(); |
| 102 | + if ( lastTag.startsWith(":") ) { |
| 103 | + sb.append(cleanContent(buffer)); |
| 104 | + closeTag(sb, lastTag.substring(1)); |
| 105 | + lastTag = stack.removeLast(); |
| 106 | + } |
| 107 | + closeTag(sb, lastTag); |
| 108 | + } |
| 109 | + } else if ( e.equals(",") ) { |
| 110 | + String lastTag = stack.removeLast(); |
| 111 | + if ( lastTag.startsWith(":") ) { |
| 112 | + sb.append(cleanContent(buffer)); |
| 113 | + closeTag(sb, lastTag.substring(1)); |
| 114 | + } else { |
| 115 | + stack.addLast(lastTag); |
| 116 | + } |
| 117 | + } else { |
| 118 | + buffer += e; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + String tmp = any.group(2); |
| 124 | + String tag = any.group(1); |
| 125 | + |
| 126 | + if ( tmp.equals(":") ) { |
| 127 | + // is an element |
| 128 | + stack.addLast(":"+tag); |
| 129 | + openTag(sb, tag); |
| 130 | + } else { |
| 131 | + // is a parent node |
| 132 | + stack.addLast(tag); |
| 133 | + openTag(sb, tag); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + StringBuffer restBuffer = new StringBuffer(); |
| 138 | + any.appendTail(restBuffer); |
| 139 | + String rest = restBuffer.toString(); |
| 140 | + |
| 141 | + String lastElement = stack.removeLast(); |
| 142 | + if ( lastElement.startsWith(":") ) { |
| 143 | + rest = rest.substring(0, Math.max(0,rest.length()-stack.size())); |
| 144 | + sb.append(cleanContent(rest)); |
| 145 | + closeTag(sb, lastElement.substring(1)); |
| 146 | + } else { |
| 147 | + rest = rest.substring(0, Math.max(0,rest.length()-(stack.size()+1))); |
| 148 | + sb.append(cleanContent(rest)); |
| 149 | + closeTag(sb, lastElement); |
| 150 | + } |
| 151 | + |
| 152 | + while ( !stack.isEmpty() ) { |
| 153 | + String tag = stack.removeLast(); |
| 154 | + if ( tag.startsWith(":") ) tag = tag.substring(1); |
| 155 | + closeTag(sb, tag); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static String cleanContent( String content ) { |
| 160 | + content = content.matches("ivt") ? "" + INVISIBLE_TIMES : content; |
| 161 | + content = content.matches("fap") ? "" + FUNCTION_APPLY : content; |
| 162 | + content = StringEscapeUtils.escapeXml11(content); |
| 163 | + return content; |
| 164 | + } |
| 165 | + |
| 166 | + private static void openTag(StringBuilder sb, String tag){ |
| 167 | + sb.append("<"); |
| 168 | + sb.append(tag); |
| 169 | + sb.append(">"); |
| 170 | + } |
| 171 | + |
| 172 | + private static void closeTag(StringBuilder sb, String tag){ |
| 173 | + sb.append("</"); |
| 174 | + sb.append(tag); |
| 175 | + sb.append(">"); |
| 176 | + } |
| 177 | + |
| 178 | + public static void main(String[] args) { |
| 179 | + if ( args == null || args.length == 0 ) { |
| 180 | + System.out.println("You have to provide a string that should be translated."); |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + String in = args[0]; |
| 185 | + String out = stringToMML(in); |
| 186 | + System.out.println(out); |
| 187 | + } |
| 188 | +} |
0 commit comments