+CURRENT_DIR = Path(__file__).parent
+
+
+def get_version():
+ black_py = CURRENT_DIR / '..' / 'black.py'
+ _version_re = re.compile(r'__version__\s+=\s+(?P<version>.*)')
+ with open(str(black_py), 'r', encoding='utf8') as f:
+ version = _version_re.search(f.read()).group('version')
+ return str(ast.literal_eval(version))
+
+
+def make_pypi_svg(version):
+ template = CURRENT_DIR / '_static' / 'pypi_template.svg'
+ target = CURRENT_DIR / '_static' / 'pypi.svg'
+ with open(str(template), 'r', encoding='utf8') as f:
+ svg = string.Template(f.read()).substitute(version=version)
+ with open(str(target), 'w', encoding='utf8') as f:
+ f.write(svg)
+
+
+def make_filename(line):
+ non_letters = re.compile(r'[^a-z]+')
+ filename = line[3:].rstrip().lower()
+ filename = non_letters.sub('_', filename)
+ if filename.startswith('_'):
+ filename = filename[1:]
+ if filename.endswith('_'):
+ filename = filename[:-1]
+ return filename + '.md'
+
+
+def generate_sections_from_readme():
+ target_dir = CURRENT_DIR / '_build' / 'generated'
+ readme = CURRENT_DIR / '..' / 'README.md'
+ shutil.rmtree(str(target_dir), ignore_errors=True)
+ target_dir.mkdir(parents=True)
+
+ output = None
+ with open(str(readme), 'r', encoding='utf8') as f:
+ for line in f:
+ if line.startswith('## '):
+ if output is not None:
+ output.close()
+ filename = make_filename(line)
+ output_path = CURRENT_DIR / filename
+ if output_path.is_symlink() or output_path.is_file():
+ output_path.unlink()
+ output_path.symlink_to(target_dir / filename)
+ output = open(str(output_path), 'w', encoding='utf8')
+ output.write(
+ '[//]: # (NOTE: THIS FILE IS AUTOGENERATED FROM README.md)\n\n'
+ )
+
+ if output is None:
+ continue
+
+ if line.startswith('##'):
+ line = line[1:]
+
+ output.write(line)
+