1. #1
    U-Hacks Routinier
    Avatar von uncopyable
    Registriert seit
    19.11.2011
    Beiträge
    282
    Thanked 234 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 0text.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=\":)\"/>");
                            
    += 1;  
                         }
                         break;
                    case 
    '°':
                        try {
                            if (
    escape){
                                
    code.append("°");
                                break;
                            }


                             if (
    text.charAt(1) == '°' && (!escape)) {
                                 
    String kcode text.substring(2);
                                 
    kcode kcode.substring(0kcode.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>"); 
                                     
    += kcode.length() + 2;
                                     break;  
                                 }                             
                                 
    += kcode.length() + 2;


                                 break;
                            }else if (
    isNumber(text.charAt(1))) { // Schriftgröße
                                 
    String kcode text.substring(1);
                                 
    kcode kcode.substring(0kcode.indexOf('°'));
                                 if (
    kcode.isEmpty()){
                                     break;
                                 }
                                 
    code.append("<span style=\"font-size:").append(kcode).append("px\">");
                                 
    += kcode.length() + 1;
                                 break;
                            }else if (
    text.charAt(2) == '°') { // Farbe
                                 
    String kcode text.substring(1);
                                 
    kcode kcode.substring(0kcode.indexOf('°'));
                                 if (
    kcode.isEmpty()){
                                     break;
                                 }
                                 
    code.append("<font color=\"").append(GetHtmlColor(kcode.charAt(0))).append("\">");
                                 
                                 
    += kcode.length() + 1;
                            }else if (
    text.charAt(1) == '[' && (!escape)) { 
                                    
    String kcode text.substring(1);
                                    
    kcode kcode.substring(0kcode.indexOf('°'));                               
                                    
    code.append("<font color=\"").append(parseColor(kcode)).append("\">");
                                    
    += kcode.length() + 2;
                                    break;
                            }else {
                                    
    String kcode text.substring(2);
                                 
    kcode kcode.substring(0kcode.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>"); 
                                     
    += kcode.length() + 2;
                                     break;  
                                 }                             
                                 
    += 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 (
    == 'A') {
                return 
    "GRAY";
            } else if (
    == 'B') {
                return 
    "BLUE";
            } else if (
    == 'C') {
                return 
    "CYAN";
            } else if (
    == 'D') {
                return 
    "DARKGRAY";
            } else if (
    == 'E') {
                return 
    "DARKGREEN";
            } else if (
    == 'G') {
                return 
    "GREEN";
            } else if (
    == 'K') {
                return 
    "BLACK";
            } else if (
    == 'Y') {
                return 
    "YELLOW";
            } else if (
    == 'L') {
                return 
    "LIGHTBLUE";
            } else if (
    == 'M') {
                return 
    "VIOLET";
            } else if (
    == 'N') {
                return 
    "BROWN";
            } else if (
    == 'O') {
                return 
    "ORANGE";
            } else if (
    == 'P') {
                return 
    "PINK";
            } else if (
    == 'R') {
                return 
    "RED"
            } else if (
    == 'W') {
                return 
    "WHITE";
            } else if (
    == 'Y') {
                return 
    "YELLOW";
            } else if (
    == '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() & 0xffffff16);
            }
            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
    U-Hacks Routinier
    Avatar von uncopyable
    Registriert seit
    19.11.2011
    Beiträge
    282
    Thanked 234 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 fontColorFont 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.PLAIN12);
            
    this.fontColor = new Color(000);
            
    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() & 0x00ffffff16)).append("\">");

            for (
    int i 0text.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() & 0x00ffffff16)).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(1) == '°' && (!escape)) {
                                break;
                                
                            } else if (
    isNumber(text.charAt(1))) { // Schriftgröße

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

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

                                    
    String link text.substring(3);
                                    
    link link.substring(0link.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;
                                    
    += length;
                                    break;
                                } else {
                                    break;
                                }

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

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

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

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

                                
    length += color.length() + 1;

                                if (
    text.charAt(2) == '>') { // Link
                                    
    String link text.substring(3);
                                    
    link link.substring(0link.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;
                                }
                                
    += length;
                                break;
                            } else if (
    text.charAt(2) == '°') { // Farbe
                                
    String color text.substring(1);
                                
    color color.substring(0color.indexOf('°'));
                                
    code.append("<font color=\"").append(GetHtmlColor(color.charAt(0))).append("\">");
                                
    += color.length() + 1;
                            } else if (
    text.charAt(1) == '[') {  // rgb 
                                
    String rgbColor text.substring(1);
                                
    rgbColor rgbColor.substring(0rgbColor.indexOf('°'));
                                
    rgbColor parseColor(rgbColor);
                                
    code.append("<font color=\"").append(rgbColor).append("\">");
                                
    += 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 (
    == 'A') {
                return 
    "GRAY";
            } else if (
    == 'B') {
                return 
    "BLUE";
            } else if (
    == 'C') {
                return 
    "CYAN";
            } else if (
    == 'D') {
                return 
    "DARKGRAY";
            } else if (
    == 'E') {
                return 
    "DARKGREEN";
            } else if (
    == 'G') {
                return 
    "GREEN";
            } else if (
    == 'K') {
                return 
    "BLACK";
            } else if (
    == 'Y') {
                return 
    "YELLOW";
            } else if (
    == 'L') {
                return 
    "LIGHTBLUE";
            } else if (
    == 'M') {
                return 
    "VIOLET";
            } else if (
    == 'N') {
                return 
    "BROWN";
            } else if (
    == 'O') {
                return 
    "ORANGE";
            } else if (
    == 'P') {
                return 
    "PINK";
            } else if (
    == 'R') {
                return 
    "RED";
            } else if (
    == 'W') {
                return 
    "WHITE";
            } else if (
    == 'Y') {
                return 
    "YELLOW";
            } else if (
    == 'r') {
                return 
    Integer.toString(fontColor.getRGB() & 0x00ffffff16);
            } 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(c1c2c3)).getRGB() & 0xffffff16);
            }
            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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191