2018-07-09 19:59:07 +00:00
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
|
|
import argparse
|
2018-08-10 14:44:49 +00:00
|
|
|
|
import datetime
|
2018-07-09 19:59:07 +00:00
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
|
import shutil
|
2018-10-12 15:43:16 +00:00
|
|
|
|
import subprocess
|
2018-07-09 19:59:07 +00:00
|
|
|
|
import sys
|
2018-10-16 10:47:17 +00:00
|
|
|
|
import time
|
|
|
|
|
|
|
|
|
|
import markdown.extensions
|
|
|
|
|
import markdown.util
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
|
|
|
|
from mkdocs import config
|
|
|
|
|
from mkdocs import exceptions
|
|
|
|
|
from mkdocs.commands import build as mkdocs_build
|
|
|
|
|
|
|
|
|
|
from concatenate import concatenate
|
2019-04-08 16:01:54 +00:00
|
|
|
|
|
2019-03-26 09:50:28 +00:00
|
|
|
|
from website import build_website, minify_website
|
2018-12-12 17:28:00 +00:00
|
|
|
|
import mdx_clickhouse
|
2018-12-18 11:32:08 +00:00
|
|
|
|
import test
|
2019-04-08 16:01:54 +00:00
|
|
|
|
import util
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
|
|
|
|
|
2018-10-16 10:47:17 +00:00
|
|
|
|
class ClickHouseMarkdown(markdown.extensions.Extension):
|
|
|
|
|
class ClickHousePreprocessor(markdown.util.Processor):
|
|
|
|
|
def run(self, lines):
|
|
|
|
|
for line in lines:
|
|
|
|
|
if '<!--hide-->' not in line:
|
|
|
|
|
yield line
|
|
|
|
|
|
|
|
|
|
def extendMarkdown(self, md):
|
|
|
|
|
md.preprocessors.register(self.ClickHousePreprocessor(), 'clickhouse_preprocessor', 31)
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
|
2018-10-16 10:47:17 +00:00
|
|
|
|
markdown.extensions.ClickHouseMarkdown = ClickHouseMarkdown
|
2018-10-16 06:58:03 +00:00
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
|
2018-07-09 19:59:07 +00:00
|
|
|
|
def build_for_lang(lang, args):
|
|
|
|
|
logging.info('Building %s docs' % lang)
|
2018-12-12 17:28:00 +00:00
|
|
|
|
os.environ['SINGLE_PAGE'] = '0'
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
2018-07-18 09:22:13 +00:00
|
|
|
|
config_path = os.path.join(args.docs_dir, 'toc_%s.yml' % lang)
|
2019-12-10 08:17:26 +00:00
|
|
|
|
if args.is_stable_release and not os.path.exists(config_path):
|
|
|
|
|
logging.warn('Skipping %s docs, because %s does not exist' % (lang, config_path))
|
|
|
|
|
return
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
theme_cfg = {
|
2018-08-10 14:44:49 +00:00
|
|
|
|
'name': 'mkdocs',
|
|
|
|
|
'custom_dir': os.path.join(os.path.dirname(__file__), args.theme_dir),
|
2018-07-09 19:59:07 +00:00
|
|
|
|
'language': lang,
|
2018-09-06 10:22:06 +00:00
|
|
|
|
'direction': 'rtl' if lang == 'fa' else 'ltr',
|
2018-07-09 19:59:07 +00:00
|
|
|
|
'feature': {
|
|
|
|
|
'tabs': False
|
|
|
|
|
},
|
|
|
|
|
'palette': {
|
|
|
|
|
'primary': 'white',
|
|
|
|
|
'accent': 'white'
|
|
|
|
|
},
|
|
|
|
|
'font': False,
|
|
|
|
|
'logo': 'images/logo.svg',
|
|
|
|
|
'favicon': 'assets/images/favicon.ico',
|
|
|
|
|
'include_search_page': False,
|
|
|
|
|
'search_index_only': True,
|
|
|
|
|
'static_templates': ['404.html'],
|
|
|
|
|
'extra': {
|
2018-10-16 10:47:17 +00:00
|
|
|
|
'now': int(time.mktime(datetime.datetime.now().timetuple())) # TODO better way to avoid caching
|
2018-07-09 19:59:07 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-10 14:44:49 +00:00
|
|
|
|
|
2018-10-08 15:45:18 +00:00
|
|
|
|
site_names = {
|
2019-04-08 16:01:54 +00:00
|
|
|
|
'en': 'ClickHouse %s Documentation',
|
|
|
|
|
'ru': 'Документация ClickHouse %s',
|
|
|
|
|
'zh': 'ClickHouse文档 %s',
|
2019-12-02 09:21:34 +00:00
|
|
|
|
'ja': 'ClickHouseドキュメント %s',
|
2019-04-08 16:01:54 +00:00
|
|
|
|
'fa': 'مستندات %sClickHouse'
|
2018-10-08 15:45:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
if args.version_prefix:
|
|
|
|
|
site_dir = os.path.join(args.docs_output_dir, args.version_prefix, lang)
|
|
|
|
|
else:
|
|
|
|
|
site_dir = os.path.join(args.docs_output_dir, lang)
|
|
|
|
|
|
2018-07-09 19:59:07 +00:00
|
|
|
|
cfg = config.load_config(
|
|
|
|
|
config_file=config_path,
|
2019-04-08 16:01:54 +00:00
|
|
|
|
site_name=site_names.get(lang, site_names['en']) % args.version_prefix,
|
2018-09-06 18:24:00 +00:00
|
|
|
|
site_url='https://clickhouse.yandex/docs/%s/' % lang,
|
2018-07-09 19:59:07 +00:00
|
|
|
|
docs_dir=os.path.join(args.docs_dir, lang),
|
2019-04-08 16:01:54 +00:00
|
|
|
|
site_dir=site_dir,
|
|
|
|
|
strict=not args.version_prefix,
|
2018-07-09 19:59:07 +00:00
|
|
|
|
theme=theme_cfg,
|
2019-01-11 15:27:45 +00:00
|
|
|
|
copyright='©2016–2019 Yandex LLC',
|
2018-07-09 19:59:07 +00:00
|
|
|
|
use_directory_urls=True,
|
2019-11-08 14:26:25 +00:00
|
|
|
|
repo_name='ClickHouse/ClickHouse',
|
|
|
|
|
repo_url='https://github.com/ClickHouse/ClickHouse/',
|
2018-07-09 19:59:07 +00:00
|
|
|
|
edit_uri='edit/master/docs/%s' % lang,
|
2018-10-16 10:47:17 +00:00
|
|
|
|
extra_css=['assets/stylesheets/custom.css'],
|
2018-07-20 09:34:42 +00:00
|
|
|
|
markdown_extensions=[
|
2018-10-16 10:47:17 +00:00
|
|
|
|
'clickhouse',
|
2018-07-20 09:34:42 +00:00
|
|
|
|
'admonition',
|
|
|
|
|
'attr_list',
|
2018-09-06 10:22:06 +00:00
|
|
|
|
'codehilite',
|
2018-12-12 17:28:00 +00:00
|
|
|
|
'extra',
|
|
|
|
|
{
|
|
|
|
|
'toc': {
|
|
|
|
|
'permalink': True,
|
|
|
|
|
'slugify': mdx_clickhouse.slugify
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-10 14:44:49 +00:00
|
|
|
|
],
|
|
|
|
|
plugins=[{
|
|
|
|
|
'search': {
|
2018-09-06 18:24:00 +00:00
|
|
|
|
'lang': ['en', 'ru'] if lang == 'ru' else ['en']
|
2018-08-10 14:44:49 +00:00
|
|
|
|
}
|
|
|
|
|
}],
|
|
|
|
|
extra={
|
|
|
|
|
'search': {
|
2018-09-06 18:24:00 +00:00
|
|
|
|
'language': 'en,ru' if lang == 'ru' else 'en'
|
2019-04-08 16:01:54 +00:00
|
|
|
|
},
|
|
|
|
|
'stable_releases': args.stable_releases,
|
|
|
|
|
'version_prefix': args.version_prefix
|
2018-08-10 14:44:49 +00:00
|
|
|
|
}
|
2018-07-09 19:59:07 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
mkdocs_build.build(cfg)
|
|
|
|
|
|
|
|
|
|
if not args.skip_single_page:
|
|
|
|
|
build_single_page_version(lang, args, cfg)
|
|
|
|
|
|
|
|
|
|
except exceptions.ConfigurationError as e:
|
|
|
|
|
raise SystemExit('\n' + str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def build_single_page_version(lang, args, cfg):
|
|
|
|
|
logging.info('Building single page version for ' + lang)
|
2018-12-12 17:28:00 +00:00
|
|
|
|
os.environ['SINGLE_PAGE'] = '1'
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
with util.autoremoved_file(os.path.join(args.docs_dir, lang, 'single.md')) as single_md:
|
2018-07-09 19:59:07 +00:00
|
|
|
|
concatenate(lang, args.docs_dir, single_md)
|
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
with util.temp_dir() as site_temp:
|
|
|
|
|
with util.temp_dir() as docs_temp:
|
|
|
|
|
docs_src_lang = os.path.join(args.docs_dir, lang)
|
2018-08-10 14:44:49 +00:00
|
|
|
|
docs_temp_lang = os.path.join(docs_temp, lang)
|
2019-04-08 16:01:54 +00:00
|
|
|
|
shutil.copytree(docs_src_lang, docs_temp_lang)
|
2018-08-10 14:44:49 +00:00
|
|
|
|
for root, _, filenames in os.walk(docs_temp_lang):
|
|
|
|
|
for filename in filenames:
|
|
|
|
|
if filename != 'single.md' and filename.endswith('.md'):
|
|
|
|
|
os.unlink(os.path.join(root, filename))
|
|
|
|
|
|
|
|
|
|
cfg.load_dict({
|
|
|
|
|
'docs_dir': docs_temp_lang,
|
|
|
|
|
'site_dir': site_temp,
|
|
|
|
|
'extra': {
|
2018-10-12 13:22:08 +00:00
|
|
|
|
'single_page': True
|
2018-08-10 14:44:49 +00:00
|
|
|
|
},
|
|
|
|
|
'nav': [
|
|
|
|
|
{cfg.data.get('site_name'): 'single.md'}
|
|
|
|
|
]
|
|
|
|
|
})
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
2018-08-10 14:44:49 +00:00
|
|
|
|
mkdocs_build.build(cfg)
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
if args.version_prefix:
|
|
|
|
|
single_page_output_path = os.path.join(args.docs_dir, args.docs_output_dir, args.version_prefix, lang, 'single')
|
|
|
|
|
else:
|
|
|
|
|
single_page_output_path = os.path.join(args.docs_dir, args.docs_output_dir, lang, 'single')
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
2018-08-10 14:44:49 +00:00
|
|
|
|
if os.path.exists(single_page_output_path):
|
|
|
|
|
shutil.rmtree(single_page_output_path)
|
|
|
|
|
|
|
|
|
|
shutil.copytree(
|
|
|
|
|
os.path.join(site_temp, 'single'),
|
|
|
|
|
single_page_output_path
|
|
|
|
|
)
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
2018-12-21 19:40:49 +00:00
|
|
|
|
if not args.skip_pdf:
|
|
|
|
|
single_page_index_html = os.path.abspath(os.path.join(single_page_output_path, 'index.html'))
|
|
|
|
|
single_page_pdf = single_page_index_html.replace('index.html', 'clickhouse_%s.pdf' % lang)
|
|
|
|
|
create_pdf_command = ['wkhtmltopdf', '--print-media-type', single_page_index_html, single_page_pdf]
|
|
|
|
|
logging.debug(' '.join(create_pdf_command))
|
|
|
|
|
subprocess.check_call(' '.join(create_pdf_command), shell=True)
|
2018-10-12 15:43:16 +00:00
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
with util.temp_dir() as test_dir:
|
2018-12-18 11:32:08 +00:00
|
|
|
|
cfg.load_dict({
|
|
|
|
|
'docs_dir': docs_temp_lang,
|
|
|
|
|
'site_dir': test_dir,
|
|
|
|
|
'extra': {
|
|
|
|
|
'single_page': False
|
|
|
|
|
},
|
|
|
|
|
'nav': [
|
|
|
|
|
{cfg.data.get('site_name'): 'single.md'}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
mkdocs_build.build(cfg)
|
2019-04-08 16:01:54 +00:00
|
|
|
|
if not args.version_prefix: # maybe enable in future
|
|
|
|
|
test.test_single_page(os.path.join(test_dir, 'single', 'index.html'), lang)
|
2018-12-18 11:32:08 +00:00
|
|
|
|
if args.save_raw_single_page:
|
|
|
|
|
shutil.copytree(test_dir, args.save_raw_single_page)
|
|
|
|
|
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
|
|
|
|
def build_redirects(args):
|
|
|
|
|
lang_re_fragment = args.lang.replace(',', '|')
|
|
|
|
|
rewrites = []
|
|
|
|
|
with open(os.path.join(args.docs_dir, 'redirects.txt'), 'r') as f:
|
|
|
|
|
for line in f:
|
|
|
|
|
from_path, to_path = line.split(' ', 1)
|
|
|
|
|
from_path = '^/docs/(' + lang_re_fragment + ')/' + from_path.replace('.md', '/?') + '$'
|
|
|
|
|
to_path = '/docs/$1/' + to_path.replace('.md', '/')
|
|
|
|
|
rewrites.append(' '.join(['rewrite', from_path, to_path, 'permanent;']))
|
|
|
|
|
|
2019-03-26 09:50:28 +00:00
|
|
|
|
with open(os.path.join(args.docs_output_dir, 'redirects.conf'), 'w') as f:
|
2018-07-09 19:59:07 +00:00
|
|
|
|
f.write('\n'.join(rewrites))
|
|
|
|
|
|
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
def build_docs(args):
|
|
|
|
|
for lang in args.lang.split(','):
|
|
|
|
|
build_for_lang(lang, args)
|
|
|
|
|
|
|
|
|
|
|
2018-07-09 19:59:07 +00:00
|
|
|
|
def build(args):
|
2019-03-26 09:50:28 +00:00
|
|
|
|
if os.path.exists(args.output_dir):
|
|
|
|
|
shutil.rmtree(args.output_dir)
|
|
|
|
|
|
|
|
|
|
if not args.skip_website:
|
|
|
|
|
build_website(args)
|
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
build_docs(args)
|
|
|
|
|
|
|
|
|
|
from github import build_releases
|
|
|
|
|
build_releases(args, build_docs)
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
|
|
|
|
build_redirects(args)
|
|
|
|
|
|
2019-03-26 09:50:28 +00:00
|
|
|
|
if not args.skip_website:
|
|
|
|
|
minify_website(args)
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
|
|
2018-07-09 19:59:07 +00:00
|
|
|
|
if __name__ == '__main__':
|
2019-04-08 16:01:54 +00:00
|
|
|
|
os.chdir(os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
|
|
2018-07-09 19:59:07 +00:00
|
|
|
|
arg_parser = argparse.ArgumentParser()
|
2019-12-02 09:21:34 +00:00
|
|
|
|
arg_parser.add_argument('--lang', default='en,ru,zh,ja,fa')
|
2018-08-10 14:44:49 +00:00
|
|
|
|
arg_parser.add_argument('--docs-dir', default='.')
|
2018-07-09 19:59:07 +00:00
|
|
|
|
arg_parser.add_argument('--theme-dir', default='mkdocs-material-theme')
|
2019-03-26 09:50:28 +00:00
|
|
|
|
arg_parser.add_argument('--website-dir', default=os.path.join('..', 'website'))
|
2018-08-10 14:44:49 +00:00
|
|
|
|
arg_parser.add_argument('--output-dir', default='build')
|
2019-04-08 16:01:54 +00:00
|
|
|
|
arg_parser.add_argument('--enable-stable-releases', action='store_true')
|
|
|
|
|
arg_parser.add_argument('--version-prefix', type=str, default='')
|
2019-12-10 08:17:26 +00:00
|
|
|
|
arg_parser.add_argument('--is-stable-release', action='store_true')
|
2018-07-09 19:59:07 +00:00
|
|
|
|
arg_parser.add_argument('--skip-single-page', action='store_true')
|
2018-12-21 19:40:49 +00:00
|
|
|
|
arg_parser.add_argument('--skip-pdf', action='store_true')
|
2019-03-26 09:50:28 +00:00
|
|
|
|
arg_parser.add_argument('--skip-website', action='store_true')
|
2018-12-18 11:32:08 +00:00
|
|
|
|
arg_parser.add_argument('--save-raw-single-page', type=str)
|
2018-07-09 19:59:07 +00:00
|
|
|
|
arg_parser.add_argument('--verbose', action='store_true')
|
|
|
|
|
|
|
|
|
|
args = arg_parser.parse_args()
|
2019-04-08 16:01:54 +00:00
|
|
|
|
args.docs_output_dir = os.path.join(os.path.abspath(args.output_dir), 'docs')
|
|
|
|
|
|
|
|
|
|
from github import choose_latest_releases
|
|
|
|
|
args.stable_releases = choose_latest_releases() if args.enable_stable_releases else []
|
2018-07-09 19:59:07 +00:00
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level=logging.DEBUG if args.verbose else logging.INFO,
|
|
|
|
|
stream=sys.stderr
|
|
|
|
|
)
|
|
|
|
|
|
2018-08-10 14:44:49 +00:00
|
|
|
|
logging.getLogger('MARKDOWN').setLevel(logging.INFO)
|
|
|
|
|
|
2018-07-09 19:59:07 +00:00
|
|
|
|
from build import build
|
|
|
|
|
build(args)
|