From aef615a22f4886ff49b88693fc2ea0c9a79aaf24 Mon Sep 17 00:00:00 2001
From: Mathieu Giraud <mathieu@vidjil.org>
Date: Thu, 23 Jan 2020 08:57:08 +0100
Subject: [PATCH] mkdocs-gitlab-plugin: initial commit, simple string transform

---
 LICENSE                          | 19 +++++++++++++
 mkdocs_gitlab_plugin/__init__.py |  0
 mkdocs_gitlab_plugin/plugin.py   | 49 ++++++++++++++++++++++++++++++++
 setup.py                         | 18 ++++++++++++
 4 files changed, 86 insertions(+)
 create mode 100644 LICENSE
 create mode 100644 mkdocs_gitlab_plugin/__init__.py
 create mode 100644 mkdocs_gitlab_plugin/plugin.py
 create mode 100644 setup.py

diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..a50be4d
--- /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 0000000..e69de29
diff --git a/mkdocs_gitlab_plugin/plugin.py b/mkdocs_gitlab_plugin/plugin.py
new file mode 100644
index 0000000..30d65d3
--- /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 0000000..aa25e6a
--- /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',
+        ]
+    },
+)
-- 
GitLab