Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 484216fa6bcec98fba60a75ec8b42e15121d3d04 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/******************************************************************************
 *  Copyright (c) 2012 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.github.core.service;

import static org.eclipse.egit.github.core.client.IGitHubConstants.CHARSET_UTF8;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_MARKDOWN;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.egit.github.core.IRepositoryIdProvider;
import org.eclipse.egit.github.core.client.GitHubClient;

/**
 * Service to request Markdown text to be rendered as HTML
 *
 * @see <a href="http://developer.github.com/v3/markdown/">GitHub Markdown API
 *      documentation</a>
 */
public class MarkdownService extends GitHubService {

	/**
	 * GitHub-flavored Markdown mode
	 */
	public static final String MODE_GFM = "gfm";

	/**
	 * Default Markdown mode
	 */
	public static final String MODE_MARKDOWN = "markdown";

	/**
	 * Create Markdown service
	 */
	public MarkdownService() {
		super();
	}

	/**
	 * Create Markdown service for client
	 *
	 * @param client
	 */
	public MarkdownService(final GitHubClient client) {
		super(client);
	}

	private String readStream(final InputStream stream) throws IOException {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				stream, CHARSET_UTF8));
		try {
			StringBuilder output = new StringBuilder();
			char[] buffer = new char[8192];
			int read;
			while ((read = reader.read(buffer)) != -1)
				output.append(buffer, 0, read);
			return output.toString();
		} finally {
			try {
				reader.close();
			} catch (IOException ignored) {
				// Ignored
			}
		}
	}

	/**
	 * Get stream of HTML for given Markdown text scoped to given repository
	 * context
	 *
	 * @param repo
	 * @param text
	 * @return stream of HTML
	 * @throws IOException
	 */
	public InputStream getRepositoryStream(final IRepositoryIdProvider repo,
			final String text) throws IOException {
		String context = getId(repo);

		Map<String, String> params = new HashMap<String, String>(3, 1);
		params.put("context", context);
		params.put("text", text);
		params.put("mode", MODE_GFM);

		return client.postStream(SEGMENT_MARKDOWN, params);
	}

	/**
	 * Get HTML for given Markdown text scoped to given repository context
	 *
	 * @param repo
	 * @param text
	 * @return HTML
	 * @throws IOException
	 */
	public String getRepositoryHtml(final IRepositoryIdProvider repo,
			final String text) throws IOException {
		return readStream(getRepositoryStream(repo, text));
	}

	/**
	 * Get stream of HTML for given Markdown text
	 * <p>
	 * Use {@link #getRepositoryStream(IRepositoryIdProvider, String)} if you
	 * want the Markdown scoped to a specific repository.
	 *
	 * @param text
	 * @param mode
	 * @return stream of HTML
	 * @throws IOException
	 */
	public InputStream getStream(final String text, final String mode)
			throws IOException {
		Map<String, String> params = new HashMap<String, String>(2, 1);
		params.put("text", text);
		params.put("mode", mode);

		return client.postStream(SEGMENT_MARKDOWN, params);
	}

	/**
	 * Get HTML for given Markdown text
	 * <p>
	 * Use {@link #getRepositoryHtml(IRepositoryIdProvider, String)} if you want
	 * the Markdown scoped to a specific repository.
	 *
	 * @param text
	 * @param mode
	 * @return HTML
	 * @throws IOException
	 */
	public String getHtml(final String text, final String mode)
			throws IOException {
		return readStream(getStream(text, mode));
	}
}

Back to the top