Kawai Wiki

Rev

Rev 195 | Rev 202 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
93 soliveira 1
package org.kawai.tag.kcode;
2
 
3
import java.util.Iterator;
4
import java.util.Map;
5
 
6
import javax.servlet.jsp.PageContext;
7
 
95 soliveira 8
import org.mentaregex.Regex;
9
 
194 helio.frota 10
/**
11
 * KCode class Code.
12
 *
13
 * @author Sergio Oliveira
14
 *
15
 */
93 soliveira 16
public class Code implements KCode {
17
 
195 helio.frota 18
    /**
19
     * {@inheritDoc}.
194 helio.frota 20
     */
21
    @Override
22
    public boolean hasBody() {
23
        return true;
24
    }
25
 
26
    /**
27
     * {@inheritDoc}
28
     */
29
    @Override
30
    public String getTag() {
31
        return "code";
32
    }
33
 
34
    /**
35
     * {@inheritDoc}
36
     */
37
    @Override
38
    public String process(PageContext context, Map<String, String> attrs, String html) {
39
 
40
        StringBuilder sb = new StringBuilder(1024 * 10);
41
 
42
        boolean indent = true;
43
 
44
        if (attrs.containsKey("ul")) {
45
            indent = attrs.get("ul").equals("true");
46
        }
47
 
48
        if (indent) sb.append("<ul>");
49
        sb.append(buildOpenShTag(attrs));
50
        sb.append(escapeAngleBrackets(html));
51
        sb.append("</pre>");
52
        if (indent) sb.append("</ul>");
53
        sb.append("<br/>");
54
        return sb.toString();
55
    }
56
 
195 helio.frota 57
    /**
58
     * Escapes the angle brackets.
59
     * @param html String
60
     * @return String
61
     */
198 soliveira 62
    public static String escapeAngleBrackets(String html) {
194 helio.frota 63
        String s = Regex.sub(html, "s/\\</\\&lt\\;/g");
64
        s = Regex.sub(s, "s/\\>/\\&gt\\;/g");
65
        return s;
66
    }
67
 
195 helio.frota 68
    /**
69
     * Builds the tag for syntax highlight.
70
     * @param map Map<String, String>
71
     * @return String
72
     */
194 helio.frota 73
    private static String buildOpenShTag(Map<String, String> map) {
74
        StringBuilder sb = new StringBuilder(256);
75
        sb.append("<pre class=\"");
76
        if (map.containsKey("brush")) {
77
            sb.append("brush: ").append(map.get("brush")).append(";");
78
        } else {
79
            sb.append("brush: java;");
80
        }
81
        map.remove("brush");
82
        if (map.containsKey("highlight")) {
83
            sb.append(" highlight: [").append(map.get("highlight")).append("];");
84
        }
85
        map.remove("highlight");
86
        Iterator<String> iter = map.keySet().iterator();
195 helio.frota 87
        while (iter.hasNext()) {
194 helio.frota 88
            String name = iter.next();
89
            if (name.equals("ul")) continue;
90
            String value = map.get(name);
91
            sb.append(" ").append(name).append(": ").append(value).append(";");
92
        }
93
        sb.append("\">");
94
        return sb.toString();
95
    }
96
}