ClickHouse/docs/tools/mdx_clickhouse.py

53 lines
1.5 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import markdown.inlinepatterns
import markdown.extensions
import markdown.util
import slugify as slugify_impl
class NofollowMixin(object):
def handleMatch(self, m):
try:
el = super(NofollowMixin, self).handleMatch(m)
except IndexError:
return
if el is not None:
href = el.get('href') or ''
if href.startswith('http') and not href.startswith('https://clickhouse.yandex'):
el.set('rel', 'external nofollow')
return el
class NofollowAutolinkPattern(NofollowMixin, markdown.inlinepatterns.AutolinkPattern):
pass
class NofollowLinkPattern(NofollowMixin, markdown.inlinepatterns.LinkPattern):
pass
class ClickHousePreprocessor(markdown.util.Processor):
def run(self, lines):
for line in lines:
if '<!--hide-->' not in line:
yield line
class ClickHouseMarkdown(markdown.extensions.Extension):
def extendMarkdown(self, md, md_globals):
md.preprocessors['clickhouse'] = ClickHousePreprocessor()
md.inlinePatterns['link'] = NofollowLinkPattern(markdown.inlinepatterns.LINK_RE, md)
2018-12-04 17:20:39 +00:00
md.inlinePatterns['autolink'] = NofollowAutolinkPattern(markdown.inlinepatterns.AUTOLINK_RE, md)
def makeExtension(**kwargs):
return ClickHouseMarkdown(**kwargs)
def slugify(value, separator):
return slugify_impl.slugify(value, separator=separator, word_boundary=True, save_order=True)