2017-12-29 12:43:05 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-07-09 15:03:54 +00:00
|
|
|
import logging
|
2017-12-29 12:43:05 +00:00
|
|
|
import re
|
|
|
|
import os
|
|
|
|
|
2020-04-03 13:23:32 +00:00
|
|
|
import yaml
|
2017-12-29 12:43:05 +00:00
|
|
|
|
2020-04-03 13:23:32 +00:00
|
|
|
|
|
|
|
def recursive_values(item):
|
|
|
|
if isinstance(item, dict):
|
|
|
|
for _, value in item.items():
|
|
|
|
yield from recursive_values(value)
|
|
|
|
elif isinstance(item, list):
|
|
|
|
for value in item:
|
|
|
|
yield from recursive_values(value)
|
|
|
|
elif isinstance(item, str):
|
|
|
|
yield item
|
|
|
|
|
|
|
|
|
|
|
|
def concatenate(lang, docs_path, single_page_file, nav):
|
2018-07-09 15:03:54 +00:00
|
|
|
lang_path = os.path.join(docs_path, lang)
|
2018-12-18 11:32:08 +00:00
|
|
|
az_re = re.compile(r'[a-z]')
|
2017-12-29 12:43:05 +00:00
|
|
|
|
2020-04-03 13:23:32 +00:00
|
|
|
proj_config = f'{docs_path}/toc_{lang}.yml'
|
|
|
|
if os.path.exists(proj_config):
|
|
|
|
with open(proj_config) as cfg_file:
|
|
|
|
nav = yaml.full_load(cfg_file.read())['nav']
|
|
|
|
files_to_concatenate = list(recursive_values(nav))
|
2020-03-31 13:04:53 +00:00
|
|
|
files_count = len(files_to_concatenate)
|
|
|
|
logging.info(f'{files_count} files will be concatenated into single md-file for {lang}.')
|
2018-07-11 08:17:36 +00:00
|
|
|
logging.debug('Concatenating: ' + ', '.join(files_to_concatenate))
|
2020-04-03 13:23:32 +00:00
|
|
|
assert files_count > 0, f'Empty single-page for {lang}'
|
2017-12-29 12:43:05 +00:00
|
|
|
|
2018-07-09 15:03:54 +00:00
|
|
|
for path in files_to_concatenate:
|
2019-08-12 16:23:12 +00:00
|
|
|
if path.endswith('introduction/info.md'):
|
|
|
|
continue
|
2019-04-08 16:01:54 +00:00
|
|
|
try:
|
|
|
|
with open(os.path.join(lang_path, path)) as f:
|
|
|
|
anchors = set()
|
|
|
|
tmp_path = path.replace('/index.md', '/').replace('.md', '/')
|
|
|
|
prefixes = ['', '../', '../../', '../../../']
|
|
|
|
parts = tmp_path.split('/')
|
|
|
|
anchors.add(parts[-2] + '/')
|
|
|
|
anchors.add('/'.join(parts[1:]))
|
2020-03-13 19:35:03 +00:00
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
for part in parts[0:-2] if len(parts) > 2 else parts:
|
|
|
|
for prefix in prefixes:
|
|
|
|
anchor = prefix + tmp_path
|
|
|
|
if anchor:
|
|
|
|
anchors.add(anchor)
|
|
|
|
anchors.add('../' + anchor)
|
|
|
|
anchors.add('../../' + anchor)
|
|
|
|
tmp_path = tmp_path.replace(part, '..')
|
2020-03-13 19:35:03 +00:00
|
|
|
|
2019-04-08 16:01:54 +00:00
|
|
|
for anchor in anchors:
|
|
|
|
if re.search(az_re, anchor):
|
2020-03-30 08:25:29 +00:00
|
|
|
single_page_file.write('<a name="%s"></a>' % anchor)
|
2020-03-13 19:35:03 +00:00
|
|
|
|
2020-03-30 08:25:29 +00:00
|
|
|
single_page_file.write('\n')
|
2020-03-13 19:35:03 +00:00
|
|
|
|
2020-03-30 08:25:29 +00:00
|
|
|
in_metadata = False
|
2019-04-08 16:01:54 +00:00
|
|
|
for l in f:
|
2020-03-30 08:25:29 +00:00
|
|
|
if l.startswith('---'):
|
|
|
|
in_metadata = not in_metadata
|
2019-04-08 16:01:54 +00:00
|
|
|
if l.startswith('#'):
|
|
|
|
l = '#' + l
|
2020-03-30 08:25:29 +00:00
|
|
|
if not in_metadata:
|
|
|
|
single_page_file.write(l)
|
2019-04-08 16:01:54 +00:00
|
|
|
except IOError as e:
|
|
|
|
logging.warning(str(e))
|
2017-12-29 12:43:05 +00:00
|
|
|
|
2018-07-09 15:03:54 +00:00
|
|
|
single_page_file.flush()
|