1. #1
    U-Hacks Routinier
    Avatar von uncopyable
    Registriert seit
    19.11.2011
    Beiträge
    282
    Thanked 234 Times in 120 Posts

    Beitrag KCodeParser (KCode to HTML)

    Hallo,
    hier mein KCodeParser, vielleicht kann es hier ja auch einer gebrauchen.

    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
    • °R>Text|link<°
    • °12>Text|link<°
    • °>sm_01.gif<°
    • °>_hJames|/serverpp "<°


    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() & 0x00ffffff, 16)).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() & 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(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>");
                                    }
                                    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;
        }

    Verbesserungsvorschlag sind erwünscht!

    mfg uncopyable

  2. #2
    Avatar von Sky.NET
    Registriert seit
    26.11.2011
    Beiträge
    2.438
    Thanked 2.707 Times in 1.282 Posts
    Blog Entries
    7

    Standard KCodeParser (KCode to HTML)

    Du musst die Farben vom Channel übergeben, sonst weiß dein Script nicht was es bei BB oder RR machen soll, und die Farben aller Links °>Nick|/w "|/serverpp "<° falsch darstellen würde.
    Java:
    Spoiler:

    Lustige Quotes:
    Spoiler:
    Zitat Zitat von Hydra Beitrag anzeigen
    Hier (in Deutschland) kann man keine andere tolle Aktivitäten machen, als zu chillen, shoppen, saufen und Partys feiern xD Ich habe nichts gegen ab und zu mal saufen und Partys feiern, aber das ist doch nicht wirklich das wahre Leben o_o
    Wieso das Internet für die meisten Leute gefährlich ist:
    Zitat Zitat von Silent Beitrag anzeigen
    Ich weiß ja nicht was der Sinn dahinter steckt es heißt immer "security reasons".

Ähnliche Themen

  1. Antworten: 0
    Letzter Beitrag: 14.10.2012, 20:07
  2. KCode Painting (?)
    Von Badboy12 im Forum Knuddels Programmierung
    Antworten: 2
    Letzter Beitrag: 28.06.2012, 19:34
  3. KCodeParser (KCode to HTML)
    Von uncopyable im Forum Sourcecode
    Antworten: 1
    Letzter Beitrag: 29.05.2012, 01:43
  4. [PHP] HTML Crawlbot
    Von Live0x00 im Forum Showroom
    Antworten: 4
    Letzter Beitrag: 01.12.2011, 20:09
  5. [Java] KCodeParser
    Von Flav im Forum Sourcecode
    Antworten: 0
    Letzter Beitrag: 02.04.2011, 17:44
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