Kawai Wiki

Compare Revisions

Ignore whitespace Rev 193 → Rev 194

/trunk/src/main/java/org/kawai/tag/kcode/Code.java
7,65 → 7,80
 
import org.mentaregex.Regex;
 
/**
* KCode class Code.
*
* @author Sergio Oliveira
*
*/
public class Code implements KCode {
@Override
public boolean hasBody() {
return true;
}
@Override
public String getTag() {
return "code";
}
@Override
public String process(PageContext context, Map<String, String> attrs, String html) {
StringBuilder sb = new StringBuilder(1024 * 10);
boolean indent = true;
if (attrs.containsKey("ul")) {
indent = attrs.get("ul").equals("true");
}
 
if (indent) sb.append("<ul>");
sb.append(buildOpenShTag(attrs));
sb.append(escapeAngleBrackets(html));
sb.append("</pre>");
if (indent) sb.append("</ul>");
sb.append("<br/>");
return sb.toString();
}
private String escapeAngleBrackets(String html) {
String s = Regex.sub(html, "s/\\</\\&lt\\;/g");
s = Regex.sub(s, "s/\\>/\\&gt\\;/g");
return s;
}
private static String buildOpenShTag(Map<String, String> map) {
StringBuilder sb = new StringBuilder(256);
sb.append("<pre class=\"");
if (map.containsKey("brush")) {
sb.append("brush: ").append(map.get("brush")).append(";");
} else {
sb.append("brush: java;");
}
map.remove("brush");
if (map.containsKey("highlight")) {
sb.append(" highlight: [").append(map.get("highlight")).append("];");
}
map.remove("highlight");
Iterator<String> iter = map.keySet().iterator();
while(iter.hasNext()) {
String name = iter.next();
if (name.equals("ul")) continue;
String value = map.get(name);
sb.append(" ").append(name).append(": ").append(value).append(";");
}
sb.append("\">");
return sb.toString();
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean hasBody() {
return true;
}
 
/**
* {@inheritDoc}
*/
@Override
public String getTag() {
return "code";
}
 
/**
* {@inheritDoc}
*/
@Override
public String process(PageContext context, Map<String, String> attrs, String html) {
 
StringBuilder sb = new StringBuilder(1024 * 10);
 
boolean indent = true;
 
if (attrs.containsKey("ul")) {
indent = attrs.get("ul").equals("true");
}
 
if (indent) sb.append("<ul>");
sb.append(buildOpenShTag(attrs));
sb.append(escapeAngleBrackets(html));
sb.append("</pre>");
if (indent) sb.append("</ul>");
sb.append("<br/>");
return sb.toString();
}
 
private String escapeAngleBrackets(String html) {
String s = Regex.sub(html, "s/\\</\\&lt\\;/g");
s = Regex.sub(s, "s/\\>/\\&gt\\;/g");
return s;
}
 
private static String buildOpenShTag(Map<String, String> map) {
StringBuilder sb = new StringBuilder(256);
sb.append("<pre class=\"");
if (map.containsKey("brush")) {
sb.append("brush: ").append(map.get("brush")).append(";");
} else {
sb.append("brush: java;");
}
map.remove("brush");
if (map.containsKey("highlight")) {
sb.append(" highlight: [").append(map.get("highlight")).append("];");
}
map.remove("highlight");
Iterator<String> iter = map.keySet().iterator();
while(iter.hasNext()) {
String name = iter.next();
if (name.equals("ul")) continue;
String value = map.get(name);
sb.append(" ").append(name).append(": ").append(value).append(";");
}
sb.append("\">");
return sb.toString();
}
}