From 9be50be98468e78400861718202f48eddfa83839 Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Tue, 29 May 2018 06:57:36 +0200 Subject: Implement the markdown rendering API Testing will be enable when GitLab 11.0 is available. --- gitlab/__init__.py | 27 +++++++++++++++++++++++++++ gitlab/exceptions.py | 4 ++++ gitlab/v4/objects.py | 4 ++-- 3 files changed, 33 insertions(+), 2 deletions(-) (limited to 'gitlab') diff --git a/gitlab/__init__.py b/gitlab/__init__.py index 134cada..fd4abcf 100644 --- a/gitlab/__init__.py +++ b/gitlab/__init__.py @@ -227,6 +227,33 @@ class Gitlab(object): return self._server_version, self._server_revision + def markdown(self, text, gfm=False, project=None, **kwargs): + """Render an arbitrary Markdown document. + + Args: + text (str): The markdown text to render + gfm (bool): Render text using GitLab Flavored Markdown. Default is + False + project (str): Full path of a project used a context when `gfm` is + True + **kwargs: Extra options to send to the server (e.g. sudo) + + Raises: + GitlabAuthenticationError: If authentication is not correct + GitlabMarkdownError: If the server cannot perform the request + + Returns: + str: The HTML rendering of the markdown text. + """ + post_data = {'text': text, 'gfm': gfm} + if project is not None: + post_data['project'] = project + try: + data = self.http_post('/markdown', post_data=post_data, **kwargs) + except Exception: + raise GitlabMarkdownError + return data['html'] + def _construct_url(self, id_, obj, parameters, action=None): if 'next_url' in parameters: return parameters['next_url'] diff --git a/gitlab/exceptions.py b/gitlab/exceptions.py index 9e3fa4a..64f3243 100644 --- a/gitlab/exceptions.py +++ b/gitlab/exceptions.py @@ -205,6 +205,10 @@ class GitlabStopError(GitlabOperationError): pass +class GitlabMarkdownError(GitlabOperationError): + pass + + def on_http_error(error): """Manage GitlabHttpError exceptions. diff --git a/gitlab/v4/objects.py b/gitlab/v4/objects.py index 65134db..2def952 100644 --- a/gitlab/v4/objects.py +++ b/gitlab/v4/objects.py @@ -1981,8 +1981,8 @@ class ProjectLabelManager(ListMixin, CreateMixin, UpdateMixin, DeleteMixin, **kwargs: Extra options to send to the Gitlab server (e.g. sudo) Raises: - GitlabAuthenticationError: If authentication is not correct. - GitlabDeleteError: If the server cannot perform the request. + GitlabAuthenticationError: If authentication is not correct + GitlabDeleteError: If the server cannot perform the request """ self.gitlab.http_delete(self.path, query_data={'name': name}, **kwargs) -- cgit v1.2.1