CodeCommitsIssuesPull requestsActionsInsightsSecurity
9af0c00053f6e376f96fc1dfcbe22b47ce74169b

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

docs/tools/concatenate.py

59lines · modecode

1# -*- coding: utf-8 -*-
2
3import logging
4import re
5import os
6
7
8def concatenate(lang, docs_path, single_page_file):
9 proj_config = os.path.join(docs_path, 'toc_%s.yml' % lang)
10 lang_path = os.path.join(docs_path, lang)
11 az_re = re.compile(r'[a-z]')
12
13 with open(proj_config) as cfg_file:
14 files_to_concatenate = []
15 for l in cfg_file:
16 if '.md' in l and 'single_page' not in l:
17 path = (l[l.index(':') + 1:]).strip(" '\n")
18 files_to_concatenate.append(path)
19
20 logging.info(
21 str(len(files_to_concatenate)) +
22 ' files will be concatenated into single md-file.')
23 logging.debug('Concatenating: ' + ', '.join(files_to_concatenate))
24
25 for path in files_to_concatenate:
26 if path.endswith('introduction/info.md'):
27 continue
28 try:
29 with open(os.path.join(lang_path, path)) as f:
30 anchors = set()
31 tmp_path = path.replace('/index.md', '/').replace('.md', '/')
32 prefixes = ['', '../', '../../', '../../../']
33 parts = tmp_path.split('/')
34 anchors.add(parts[-2] + '/')
35 anchors.add('/'.join(parts[1:]))
36
37 for part in parts[0:-2] if len(parts) > 2 else parts:
38 for prefix in prefixes:
39 anchor = prefix + tmp_path
40 if anchor:
41 anchors.add(anchor)
42 anchors.add('../' + anchor)
43 anchors.add('../../' + anchor)
44 tmp_path = tmp_path.replace(part, '..')
45
46 for anchor in anchors:
47 if re.search(az_re, anchor):
48 single_page_file.write('<a name="%s"></a>\n' % anchor)
49
50 single_page_file.write('\n\n')
51
52 for l in f:
53 if l.startswith('#'):
54 l = '#' + l
55 single_page_file.write(l)
56 except IOError as e:
57 logging.warning(str(e))
58
59 single_page_file.flush()