Generate anchors for single-page mode automatically

This commit is contained in:
Ivan Blinkov 2018-12-12 15:54:47 +03:00
parent 4e4d5477ca
commit a433f1fd25
3 changed files with 31 additions and 33 deletions

View File

@ -54,6 +54,7 @@ markdown.extensions.ClickHouseMarkdown = ClickHouseMarkdown
def build_for_lang(lang, args):
logging.info('Building %s docs' % lang)
os.environ['SINGLE_PAGE'] = '0'
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):
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:
concatenate(lang, args.docs_dir, single_md)

View File

@ -37,34 +37,21 @@ def concatenate(lang, docs_path, single_page_file):
first_file = True
for path in files_to_concatenate:
single_page_file.write('\n\n')
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
def link_proc(matchObj):
text, link = matchObj.group().strip('[)').split('](', 1)
if link.startswith('http:') or link.startswith('https:') or '.jpeg' in link or '.jpg' in link or '.png' in link or '.gif' in link:
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 part in parts[0:-2]:
for prefix in prefixes:
single_page_file.write('<a name="%s"></a>\n' % (prefix + tmp_path))
tmp_path = tmp_path.replace(part, '..')
for l in f:
# Processing links in a string
l = re.sub(r'\[.+?\]\(.+?\)', link_proc, l)
# Correcting headers levels
if not first_file:
if l.startswith('#'):
l = '#' + l
else:
first_file = False
if l.startswith('#'):
l = '#' + l
single_page_file.write(l)
single_page_file.flush()

View File

@ -2,31 +2,41 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os
import markdown.inlinepatterns
import markdown.extensions
import markdown.util
import slugify as slugify_impl
class NofollowMixin(object):
class ClickHouseLinkMixin(object):
def handleMatch(self, m):
single_page = (os.environ.get('SINGLE_PAGE') == '1')
try:
el = super(NofollowMixin, self).handleMatch(m)
el = super(ClickHouseLinkMixin, 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'):
is_external = href.startswith('http:') or href.startswith('https:')
if is_external and not href.startswith('https://clickhouse.yandex'):
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
class NofollowAutolinkPattern(NofollowMixin, markdown.inlinepatterns.AutolinkPattern):
class ClickHouseAutolinkPattern(ClickHouseLinkMixin, markdown.inlinepatterns.AutolinkPattern):
pass
class NofollowLinkPattern(NofollowMixin, markdown.inlinepatterns.LinkPattern):
class ClickHouseLinkPattern(ClickHouseLinkMixin, markdown.inlinepatterns.LinkPattern):
pass
@ -41,9 +51,8 @@ class ClickHouseMarkdown(markdown.extensions.Extension):
def extendMarkdown(self, md, md_globals):
md.preprocessors['clickhouse'] = ClickHousePreprocessor()
md.inlinePatterns['link'] = NofollowLinkPattern(markdown.inlinepatterns.LINK_RE, md)
md.inlinePatterns['autolink'] = NofollowAutolinkPattern(markdown.inlinepatterns.AUTOLINK_RE, md)
md.inlinePatterns['link'] = ClickHouseLinkPattern(markdown.inlinepatterns.LINK_RE, md)
md.inlinePatterns['autolink'] = ClickHouseAutolinkPattern(markdown.inlinepatterns.AUTOLINK_RE, md)
def makeExtension(**kwargs):
return ClickHouseMarkdown(**kwargs)