Mentions légales du service

Skip to content
Snippets Groups Projects
Commit aef615a2 authored by Mathieu Giraud's avatar Mathieu Giraud
Browse files

mkdocs-gitlab-plugin: initial commit, simple string transform

parent f68b4f88
No related branches found
No related tags found
No related merge requests found
LICENSE 0 → 100644
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.
#!/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)
  • Owner

    [\s\(\[][#!%&$]\d+ ?

    • [\s\(\[] : pour permettre d'autres types d'espaces et les ( et [ pour les cas comme blabla (#XXXX)
    • [#!%&$] : pour inclure les epic et les snippets
  • Author Owner

    Oui, je te laisse voir cela. (Le plugin s'appelait récursivement (de temps en temps) quand il n'y avait pas l'espace avant, mais il doit y avoir des moyens d'enlever cela).

    Et même #1.

  • Author Owner

    Je m'y mets en fait.

  • Author Owner
  • Please register or sign in to reply
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
  • Author Owner

    Et donc aucune raison d'avoir cela en particulier. On peut avoir un ensemble de tokens, comme ['#', '%', '!', 'bli#', '<<'], de tailles différentes.

  • Author Owner

    fa721240: pas fait les tailles différentes, mais il suffirait désormais de changer la regex si besoin pour cela.

  • Please register or sign in to reply
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)
  • Owner

    Ça posera un problème si on a à la fois #123 et #1234 non ?

    Un re.sub (avec du text munging pour appeler une fonction construisant le lien), à la place du re.findall, pourrait-il marcher ?

    Edited by Mikaël Salson
  • Author Owner

    Ah oui, c'est ce qui faisait l'appel récursif !

    Tout à fait.

  • Author Owner
  • Please register or sign in to reply
return markdown
\ No newline at end of file
setup.py 0 → 100644
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',
]
},
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment