Skip to content

Instantly share code, notes, and snippets.

@komiya-atsushi
Created April 15, 2014 05:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komiya-atsushi/10705657 to your computer and use it in GitHub Desktop.
Save komiya-atsushi/10705657 to your computer and use it in GitHub Desktop.
GitHub API の /markdown を呼び出す Java コード。
import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
/**
* GitHub API を利用して Markdown 書式のテキストを HTML としてレンダリングする機能を提供します。
* <p>
* HttpComponents と GSON を利用しています。以下に {@code build.gradle} での指定方法を記載しています。
* </p>
* <pre>
* dependencies {
* compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.1'
* compile group: 'com.google.code.gson', name: 'gson', version: '2.2.4'
* }
* </pre>
*
* @author KOMIYA Atsushi
* @see <a href="https://developer.github.com/v3/markdown/">Markdown | GitHub API</a>
*/
public class Markdown {
public static enum Mode {
markdown,
gfm
}
public static class Request {
public String text;
public String mode;
public String context;
public Request text(String text) {
this.text = text;
return this;
}
public Request mode(Mode mode) {
this.mode = mode.toString();
return this;
}
public String toJson() {
return new Gson().toJson(this);
}
}
/**
* {@code /makrdown} のエンドポイントを呼び出して {@code text} の内容を HTML としてレンダリングし、その HTML を返却します。
*
* @param text Markdown 書式で書かれたテキスト。
* @param mode レンダリングモード {@link albert.lab.github.Markdown.Mode} を指定します。
* @return レンダリングされた HTML
*/
public String markdown(String text, Mode mode) {
try (CloseableHttpClient client = HttpClientBuilder.create().build()) {
HttpPost postRequest = new HttpPost(URI.create("https://api.github.com/markdown"));
postRequest.addHeader("Content-Type", "application/json");
String body = new Request()
.text(text)
.mode(mode)
.toJson();
HttpEntity entity = new StringEntity(body, "UTF-8");
postRequest.setEntity(entity);
try (CloseableHttpResponse resp = client.execute(postRequest);
InputStream is = resp.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isr)) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\n");
}
return sb.toString();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* 使い方。
*
* @param args
*/
public static void main(String[] args) {
String html = new Markdown().markdown("### 見出し\n\n- ほげ\n- ふが", Mode.markdown);
System.out.println(html);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment