Skip to content

Commit

Permalink
fix the table link
Browse files Browse the repository at this point in the history
  • Loading branch information
ToDou committed Aug 28, 2016
1 parent 2827d3c commit e14130b
Showing 1 changed file with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,15 +420,14 @@ public String genericCodeBlock(String text) {
}

private TextEditor doTableBlocks(TextEditor markup) {
doAnchors(markup);
Pattern p = Pattern.compile(
"(\\|(?:[^\\n]*\\|)+\\n" +
"\\|(?:[ ]*-+[ ]*\\|)+\\n" +
"(?:\\|(?:[^\\n]*\\|)+[^\n]*\\n)+)", Pattern.DOTALL);
return markup.replaceAll(p, new Replacement() {
@Override
public String replacement(Matcher m) {
String tableMd = m.group(1);
String tableMd = getAnchorsString(m.group(1));
String[] lines = tableMd.split("\\n");
StringBuilder sb = new StringBuilder();
sb.append("<table class=\"table\"");
Expand Down Expand Up @@ -787,6 +786,47 @@ private void doLocalImage(TextEditor text) {
text.update(sb.toString());
}

private String getAnchorsString(String s) {
Pattern internalLink = Pattern.compile("(" +
"\\[(.*?)\\]" + // Link text = $2
"[ ]?(?:\\n[ ]*)?" +
"\\[(.*?)\\]" + // ID = $3
")");

return replaceAll(s, internalLink, new Replacement() {
@Override
public String replacement(Matcher m) {
String replacementText;
String wholeMatch = m.group(1);
String linkText = m.group(2);
String id = m.group(3).toLowerCase();
if ("".equals(id)) { // for shortcut links like [this][]
id = linkText.toLowerCase();
}

LinkDefinition defn = linkDefinitions.get(id);
if (defn != null) {
String url = defn.getUrl();
// protect emphasis (* and _) within urls
url = url.replaceAll("\\*", CHAR_PROTECTOR.encode("*"));
url = url.replaceAll("_", CHAR_PROTECTOR.encode("_"));
String title = defn.getTitle();
String titleTag = "";
if (title != null && !title.equals("")) {
// protect emphasis (* and _) within urls
title = title.replaceAll("\\*", CHAR_PROTECTOR.encode("*"));
title = title.replaceAll("_", CHAR_PROTECTOR.encode("_"));
titleTag = " title=\"" + title + "\"";
}
replacementText = "<a href=\"" + url + "\"" + titleTag + ">" + linkText + "</a>";
} else {
replacementText = wholeMatch;
}
return replacementText;
}
});
}

private TextEditor doAnchors(TextEditor markup) {
// Internal references: [link text] [id]
Pattern internalLink = Pattern.compile("(" +
Expand Down

0 comments on commit e14130b

Please sign in to comment.