mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-10-06 16:40:48 +00:00
Generate anchors for single-page mode automatically
This commit is contained in:
parent
4e4d5477ca
commit
a433f1fd25
@ -54,6 +54,7 @@ markdown.extensions.ClickHouseMarkdown = ClickHouseMarkdown
|
|||||||
|
|
||||||
def build_for_lang(lang, args):
|
def build_for_lang(lang, args):
|
||||||
logging.info('Building %s docs' % lang)
|
logging.info('Building %s docs' % lang)
|
||||||
|
os.environ['SINGLE_PAGE'] = '0'
|
||||||
|
|
||||||
config_path = os.path.join(args.docs_dir, 'toc_%s.yml' % lang)
|
config_path = os.path.join(args.docs_dir, 'toc_%s.yml' % lang)
|
||||||
|
|
||||||
@ -139,6 +140,7 @@ def build_for_lang(lang, args):
|
|||||||
|
|
||||||
def build_single_page_version(lang, args, cfg):
|
def build_single_page_version(lang, args, cfg):
|
||||||
logging.info('Building single page version for ' + lang)
|
logging.info('Building single page version for ' + lang)
|
||||||
|
os.environ['SINGLE_PAGE'] = '1'
|
||||||
|
|
||||||
with autoremoved_file(os.path.join(args.docs_dir, lang, 'single.md')) as single_md:
|
with autoremoved_file(os.path.join(args.docs_dir, lang, 'single.md')) as single_md:
|
||||||
concatenate(lang, args.docs_dir, single_md)
|
concatenate(lang, args.docs_dir, single_md)
|
||||||
|
@ -37,34 +37,21 @@ def concatenate(lang, docs_path, single_page_file):
|
|||||||
first_file = True
|
first_file = True
|
||||||
|
|
||||||
for path in files_to_concatenate:
|
for path in files_to_concatenate:
|
||||||
|
|
||||||
single_page_file.write('\n\n')
|
|
||||||
|
|
||||||
with open(os.path.join(lang_path, path)) as f:
|
with open(os.path.join(lang_path, path)) as f:
|
||||||
|
tmp_path = path.replace('.md', '/')
|
||||||
|
prefixes = ['', '../', '../../', '../../../']
|
||||||
|
parts = tmp_path.split('/')
|
||||||
|
single_page_file.write('<a name="%s/"></a>\n' % parts[-2])
|
||||||
|
single_page_file.write('\n\n')
|
||||||
|
|
||||||
# function is passed into re.sub() to process links
|
for part in parts[0:-2]:
|
||||||
def link_proc(matchObj):
|
for prefix in prefixes:
|
||||||
text, link = matchObj.group().strip('[)').split('](', 1)
|
single_page_file.write('<a name="%s"></a>\n' % (prefix + tmp_path))
|
||||||
if link.startswith('http:') or link.startswith('https:') or '.jpeg' in link or '.jpg' in link or '.png' in link or '.gif' in link:
|
tmp_path = tmp_path.replace(part, '..')
|
||||||
return '[' + text + '](' + link + ')'
|
|
||||||
else:
|
|
||||||
sharp_pos = link.find('#')
|
|
||||||
if sharp_pos > -1:
|
|
||||||
return '[' + text + '](' + link[sharp_pos:] + ')'
|
|
||||||
else:
|
|
||||||
return '[' + text + '](#' + link.replace('../', '').replace('/index.md', '').replace('.md', '') + ')'
|
|
||||||
|
|
||||||
for l in f:
|
for l in f:
|
||||||
# Processing links in a string
|
if l.startswith('#'):
|
||||||
l = re.sub(r'\[.+?\]\(.+?\)', link_proc, l)
|
l = '#' + l
|
||||||
|
|
||||||
# Correcting headers levels
|
|
||||||
if not first_file:
|
|
||||||
if l.startswith('#'):
|
|
||||||
l = '#' + l
|
|
||||||
else:
|
|
||||||
first_file = False
|
|
||||||
|
|
||||||
single_page_file.write(l)
|
single_page_file.write(l)
|
||||||
|
|
||||||
single_page_file.flush()
|
single_page_file.flush()
|
||||||
|
@ -2,31 +2,41 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
import markdown.inlinepatterns
|
import markdown.inlinepatterns
|
||||||
import markdown.extensions
|
import markdown.extensions
|
||||||
import markdown.util
|
import markdown.util
|
||||||
|
|
||||||
import slugify as slugify_impl
|
import slugify as slugify_impl
|
||||||
|
|
||||||
class NofollowMixin(object):
|
class ClickHouseLinkMixin(object):
|
||||||
|
|
||||||
def handleMatch(self, m):
|
def handleMatch(self, m):
|
||||||
|
single_page = (os.environ.get('SINGLE_PAGE') == '1')
|
||||||
try:
|
try:
|
||||||
el = super(NofollowMixin, self).handleMatch(m)
|
el = super(ClickHouseLinkMixin, self).handleMatch(m)
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return
|
return
|
||||||
|
|
||||||
if el is not None:
|
if el is not None:
|
||||||
href = el.get('href') or ''
|
href = el.get('href') or ''
|
||||||
if href.startswith('http') and not href.startswith('https://clickhouse.yandex'):
|
is_external = href.startswith('http:') or href.startswith('https:')
|
||||||
|
if is_external and not href.startswith('https://clickhouse.yandex'):
|
||||||
el.set('rel', 'external nofollow')
|
el.set('rel', 'external nofollow')
|
||||||
|
elif single_page:
|
||||||
|
if '#' in href:
|
||||||
|
el.set('href', '#' + href.split('#', 1)[1])
|
||||||
|
else:
|
||||||
|
el.set('href', '#' + href.replace('.md', '/'))
|
||||||
return el
|
return el
|
||||||
|
|
||||||
|
|
||||||
class NofollowAutolinkPattern(NofollowMixin, markdown.inlinepatterns.AutolinkPattern):
|
class ClickHouseAutolinkPattern(ClickHouseLinkMixin, markdown.inlinepatterns.AutolinkPattern):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class NofollowLinkPattern(NofollowMixin, markdown.inlinepatterns.LinkPattern):
|
class ClickHouseLinkPattern(ClickHouseLinkMixin, markdown.inlinepatterns.LinkPattern):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
@ -41,9 +51,8 @@ class ClickHouseMarkdown(markdown.extensions.Extension):
|
|||||||
|
|
||||||
def extendMarkdown(self, md, md_globals):
|
def extendMarkdown(self, md, md_globals):
|
||||||
md.preprocessors['clickhouse'] = ClickHousePreprocessor()
|
md.preprocessors['clickhouse'] = ClickHousePreprocessor()
|
||||||
md.inlinePatterns['link'] = NofollowLinkPattern(markdown.inlinepatterns.LINK_RE, md)
|
md.inlinePatterns['link'] = ClickHouseLinkPattern(markdown.inlinepatterns.LINK_RE, md)
|
||||||
md.inlinePatterns['autolink'] = NofollowAutolinkPattern(markdown.inlinepatterns.AUTOLINK_RE, md)
|
md.inlinePatterns['autolink'] = ClickHouseAutolinkPattern(markdown.inlinepatterns.AUTOLINK_RE, md)
|
||||||
|
|
||||||
|
|
||||||
def makeExtension(**kwargs):
|
def makeExtension(**kwargs):
|
||||||
return ClickHouseMarkdown(**kwargs)
|
return ClickHouseMarkdown(**kwargs)
|
||||||
|
Loading…
Reference in New Issue
Block a user