1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- package lib;
- import java.util.ArrayList;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class TextLayoutHelper {
- /**
- * Modifies the given string such that its lenght matches the given length.
- * If the string is too small, whitespace is appended on both sides equally.
- * Then, if the string is too large, it is cut off on the right.
- * @param s the string
- * @param lenght the target length
- * @return the modified string
- */
- public static String strToLen( String s, int lenght )
- {
- while( s.length() < lenght )
- {
- s = " " + s + " ";
- }
- if( s.length() > lenght )
- return s.substring( 0, lenght );
- return s;
- }
-
- private static String[] keywords = { "for", "do", "to", "then", "else", "if", "foreach", "while", "or", "and", "call", "function", "false", "true", "undefined", "with" };
- private static String[] delimiter = { "\\+", "\\-", "\\[", "\\]", "\\|", " ", "^", "$", "\\=", "\\,", "\\(", "\\;", "\\.", "\\)" };
-
- private static String getDelimiterRegex()
- {
- String reg = "(";
- for( String d : delimiter )
- {
- reg += d + "|";
- }
- reg = reg.substring( 0, reg.length() - 1 ) + ")";
- return reg;
- }
-
- public static String setupPseudoCodeComment( String s )
- {
- return "<html><font color=#FDD017>"+s+"</font></html>";
- }
-
- public static String setupPseudoCode( String s, String[] vars )
- {
- if( s.startsWith( "--" ) )
- return setupPseudoCodeComment( s );
- String delimiter = getDelimiterRegex();
- String ret = "<html>";
- String current = s.replaceAll( "&", "&" ).replaceAll( "<", " < ").replaceAll( ">", " > ");
- for( String k : keywords )
- {
- current = current.replaceAll( delimiter + "(" + k + ")" + delimiter, "$1<font color=orange>$2</font>$3" );
- }
- for( String v : vars )
- {
- current = current.replaceAll( delimiter + "(" + v + ")" + delimiter, "$1<font color=#3BB9FF>$2</font>$3" );
- }
- current = current.replaceAll( "\\'(.*?)\\'", "'<font color=#3cb371>$1</font>'" );
- ret += current + "</html>";
- return ret;
- }
-
- public static String[] getVariables( String s )
- {
- ArrayList<String> list = new ArrayList<>();
- Matcher m = Pattern.compile( "<font color=#3BB9FF>(.*?)</font>" ).matcher( s );
- while( m.find())
- {
- if( !list.contains( s.substring( m.start( 1 ), m.end( 1 ) ) ) )
- list.add( s.substring( m.start( 1 ), m.end( 1 ) ) );
- }
- return list.toArray( new String[ list.size() ] );
- }
- }
|