Skip to content

Commit

Permalink
Added markdown support
Browse files Browse the repository at this point in the history
Fixes issue #165
kohsuke committed Mar 22, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 75512ff commit 5bf252e
Showing 5 changed files with 87 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/main/java/org/kohsuke/github/GHRepository.java
Original file line number Diff line number Diff line change
@@ -30,7 +30,9 @@
import javax.xml.bind.DatatypeConverter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.Reader;
import java.net.URL;
import java.util.*;

@@ -1149,7 +1151,25 @@ public int getContributions() {
return contributions;
}
}


/**
* Render a Markdown document.
*
* In {@linkplain MarkdownMode#GFM GFM mode}, issue numbers and user mentions
* are linked accordingly.
*
* @see GitHub#renderMarkdown(String)
*/
public Reader renderMarkdown(String text, MarkdownMode mode) throws IOException {
return new InputStreamReader(
new Requester(root)
.with("text", text)
.with("mode",mode==null?null:mode.toString())
.with("context", getFullName())
.read("/markdown"),
"UTF-8");
}



@Override
21 changes: 21 additions & 0 deletions src/main/java/org/kohsuke/github/GitHub.java
Original file line number Diff line number Diff line change
@@ -26,8 +26,10 @@
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.ANY;
import static com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility.NONE;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
@@ -470,6 +472,25 @@ protected void wrapUp(GHRepository[] page) {
};
}

/**
* Render a Markdown document in raw mode.
*
* <p>
* It takes a Markdown document as plaintext and renders it as plain Markdown
* without a repository context (just like a README.md file is rendered – this
* is the simplest way to preview a readme online).
*
* @see GHRepository#renderMarkdown(String, MarkdownMode)
*/
public Reader renderMarkdown(String text) throws IOException {
return new InputStreamReader(
new Requester(this)
.with(new ByteArrayInputStream(text.getBytes("UTF-8")))
.contentType("text/plain;charset=UTF-8")
.read("/markdown/raw"),
"UTF-8");
}

/*package*/ static URL parseURL(String s) {
try {
return s==null ? null : new URL(s);
29 changes: 29 additions & 0 deletions src/main/java/org/kohsuke/github/MarkdownMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.kohsuke.github;

import java.util.Locale;

/**
* Rendering mode of markdown.
*
* @author Kohsuke Kawaguchi
* @see GitHub#renderMarkdown(String)
* @see GHRepository#renderMarkdown(String, MarkdownMode)
*/
public enum MarkdownMode {
/**
* Render a document as plain Markdown, just like README files are rendered.
*/
MARKDOWN,
/**
* Render a document as user-content, e.g. like user comments or issues are rendered.
* In GFM mode, hard line breaks are always taken into account, and issue and user
* mentions are linked accordingly.
*
* @see GHRepository#renderMarkdown(String, MarkdownMode)
*/
GFM;

public String toString() {
return name().toLowerCase(Locale.ENGLISH);
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/kohsuke/github/Requester.java
Original file line number Diff line number Diff line change
@@ -251,7 +251,7 @@ public InputStream read(String tailApiUrl) throws IOException {
buildRequest(uc);

try {
return uc.getInputStream();
return wrapStream(uc,uc.getInputStream());
} catch (IOException e) {
handleApiError(e,uc);
}
16 changes: 15 additions & 1 deletion src/test/java/org/kohsuke/github/AppTest.java
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;

import org.apache.commons.io.IOUtils;
import org.junit.Assume;
import org.junit.Test;
import org.kohsuke.github.GHCommit.File;
@@ -758,11 +759,24 @@ public void testIssue162() throws Exception {
String content1 = content.getContent();
String content2 = r.getFileContent(content.getPath(), "gh-pages").getContent();
System.out.println(content.getPath());
assertEquals(content1,content2);
assertEquals(content1, content2);
}
}
}

@Test
public void markDown() throws Exception {
assertEquals("<p><strong>Test日本語</strong></p>", IOUtils.toString(gitHub.renderMarkdown("**Test日本語**")).trim());

String actual = IOUtils.toString(gitHub.getRepository("kohsuke/github-api").renderMarkdown("@kohsuke to fix issue #1", MarkdownMode.GFM));
System.out.println(actual);
assertTrue(actual.contains("href=\"https://github.com/kohsuke\""));
assertTrue(actual.contains("href=\"https://github.com/kohsuke/github-api/pull/1\""));
assertTrue(actual.contains("class=\"user-mention\""));
assertTrue(actual.contains("class=\"issue-link\""));
assertTrue(actual.contains("to fix issue"));
}

private void kohsuke() {
String login = getUser().getLogin();
Assume.assumeTrue(login.equals("kohsuke") || login.equals("kohsuke2"));

0 comments on commit 5bf252e

Please sign in to comment.