1. #1
    Avatar von uncopyable
    Registriert seit
    19.11.2011
    Beiträge
    282
    Thanked 235 Times in 120 Posts

    Standard KCodeParser (KCode to HTML)

    Hallo, hier mein KCodeParser.

    Funktionen:
    Übersetzt Knuddels Codes in HTML, denn kann man dann z.B in ein JTextPane anzeigen lassen.
    Beispiel: _Fett_ wird zu <b>Fett</b>

    Folgende Knuddels Code werden unterstützt:
    _Fett_, "Kursiv" °Farbe°, °>link<°, °>Text|link<° # Zeilenumbruch

    Bugs
    °R>Text|link<° Hier wird die Farbe nicht berücksichtigt °R°°>Text|link<° funktioniert.


    Das Snippet ist noch nicht perfekt und man kann es bestimmt noch besser/optimieren Verbesserungsvorschlag sind erwünscht!

    PHP-Code:

    package jchat
    .tools;


    import java.awt.Color;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;


    /**
     *
     * @author uncopyable
     */
    public class KCodeParser {
        private static 
    String fontColor ="BLACK";
        private static 
    int fontSize = 12;
        private static 
    StringBuilder code = new StringBuilder();
        private static 
    String style;
        public 
    KCodeParser(String fontColor,int fontSize){
            
    this.fontColor = fontColor;
            
    this.fontSize = fontSize;
            
    style = "<style type=\"text/css\">"
                    
    + "body {"
                    
    + "font-family:Arial;"
                    
    + "font-size:"
                    
    + fontSize
                    
    + "px;"
                    
    + "color:"
                    
    + fontColor
                    
    + ";";
        }
        public 
    KCodeParser(){
           
    style = null;
           
    fontColor ="BLACK";
           
    fontSize = 12;
        }
       
        public static 
    String parse(String text) {
            
    code = new StringBuilder(style);
            
    boolean escape = false;
            
    boolean bold = false;
            
    boolean italic = false;
            
    int b = 0;
            
            for (
    int i = 0; i < text.length(); i++) {   
                
    char ch = text.charAt(i);           
                switch (
    ch) {         
                    case 
    '<':                       
                        
    code.append("&lt;");        
                        break;                      
                    case 
    '>':                       
                        
    code.append("&gt;");        
                        break;                      
                    case 
    '&':                       
                        
    code.append("&amp;");      
                        break;                      
                        
                    case 
    '\\':                       
                         if (
    escape){                
                            
    code.append("\\");   
                            break;                   
                         }                           
                        break;                       
                        
                    case 
    ':':  // Smileys 
                         
    if (text.startsWith(":)", i) && (!escape)) { 
                              
    code.append("<img src=\"http://chat.knuddels.de/pics/sm_classic_00.gif\" alt=\":)\"/>");
                            
    i += 1;  
                         }
                         break;
                    case 
    '°':
                        try {
                            if (
    escape){
                                
    code.append("°");
                                break;
                            }


                             if (
    text.charAt(i + 1) == '°' && (!escape)) {
                                 
    String kcode = text.substring(i + 2);
                                 
    kcode = kcode.substring(0, kcode.indexOf('<'));
                                 if (
    kcode.split("\\|").length > 1){
                                     
    String href = kcode.split("\\|")[0];
                                     
    String a = kcode.split("\\|")[1]; 
                                     
    code.append("<a href=\"").append(a).append("\">").append(href).append("</a>");   
                                 if (
    kcode.split("\\|").length > 2){
                                     
    // 
                                 
    }
                                 }else {
                                     
    code.append("<a href=\"").append(kcode).append("\">").append(kcode).append("</a>"); 
                                     
    i += kcode.length() + 2;
                                     break;  
                                 }                             
                                 
    i += kcode.length() + 2;


                                 break;
                            }else if (
    isNumber(text.charAt(i + 1))) { // Schriftgröße
                                 
    String kcode = text.substring(i + 1);
                                 
    kcode = kcode.substring(0, kcode.indexOf('°'));
                                 if (
    kcode.isEmpty()){
                                     break;
                                 }
                                 
    code.append("<span style=\"font-size:").append(kcode).append("px\">");
                                 
    i += kcode.length() + 1;
                                 break;
                            }else if (
    text.charAt(i + 2) == '°') { // Farbe
                                 
    String kcode = text.substring(i + 1);
                                 
    kcode = kcode.substring(0, kcode.indexOf('°'));
                                 if (
    kcode.isEmpty()){
                                     break;
                                 }
                                 
    code.append("<font color=\"").append(GetHtmlColor(kcode.charAt(0))).append("\">");
                                 
                                 
    i += kcode.length() + 1;
                            }else if (
    text.charAt(i + 1) == '[' && (!escape)) { 
                                    
    String kcode = text.substring(i + 1);
                                    
    kcode = kcode.substring(0, kcode.indexOf('°'));                               
                                    
    code.append("<font color=\"").append(parseColor(kcode)).append("\">");
                                    
    i += kcode.length() + 2;
                                    break;
                            }else {
                                    
    String kcode = text.substring(i + 2);
                                 
    kcode = kcode.substring(0, kcode.indexOf('<'));
                                 if (
    kcode.split("\\|").length > 1){
                                     
    String href = kcode.split("\\|")[0];
                                     
    String a = kcode.split("\\|")[1]; 
                                     
    code.append("<a href=\"").append(a).append("\">").append(href).append("</a>");   
                                 if (
    kcode.split("\\|").length > 2){
                                     
    // 
                                 
    }
                                 }else {
                                     
    code.append("<a href=\"").append(kcode).append("\">").append(kcode).append("</a>"); 
                                     
    i += kcode.length() + 2;
                                     break;  
                                 }                             
                                 
    i += kcode.length() + 2;


                                 break;
                            }
                                       
                        } catch (
    Exception ex) {
                            
                        }
                        
                        break;
                        
                    case 
    '§':
                        
    escape = false;
                        
    bold = false;
                        
    italic = false;
                        
    code.append("<span style=\"font-size:").append(fontSize).append("px\">");
                        break;
                    case 
    '_':
                        if (
    escape){
                            
    code.append("_");
                            break;
                        }
                        if (
    bold){
                            
    code.append("</b>");
                            
    bold = false;
                            break;
                        }
                        
    code.append("<b>");
                        
    bold = !bold;
                        break;
                        
                        
                    case 
    '"':
                        if (
    escape){
                            
    code.append("\"");
                            break;
                        }
                        if (
    italic){
                            
    code.append("</i>");
                            break;
                        }
                        
    code.append("<i>");
                        
    italic = !italic;
                        break;
                    case 
    '#':
                         if (
    escape){
                            
    code.append("#");
                            break;
                        }
                        
    code.append("<br>");
                         break;
                    default:
                        
    code.append(ch);    
                }
               if (
    ch == '\\') {
                    
    code.append(""); 
                    
    escape = true;
                }else {
                    
    escape = false;
                }
            }
            return 
    code.toString();
        }
     
        private static 
    String GetHtmlColor(char s) {
            if (
    s == 'A') {
                return 
    "GRAY";
            } else if (
    s == 'B') {
                return 
    "BLUE";
            } else if (
    s == 'C') {
                return 
    "CYAN";
            } else if (
    s == 'D') {
                return 
    "DARKGRAY";
            } else if (
    s == 'E') {
                return 
    "DARKGREEN";
            } else if (
    s == 'G') {
                return 
    "GREEN";
            } else if (
    s == 'K') {
                return 
    "BLACK";
            } else if (
    s == 'Y') {
                return 
    "YELLOW";
            } else if (
    s == 'L') {
                return 
    "LIGHTBLUE";
            } else if (
    s == 'M') {
                return 
    "VIOLET";
            } else if (
    s == 'N') {
                return 
    "BROWN";
            } else if (
    s == 'O') {
                return 
    "ORANGE";
            } else if (
    s == 'P') {
                return 
    "PINK";
            } else if (
    s == 'R') {
                return 
    "RED"; 
            } else if (
    s == 'W') {
                return 
    "WHITE";
            } else if (
    s == 'Y') {
                return 
    "YELLOW";
            } else if (
    s == 'r') {
                return 
    fontColor;
            } else {
                return 
    "BLACK";
            }
        }
        public static 
    String parseColor(String input) {
            
    Pattern p = Pattern.compile("(\\d{1,3}),(\\d{1,3}),(\\d{1,3})");
            
    Matcher matcher = p.matcher(input);
            if(
    matcher.find()) {
               
    String colors = matcher.group();
               
    int c1 = Integer.parseInt(colors.split(",")[0]);
               
    int c2 = Integer.parseInt(colors.split(",")[1]);
               
    int c3 = Integer.parseInt(colors.split(",")[2]);
               return 
    Integer.toString((new Color(c1,c2,c3)).getRGB() & 0xffffff, 16);
            }
            return 
    null;  
        }
        private static 
    boolean isNumber(int character) {
            return 
    character >= '0' && character <= '9';
        }


    } 


    while( behappy() ) { code(); }

  2. The Following 2 Users Say Thank You to uncopyable For This Useful Post:

    Comu (08.05.2012), Mentos (12.05.2012)

  3. #2
    Avatar von uncopyable
    Registriert seit
    19.11.2011
    Beiträge
    282
    Thanked 235 Times in 120 Posts

    Standard KCodeParser Version 1.2

    Guten Morgen

    hab mal am Parser weiter gearbeitet und die Bugs gefixt.
    Wie oben erwähnt, ist das Snippet noch nicht perfekt. Verbesserungsvorschläge sind auch hier erwünscht.

    • °R>Text|link<°
    • °12>Text|link<°
    • °>sm_01.gif<°
    • °>_hJames|/serverpp \"<°
    kann man nun auch verwenden.

    Letze Änderung: Mittwoch, 30. Mai 2012 - kleine Schönheitskorrekturen

    PHP-Code:
    package jchat.tools;

    import java.awt.Color;
    import java.awt.Font;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    /**
     *
     * @author uncopyable
     *
     * Version 1.2 Dienstag, 29. Mai 2012 
     *
     * Last Modified: Mittwoch, 30. Mai 2012
     *  - Schönheitskorrekturen
     *
     */

    public class KCodeParser {
        private static 
    Color fontColor;
        private static 
    int fontSize;
        private static 
    StringBuilder code;
        private static 
    Font font;
        
        public 
    KCodeParser(Color fontColor, Font font) {
            
    this.code = new StringBuilder();
            
    this.font = font;
            
    this.fontColor = fontColor;
            
    this.fontSize = font.getSize();
        }

        public 
    KCodeParser() {
            
    this.code = new StringBuilder();
            
    this.font = new Font("Arial", Font.PLAIN, 12);
            
    this.fontColor = new Color(0, 0, 0);
            
    this.fontSize = 12;
        }

        public static 
    String parse(String text) {
            
    code = new StringBuilder();
            
    boolean escape = false;
            
    boolean bold = false;
            
    boolean italic = false;
            
            
    // Style
            
    code.append("<span style=\"font-size:").append(fontSize).append("px\">");
            
    code.append("<font color=\"").append(Integer.toString(fontColor.getRGB() & 0x00ffffff, 16)).append("\">");

            for (
    int i = 0; i < text.length(); i++) {
                
    char ch = text.charAt(i);
                switch (
    ch) {
                    case 
    '<':
                        
    code.append("&lt;");
                        break;
                    case 
    '>':
                        
    code.append("&gt;");
                        break;
                    case 
    '&':
                        
    code.append("&amp;");
                        break;
                    case 
    '\\':
                        
    i++;
                        break;
                        
                    
    // Smileys 
                    
    case ':':
                        if (
    text.startsWith(":)", i) && (!escape)) {
                            
    code.append("<img src=\"http://chat.knuddels.de/pics/sm_classic_00.gif\" alt=\":)\"/>");
                            
    i++;
                        } else {
                            
    code.append(":");
                        }
                        break;
                        
                    
    // Reset Style 
                    
    case '§':
                        
    escape = false;
                        
    bold = false;
                        
    italic = false;
                        
    code.append("<span style=\"font-size:").append(fontSize).append("px\">");
                        
    code.append("<font color=\"").append(Integer.toString(fontColor.getRGB() & 0x00ffffff, 16)).append("\">");
                        break;
                        
                    
    // Bold
                    
    case '_':
                        if (
    escape) {
                            
    code.append("_");
                            break;
                        }
                        if (
    bold) {
                            
    code.append("</b>");
                            
    bold = false;
                            break;
                        }
                        
    code.append("<b>");
                        
    bold = !bold;
                        break;

                    
    // Italic   
                    
    case '"':
                        if (
    escape) {
                            
    code.append("\"");
                            break;
                        }
                        if (
    italic) {
                            
    code.append("</i>");
                            break;
                        }
                        
    code.append("<i>");
                        
    italic = !italic;
                        break;

                    
    // Line break
                    
    case '#':
                        if (
    escape) {
                            
    code.append("#");
                            break;
                        }
                        
    code.append("<br>");
                        break;

                    
    // KCode
                    
    case '°':
                    
                        try {
                        
                            if (
    escape) {
                                
    code.append("°");
                                break;
                            }

                            if (
    text.charAt(i + 1) == '°' && (!escape)) {
                                break;
                                
                            } else if (
    isNumber(text.charAt(i + 1))) { // Schriftgröße

                                
    if (text.charAt(i + 3) == '>') { // Link
                                    
    int length = 0;
                                    
    StringBuilder size = new StringBuilder();
                                    
    size.append(text.charAt(i + 1));
                                    
    size.append(text.charAt(i + 2));
                                    
    code.append("<span style=\"font-size:").append(size.toString()).append("px\">");
                                    
    length += size.toString().length();
                                    
                                    
    String link = text.substring(i + 4);
                                    
    link = link.substring(0, link.indexOf('<'));
                                    if (
    link.split("\\|").length > 1) {
                                        
    String href = link.split("\\|")[0];
                                        
    String a = link.split("\\|")[1];
                                        
    code.append("<a href=\"").append(href).append("\">").append(a).append("</a>");
                                    } else {
                                        
    code.append("<a href=\"").append(link).append("\">").append(link).append("</a>");
                                    }
                                    
    length += link.length() + 2;
                                    
    i += length;
                                    break;
                                    
                                } else if ((
    isNumber(text.charAt(i + 2)))) {
                                    
    StringBuilder size = new StringBuilder();
                                    
    size.append(text.charAt(i + 1));
                                    
    size.append(text.charAt(i + 2));
                                    
    code.append("<span style=\"font-size:").append(size.toString()).append("px\">");
                                    
    i += size.toString().length();
                                    break;
                                } else if (
    text.charAt(i + 2) == '°') {
                                    
    String size = text.substring(i + 1);
                                    
    size = size.substring(0, size.indexOf('°'));
                                    
    code.append("<span style=\"font-size:").append(size).append("px\">");
                                    
    i += size.length() + 1;
                                    break;
                                } else if (
    text.charAt(i + 2) == '>') { // Link
                                    
    int length = 0;
                                    
    StringBuilder size = new StringBuilder();
                                    
    size.append(text.charAt(i + 1));
                                    
    code.append("<span style=\"font-size:").append(size.toString()).append("px\">");

                                    
    length += size.toString().length();

                                    
    String link = text.substring(i + 3);
                                    
    link = link.substring(0, link.indexOf('<'));
                                    if (
    link.split("\\|").length > 1) {
                                        
    String href = link.split("\\|")[0];
                                        
    String a = link.split("\\|")[1];
                                        
    code.append("<a href=\"").append(href).append("\">").append(a).append("</a>");
                                    } else {
                                        
    code.append("<a href=\"").append(link).append("\">").append(link).append("</a>");
                                    }

                                    
    length += link.length() + 2;
                                    
    i += length;
                                    break;
                                } else {
                                    break;
                                }

                            } else if ((
    text.charAt(i + 1) == '>')) { // Link
                                
    String kcode = text.substring(i + 2);
                                
    kcode = kcode.substring(0, kcode.indexOf('<'));

                                if ((
    text.charAt(i + 2) == '_') && (text.charAt(i + 3) == 'h')) {

                                    if (
    kcode.split("\\|").length > 1) {
                                        
    String href = kcode.replace("_h", "").split("\\|")[0];
                                        
    String a = kcode.split("\\|")[1];
                                        
    a = a.replace("\"", href);
                                        
    code.append("<a href=\"").append(a).append("\">").append(href).append("</a>");
                                    }
                                    
    i += kcode.length() + 2;
                                    break;
                                }
                                if (
    kcode.split("\\|").length > 1) {
                                    
    String href = kcode.split("\\|")[0];
                                    
    String a = kcode.split("\\|")[1];
                                    
    code.append("<a href=\"").append(href).append("\">").append(a).append("</a>");
                                    
    i += kcode.length() + 2;
                                    break;
                                }
                                
                                if (
    isImage(kcode)) {
                                    
    code.append("<img src=\"http://chat.knuddels.de/pics/");
                                    
    code.append(kcode);
                                    
    code.append("\"/>");
                                    
    i += kcode.length() + 2;
                                    break;
                                }
                                
    code.append("<a href=\"").append(kcode).append("\">").append(kcode).append("</a>");
                                
    i += kcode.length() + 2;
                                break;

                            } else if (
    isColor(text.charAt(i + 1)) && (text.charAt(i + 2) == '>')) { // Schriftgröße
                                
    int length = 0;
                                
    String color = String.valueOf(text.charAt(i + 1));
                                
    code.append("<font color=\"").append(GetHtmlColor(color.charAt(0))).append("\">");

                                
    length += color.length() + 1;

                                if (
    text.charAt(i + 2) == '>') { // Link
                                    
    String link = text.substring(i + 3);
                                    
    link = link.substring(0, link.indexOf('<'));
                                    if (
    link.split("\\|").length > 1) {
                                        
    String href = link.split("\\|")[0];
                                        
    String a = link.split("\\|")[1];
                                        
    code.append("<a href=\"").append(href).append("\">").append(a).append("</a>");
                                    } else {
                                        
    code.append("<a href=\"").append(link).append("\">").append(link).append("</a>");
                                    }

                                    
    length += link.length() + 2;
                                }
                                
    i += length;
                                break;
                            } else if (
    text.charAt(i + 2) == '°') { // Farbe
                                
    String color = text.substring(i + 1);
                                
    color = color.substring(0, color.indexOf('°'));
                                
    code.append("<font color=\"").append(GetHtmlColor(color.charAt(0))).append("\">");
                                
    i += color.length() + 1;
                            } else if (
    text.charAt(i + 1) == '[') {  // rgb 
                                
    String rgbColor = text.substring(i + 1);
                                
    rgbColor = rgbColor.substring(0, rgbColor.indexOf('°'));
                                
    rgbColor = parseColor(rgbColor);
                                
    code.append("<font color=\"").append(rgbColor).append("\">");
                                
    i += rgbColor.length() + 3;
                                break;
                            } else {                        
                            }                        
                        } catch (
    Exception ex) {
                        }
                        break;
                    default:
                        
    code.append(ch);
                }
            }
            return 
    code.toString();
        }

        private static 
    boolean isColor(int character) {
            switch (
    character) {
                case 
    'A':
                case 
    'B':
                case 
    'C':
                case 
    'D':
                case 
    'E':
                case 
    'G':
                case 
    'K':
                case 
    'L':
                case 
    'M':
                case 
    'N':
                case 
    'O':
                case 
    'P':
                case 
    'R':
                case 
    'r':
                case 
    'W':
                case 
    'Y':
                    return 
    true;
                default:
                    return 
    false;
            }
        }

        private static 
    String GetHtmlColor(char s) {
            if (
    s == 'A') {
                return 
    "GRAY";
            } else if (
    s == 'B') {
                return 
    "BLUE";
            } else if (
    s == 'C') {
                return 
    "CYAN";
            } else if (
    s == 'D') {
                return 
    "DARKGRAY";
            } else if (
    s == 'E') {
                return 
    "DARKGREEN";
            } else if (
    s == 'G') {
                return 
    "GREEN";
            } else if (
    s == 'K') {
                return 
    "BLACK";
            } else if (
    s == 'Y') {
                return 
    "YELLOW";
            } else if (
    s == 'L') {
                return 
    "LIGHTBLUE";
            } else if (
    s == 'M') {
                return 
    "VIOLET";
            } else if (
    s == 'N') {
                return 
    "BROWN";
            } else if (
    s == 'O') {
                return 
    "ORANGE";
            } else if (
    s == 'P') {
                return 
    "PINK";
            } else if (
    s == 'R') {
                return 
    "RED";
            } else if (
    s == 'W') {
                return 
    "WHITE";
            } else if (
    s == 'Y') {
                return 
    "YELLOW";
            } else if (
    s == 'r') {
                return 
    Integer.toString(fontColor.getRGB() & 0x00ffffff, 16);
            } else {
                return 
    "BLACK";
            }
        }

        private static 
    boolean isNumber(int character) {
            return 
    character >= '0' && character <= '9';
        }

        private static 
    boolean isImage(String s) {
            return 
    s.endsWith(".gif") | s.endsWith(".png") | s.endsWith(".jpg");
        }

        public static 
    String parseColor(String input) {
            
    Pattern p = Pattern.compile("(\\d{1,3}),(\\d{1,3}),(\\d{1,3})");
            
    Matcher matcher = p.matcher(input);
            if (
    matcher.find()) {
                
    String colors = matcher.group();
                
    int c1 = Integer.parseInt(colors.split(",")[0]);
                
    int c2 = Integer.parseInt(colors.split(",")[1]);
                
    int c3 = Integer.parseInt(colors.split(",")[2]);
                return 
    Integer.toString((new Color(c1, c2, c3)).getRGB() & 0xffffff, 16);
            }
            return 
    null;
        }
    } 
    Geändert von uncopyable (30.05.2012 um 18:26 Uhr) Grund: Schönheitskorrekturen


    while( behappy() ) { code(); }

Ähnliche Themen

  1. HTML Editor
    Von Shane im Forum HTML
    Antworten: 5
    Letzter Beitrag: 21.10.2014, 21:04
  2. Antworten: 0
    Letzter Beitrag: 14.10.2012, 20:07
  3. KCode Painting (?)
    Von Badboy12 im Forum Knuddels Programmierung
    Antworten: 2
    Letzter Beitrag: 28.06.2012, 19:34
  4. HTML Templates?
    Von x BoooM x im Forum HTML
    Antworten: 1
    Letzter Beitrag: 28.01.2012, 17:11
  5. HTML-Einführung
    Von Dost im Forum Skriptsprachen
    Antworten: 16
    Letzter Beitrag: 19.10.2011, 21:23
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.