Skip to content

Commit

Permalink
Merge remote-tracking branch 'fork/581'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jun 3, 2015
2 parents 994ba15 + cf5d0a4 commit 08db041
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
31 changes: 30 additions & 1 deletion netbout-web/src/main/java/com/netbout/rest/Markdown.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.regex.Pattern;
import javax.validation.constraints.NotNull;
import org.apache.commons.codec.CharEncoding;
import org.apache.commons.io.IOUtils;
Expand All @@ -56,6 +57,24 @@ public final class Markdown {
*/
private static final Tidy TIDY = Markdown.makeTidy();

/**
* Plain link detection pattern.
* The regex consists of the following parts:
* <ul>
* <li>Negative lookbehind to determine we skip properly formatted
* links (e.g. [foo](http://bar))</li>
* <li>Scheme prefix followed by arbitrary number of URL-valid
* characters</li>
* <li>Last character &ndash; URL-valid characters with exclusion of common
* punctuation like dot, comma, question mark, exclamation mark, semicolon,
* colon, and parentheses</li>
* </ul>
*/
private static final Pattern LINK = Pattern.compile(
// @checkstyle LineLength (1 line)
"(?<!(\\]\\())(https?://[a-zA-Z0-9-._~:/\\?#\\[\\]\\(\\)@!$&'*+,;=%]+[a-zA-Z0-9-_~/#\\[\\]@$&'*+=%])"
);

/**
* The source text.
*/
Expand All @@ -78,7 +97,9 @@ public Markdown(@NotNull final String txt) {
public String html() {
synchronized (Markdown.TIDY) {
return Markdown.clean(
new PegDownProcessor().markdownToHtml(this.text)
new PegDownProcessor().markdownToHtml(
Markdown.formatLinks(this.text)
)
);
}
}
Expand Down Expand Up @@ -133,4 +154,12 @@ private static Tidy makeTidy() {
return tidy;
}

/**
* Replace plain links with Markdown syntax.
* @param txt Text to find links in
* @return Text with Markdown-formatted links
*/
private static String formatLinks(final String txt) {
return LINK.matcher(txt).replaceAll("[$2]($2)");
}
}
41 changes: 41 additions & 0 deletions netbout-web/src/test/java/com/netbout/rest/MarkdownTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void formatsTextFragmentsToHtml() throws Exception {
MatcherAssert.assertThat(
new Markdown(pair[0]).html().trim(),
Matchers.equalTo(
// @checkstyle MultipleStringLiteralsCheck (1 line)
pair[1].replace("\n", System.getProperty("line.separator"))
)
);
Expand Down Expand Up @@ -155,4 +156,44 @@ public void leavesDivUntouched() throws Exception {
);
}

/**
* Markdown can detect plain text links and produce HTML
* with links wrapped correctly.
* @throws Exception If there is some problem inside
*/
@Test
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops")
public void detectsLinks() throws Exception {
final String[][] texts = {
new String[] {
"http://foo.com",
"<p>\n <a href=\"http://foo.com\">http://foo.com</a>\n</p>",
},
new String[] {
"(http://foo?com)",
"<p>(\n<a href=\"http://foo?com\">http://foo?com</a>)</p>",
},
new String[] {
"(http://foo#com)",
"<p>(\n<a href=\"http://foo#com\">http://foo#com</a>)</p>",
},
new String[] {
"(https://a?b=c)",
"<p>(\n<a href=\"https://a?b=c\">https://a?b=c</a>)</p>",
},
new String[] {
"[foo](http://foo)",
"<p>\n <a href=\"http://foo\">foo</a>\n</p>",
},
};
for (final String[] pair : texts) {
MatcherAssert.assertThat(
new Markdown(pair[0]).html().trim(),
Matchers.equalTo(
// @checkstyle MultipleStringLiteralsCheck (1 line)
pair[1].replace("\n", System.getProperty("line.separator"))
)
);
}
}
}

0 comments on commit 08db041

Please sign in to comment.