Refactor docs print/pdf (#9934)

This commit is contained in:
Ivan Blinkov 2020-03-30 14:39:26 +03:00 committed by GitHub
parent 9436a136b6
commit 700c27a99d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 28 deletions

View File

@ -4,11 +4,14 @@ from __future__ import unicode_literals
import argparse
import datetime
import http.server
import logging
import os
import shutil
import socketserver
import subprocess
import sys
import threading
import time
import jinja2
@ -21,11 +24,10 @@ from mkdocs.commands import build as mkdocs_build
from concatenate import concatenate
from website import build_website, minify_website
import mdx_clickhouse
import test
import util
import website
class ClickHouseMarkdown(markdown.extensions.Extension):
@ -214,14 +216,10 @@ def build_single_page_version(lang, args, cfg):
)
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', f'clickhouse_{lang}.pdf')
create_pdf_command = ['wkhtmltopdf', '--print-media-type', '--log-level', 'warn', single_page_index_html, single_page_pdf]
logging.info(' '.join(create_pdf_command))
subprocess.check_call(' '.join(create_pdf_command), shell=True)
if not args.version_prefix: # maybe enable in future
with util.temp_dir() as test_dir:
single_page_pdf = os.path.abspath(
os.path.join(single_page_output_path, f'clickhouse_{lang}.pdf')
)
extra['single_page'] = False
cfg.load_dict({
'docs_dir': docs_temp_lang,
@ -232,11 +230,37 @@ def build_single_page_version(lang, args, cfg):
]
})
mkdocs_build.build(cfg)
if args.save_raw_single_page:
shutil.copytree(test_dir, args.save_raw_single_page)
test.test_single_page(os.path.join(test_dir, 'single', 'index.html'), lang)
logging.info(f'Finished building single page version for {lang}')
css_in = ' '.join(website.get_css_in(args))
js_in = ' '.join(website.get_js_in(args))
subprocess.check_call(f'cat {css_in} > {test_dir}/css/base.css', shell=True)
subprocess.check_call(f'cat {js_in} > {test_dir}/js/base.js', shell=True)
port_for_pdf = util.get_free_port()
with socketserver.TCPServer(
('', port_for_pdf), http.server.SimpleHTTPRequestHandler
) as httpd:
logging.info(f"serving for pdf at port {port_for_pdf}")
thread = threading.Thread(target=httpd.serve_forever)
with util.cd(test_dir):
thread.start()
create_pdf_command = [
'wkhtmltopdf',
'--print-media-type',
'--no-stop-slow-scripts',
'--log-level', 'warn',
f'http://localhost:{port_for_pdf}/single/', single_page_pdf
]
if args.save_raw_single_page:
shutil.copytree(test_dir, args.save_raw_single_page)
logging.info(' '.join(create_pdf_command))
subprocess.check_call(' '.join(create_pdf_command), shell=True)
httpd.shutdown()
if not args.version_prefix: # maybe enable in future
test.test_single_page(
os.path.join(test_dir, 'single', 'index.html'), lang)
logging.info(f'Finished building single page version for {lang}')
def write_redirect_html(out_path, to_url):
@ -293,7 +317,7 @@ def build(args):
shutil.rmtree(args.output_dir)
if not args.skip_website:
build_website(args)
website.build_website(args)
build_docs(args)
@ -301,7 +325,7 @@ def build(args):
build_releases(args, build_docs)
if not args.skip_website:
minify_website(args)
website.minify_website(args)
for static_redirect in [
('tutorial.html', '/docs/en/getting_started/tutorial/',),

View File

@ -3,6 +3,7 @@ import multiprocessing
import os
import shutil
import sys
import socket
import tempfile
import threading
@ -25,6 +26,23 @@ def autoremoved_file(path):
os.unlink(path)
@contextlib.contextmanager
def cd(new_cwd):
old_cwd = os.getcwd()
os.chdir(new_cwd)
try:
yield
finally:
os.chdir(old_cwd)
def get_free_port():
with contextlib.closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s:
s.bind(('', 0))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return s.getsockname()[1]
def run_function_in_parallel(func, args_list, threads=False):
processes = []
exit_code = 0

View File

@ -79,11 +79,30 @@ def build_website(args):
f.write(content.encode('utf-8'))
def minify_website(args):
css_in = f"'{args.website_dir}/css/bootstrap.css' " \
f"'{args.website_dir}/css/docsearch.css' " \
f"'{args.website_dir}/css/base.css' '{args.website_dir}/css/docs.css' " \
def get_css_in(args):
return [
f"'{args.website_dir}/css/bootstrap.css'",
f"'{args.website_dir}/css/docsearch.css'",
f"'{args.website_dir}/css/base.css'",
f"'{args.website_dir}/css/docs.css'",
f"'{args.website_dir}/css/highlight.css'"
]
def get_js_in(args):
return [
f"'{args.website_dir}/js/jquery-3.4.1.js'",
f"'{args.website_dir}/js/popper.js'",
f"'{args.website_dir}/js/bootstrap.js'",
f"'{args.website_dir}/js/base.js'",
f"'{args.website_dir}/js/index.js'",
f"'{args.website_dir}/js/docsearch.js'",
f"'{args.website_dir}/js/docs.js'"
]
def minify_website(args):
css_in = ' '.join(get_css_in(args))
css_out = f'{args.output_dir}/css/base.css'
if args.minify:
command = f"purifycss -w '*algolia*' --min {css_in} '{args.output_dir}/*.html' " \
@ -96,15 +115,7 @@ def minify_website(args):
with open(css_out, 'rb') as f:
css_digest = hashlib.sha3_224(f.read()).hexdigest()[0:8]
js_in = [
f"'{args.website_dir}/js/jquery-3.4.1.js'",
f"'{args.website_dir}/js/popper.js'",
f"'{args.website_dir}/js/bootstrap.js'",
f"'{args.website_dir}/js/base.js'",
f"'{args.website_dir}/js/index.js'",
f"'{args.website_dir}/js/docsearch.js'",
f"'{args.website_dir}/js/docs.js'"
]
js_in = get_js_in(args)
js_out = f'{args.output_dir}/js/base.js'
if args.minify:
import closure

View File

@ -141,6 +141,9 @@
}
@media print {
body {
min-width: 0!important;
}
#content-wrapper {
-ms-flex: 0 0 100%;
flex: 0 0 100%;