Page 1 of 1

Скрипт раскраски diff

PostPosted: Tue Dec 01, 2009 8:41 am
by artzub
Привет!

Написал скрипт для раскраски diff патчей:

Code: Select all
import com.trackstudio.exception.GranException;
import java.util.*;
try
{
   boolean git = false;
   String[] testArr;
   String testRes = "";
   String testStr = message.description;
      
   Scanner sc = new Scanner(testStr).useDelimiter("=(C|c)(O|o)(D|d)(E|e)(=| )");
   if(sc.hasNext())
       testRes = sc.next();
   else
       testRes = testStr;
   
   while (sc.hasNext())
   {
       String string = sc.next();
      
       git = (string.startsWith("diff=") || string.startsWith("DIFF="));
       string = string.replaceFirst("(D|d)(I|i)(F|f)(F|f)=", "");
   
       testArr = string.split("=\\/(C|c)(O|o)(D|d)(E|e)=");
       string = "";
       for (int i = 0; i < (testArr.length > 2 ? testArr.length - 1 : testArr.length); i++)
           string += testArr[i];
      
       testRes += "<div style=\"background-color: #ffffff;\"><span style=\"font-family: 'courier new', courier;\">";

        string = string.replaceAll("</.*?>", "\n");
        string = string.replaceAll("<[a-zA-Z]+.?/>", "\n");
        string = string.replaceAll("<.*?>", "");
        string = string.replaceAll("&nbsp;", "");

        for (StringTokenizer stringTokenizer = new StringTokenizer(string, "\n"); stringTokenizer.hasMoreTokens();)
        {
            String token = stringTokenizer.nextToken();           
            if(token.matches("(.*)diff --git(.*)"))
            {
                testRes += token.replaceAll("(.*?)diff --git(.*?)", "<span style=\"color: #000080;\">diff --git") + "</span><br />";
                continue;
            }
            if(token.matches("(.*)index(.*)"))
            {
                testRes += token.replaceAll("(.*?)index", "<span style=\"color: #000080;\">index") + "</span><br />";
                continue;
            }
            if(token.matches("(.*)(\\-{3})\\s(.*)"))
            {
                testRes += token.replaceAll("(.*)(\\-{3})\\s(.*?)", "<span style=\"color: #000080;\">--- ") + "</span><br />";
                continue;
            }
            if(token.matches("(.*)(\\+{3})\\s(.*)"))
            {
                testRes += token.replaceAll("(.*)(\\+{3})\\s(.*?)", "<span style=\"color: #000080;\">+++ ") + "</span><br />";
                continue;
            }
            if(token.matches("(.*)@@(.*)@@(.*)"))
            {
                testRes += token.replaceAll("(.*)@@(.*)@@(.*)", "<span style=\"color: #0000ff;\"><strong>@@$2@@$3</strong></span><br />");
                continue;
            }
            if(token.matches("^\\+(.*)"))
            {
                testRes += token.replaceAll("^\\+(.*)", "<span style=\"color: #00ff00;\">+$1</span><br />");
                continue;
            }
            if(token.matches("^\\-(.*)"))
            {
                testRes += token.replaceAll("^\\-(.*)", "<span style=\"color: #ff0000;\">-$1</span><br />");
                continue;
            }
            testRes += token + (token.matches("(.*)[a-zA-ZР°-СЏРђ-РЇ](.*)") ? "<br />" : "");
        }
        testRes += "</span></span></div><br />" + (testArr.length > 2 ? testArr[testArr.length - 1] : "");
   }
   sc.close();
   message.description = testRes;
}
catch(GranException e)
{
   throw new com.trackstudio.exception.UserMessageException(e.getMessage());
}
return message;


Если есть замечания или предложения об оптимизации пишите буду благодарен!