io.github.gitbucket.markedj.Renderer Maven / Gradle / Ivy
package io.github.gitbucket.markedj;
import static io.github.gitbucket.markedj.Utils.*;
import java.util.Map;
public class Renderer {
protected Options options;
public Renderer(Options options){
this.options = options;
}
public String code(String code, String lang, boolean escaped){
if(lang != null){
StringBuilder sb = new StringBuilder();
if (escape(lang, true).equals("math")) {
sb.append("\n$$\n");
sb.append(code); // Markdownのエスケープはしない
sb.append("\n$$\n");
} else {
sb.append("");
if(escaped){
sb.append(code);
} else {
sb.append(escape(code, true));
}
sb.append("\n
\n");
}
return sb.toString();
} else {
StringBuilder sb = new StringBuilder();
sb.append("");
if(escaped){
sb.append(code);
} else {
sb.append(escape(code, true));
}
sb.append("\n
\n");
return sb.toString();
}
}
public String mathBlock(String code){
StringBuilder sb = new StringBuilder();
sb.append("\n");
sb.append(code); // Markdownのエスケープはしない
sb.append("\n\n");
return sb.toString();
}
public String blockquote(String quote){
return "\n" + quote + "
\n";
}
public String html(String html){
return html;
}
public String heading(String text, int level, String raw){
if (options.isHeaderIdSequential()) {
return "" + text + " \n";
}
return "" + text + " \n";
}
public String hr() {
if (options.isXhtml()){
return "
\n";
} else {
return "
\n";
}
}
public String list(String body, boolean ordered){
String listType;
if(ordered){
listType = "ol";
} else {
listType = "ul";
}
return "<" + listType + ">\n" + body + "" + listType + ">\n";
}
public String listitem(String text){
return "" + text + " \n";
}
public String paragraph(String text){
return "" + text + "
\n";
}
public String table(String header, String body){
return "\n\n" + header + "\n\n" + body + "\n
\n";
}
public String tablerow(String content){
return "\n" + content + " \n";
}
public String tablecell(String content, TableCellFlags flags){
String cellType;
if(flags.isHeader()){
cellType = "th";
} else {
cellType = "td";
}
String align = flags.getAlign();
if(align != null){
return "<" + cellType + " style=\"text-align: " + align + "\">" + content + "" + cellType + ">\n";
} else {
return "<" + cellType + ">" + content + "" + cellType + ">\n";
}
}
public String strong(String text){
return "" + text + "";
}
public String em(String text){
return "" + text + "";
}
public String codespan(String text){
return "" + text + "
";
}
public String br(){
if(options.isXhtml()){
return "
";
} else {
return "
";
}
}
public String del(String text){
return "" + text + "";
}
public String oembed(String href) {
return "" + href + "";
}
public String slide(String fileNo) {
return "" + fileNo + "";
}
public Object internallink(String noTerm) {
String no = noTerm.substring("#".length());
return "" + noTerm + "";
}
public String footnoteref(String key) {
return ""
+ "" + key + "";
}
public String footnote(Map notes) {
String out = "";
for (Map.Entry noteEntry : notes.entrySet()) {
String key = noteEntry.getKey();
String text = noteEntry.getValue();
out += "- ";
out += text.trim();
out += "↩";
out += "
";
}
out += "
";
return out;
}
public String link(String href, String title, String text){
if(options.isSanitize()){
// TODO
}
String titleAttr = "";
if(title != null){
titleAttr = " title=\"" + title + "\"";
}
boolean external = false;
if (href.startsWith("http")) {
external = true;
}
StringBuilder builder = new StringBuilder();
builder.append("").append(text).append("");
return builder.toString();
}
public String image(String href, String title, String text){
String titleAttr = "";
if(title != null){
titleAttr = " title=\"" + title + "\"";
}
if(options.isXhtml()){
return "";
} else {
return "";
}
}
public String nolink(String text){
return escape(text);
}
public String text(String text){
return text;
}
public static class TableCellFlags {
private boolean header;
private String align;
public TableCellFlags(boolean header, String align){
this.header = header;
this.align = align;
}
public boolean isHeader() {
return header;
}
public String getAlign() {
return align;
}
}
}