Skip to content

Commit 9be50be

Browse files
author
Gauvain Pocentek
committed
Implement the markdown rendering API
Testing will be enable when GitLab 11.0 is available.
1 parent fbd2010 commit 9be50be

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

gitlab/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,33 @@ def version(self):
227227

228228
return self._server_version, self._server_revision
229229

230+
def markdown(self, text, gfm=False, project=None, **kwargs):
231+
"""Render an arbitrary Markdown document.
232+
233+
Args:
234+
text (str): The markdown text to render
235+
gfm (bool): Render text using GitLab Flavored Markdown. Default is
236+
False
237+
project (str): Full path of a project used a context when `gfm` is
238+
True
239+
**kwargs: Extra options to send to the server (e.g. sudo)
240+
241+
Raises:
242+
GitlabAuthenticationError: If authentication is not correct
243+
GitlabMarkdownError: If the server cannot perform the request
244+
245+
Returns:
246+
str: The HTML rendering of the markdown text.
247+
"""
248+
post_data = {'text': text, 'gfm': gfm}
249+
if project is not None:
250+
post_data['project'] = project
251+
try:
252+
data = self.http_post('/markdown', post_data=post_data, **kwargs)
253+
except Exception:
254+
raise GitlabMarkdownError
255+
return data['html']
256+
230257
def _construct_url(self, id_, obj, parameters, action=None):
231258
if 'next_url' in parameters:
232259
return parameters['next_url']

gitlab/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ class GitlabStopError(GitlabOperationError):
205205
pass
206206

207207

208+
class GitlabMarkdownError(GitlabOperationError):
209+
pass
210+
211+
208212
def on_http_error(error):
209213
"""Manage GitlabHttpError exceptions.
210214

gitlab/v4/objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1981,8 +1981,8 @@ def delete(self, name, **kwargs):
19811981
**kwargs: Extra options to send to the Gitlab server (e.g. sudo)
19821982
19831983
Raises:
1984-
GitlabAuthenticationError: If authentication is not correct.
1985-
GitlabDeleteError: If the server cannot perform the request.
1984+
GitlabAuthenticationError: If authentication is not correct
1985+
GitlabDeleteError: If the server cannot perform the request
19861986
"""
19871987
self.gitlab.http_delete(self.path, query_data={'name': name}, **kwargs)
19881988

tools/python_test_v4.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
gl.auth()
6161
assert(isinstance(gl.user, gitlab.v4.objects.CurrentUser))
6262

63+
# markdown (need to wait for gitlab 11 to enable the test)
64+
# html = gl.markdown('foo')
65+
# assert('foo' in html)
66+
6367
# sidekiq
6468
out = gl.sidekiq.queue_metrics()
6569
assert(isinstance(out, dict))

0 commit comments

Comments
 (0)