diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..a50be4d689a94a58ef8921722f23b723bf1246e5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2020 The Vidjil Team <contact@vidjil.org> + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/mkdocs_gitlab_plugin/__init__.py b/mkdocs_gitlab_plugin/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/mkdocs_gitlab_plugin/plugin.py b/mkdocs_gitlab_plugin/plugin.py new file mode 100644 index 0000000000000000000000000000000000000000..30d65d38a59a7b9d323c67def9adcc35516806bc --- /dev/null +++ b/mkdocs_gitlab_plugin/plugin.py @@ -0,0 +1,49 @@ +#!/usr/bin/python3 + +import re +import mkdocs + +class GitlabLinksPlugin(mkdocs.plugins.BasePlugin): + ''' + Transform handles such as #1234, %56, !789 into links to a gitlab repository. + A space before the #/%/! is needed. + ''' + + config_scheme = ( + ('gitlab_url', mkdocs.config.config_options.Type(mkdocs.utils.string_types, default='http://gitlab.com/XXX')), + ) + + GITLAB_LINK = re.compile(r' [#!%]\d+', re.UNICODE) + + def on_page_markdown(self, markdown, page=None, config=None, **kwargs): + + links = re.findall(self.GITLAB_LINK, markdown) + + gitlab_url = self.config['gitlab_url'] + + tokens = { + ' #': 'issues', + ' !': 'merge_requests', + ' %': 'milestones', + ' &': 'epic', + } + + for link in links: + + if len(link) < 2: + continue + if not link[:2] in tokens: + continue + + token_url = gitlab_url + '/' + tokens[link[:2]] + + # Build the actual link + link_id = link[2:] + link_new = " [%s](%s/%s)" % (link[1:], token_url, link_id) + + # print('%s -> %s' % (link, link_new)) + + # Replace the link in the markdown + markdown = markdown.replace(link, link_new) + + return markdown \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000000000000000000000000000000000000..aa25e6a55d9a50d38f9c01336f1209cec1421de1 --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +from setuptools import setup + +setup( + name='mkdocs-gitlab-plugin', + version='0.1.0', + packages=['mkdocs_gitlab_plugin'], + url='https://gitlab.vidjil.org/mkdocs', + license='MIT', + author='magiraud', + keywords='markdown gitlab links', + description='MkDocs plugin to transform strings such as #1234, %56, or !789 into links to a Gitlab repository.', + install_requires=['mkdocs'], + entry_points={ + 'mkdocs.plugins': [ + 'gitlab_links = mkdocs_gitlab_plugin.plugin:GitlabLinksPlugin', + ] + }, +)