#!/usr/bin/env python3 from pathlib import Path import markdown # https://github.com/FND/markdown-checklist from markdown_checklist.extension import ChecklistExtension OUTPUT_DIR = 'html' CHAPTERS = ( 'overview', 'installation', 'mua', 'upgrade', 'iredmail-easy', 'migrations', 'howto', 'integrations', 'cluster', 'iredadmin', 'troubleshooting', 'faq') MD_EXTENSIONS = ['toc', 'meta', 'extra', 'footnotes', 'admonition', 'tables', 'attr_list', ChecklistExtension()] TITLE = 'iRedMail Documentations' LINK_INDEX = '  //  Document Index' TEMPLATE = """ {title} {body} """ def _get_dir_languages(path): FILE_NAME = '_lang.md' languages = {} directories = [p for p in Path(path).iterdir() if p.is_dir()] for d in directories: path_name = d / FILE_NAME if path_name.exists(): languages[d.name.lower()] = { 'path': d, 'name': path_name.read_text(encoding='utf-8').strip() } return languages def _get_content_dir(path): files = sorted([p for p in Path(path).iterdir() if not '_summary' in str(p)]) title = files.pop(-1) return files, title def _get_target_name(path): path_html = path.stem.split('-')[1].replace('.', '_') + '.html' return path_html def main(): current_dir = Path(__file__).parent output_dir = current_dir / OUTPUT_DIR languages = _get_dir_languages(current_dir) en = languages.pop('en_us') index_md = [] for chapter in CHAPTERS: path_chapter = en['path'] / chapter files, file_title = _get_content_dir(path_chapter) title_chapter = file_title.open(encoding='utf-8').readline() index_md.append(f'### {title_chapter}') index_md.append('') for f in files: name_html = _get_target_name(f) path_html = OUTPUT_DIR + '/' + name_html title = f.open(encoding='utf-8').readline() index_md.append(f'- [{title[2:]}]({name_html})') source = f.read_text(encoding='utf-8') body = markdown.markdown(source, extensions=MD_EXTENSIONS) data = dict( title = title, link_index = LINK_INDEX, body = body ) html = TEMPLATE.format(**data) Path(path_html).write_text(html, encoding='utf-8') index_md.append('') break path_html = OUTPUT_DIR + '/index.html' body = markdown.markdown('\n'.join(index_md), extensions=MD_EXTENSIONS) data = dict( title = TITLE, link_index = '', body = body ) html = TEMPLATE.format(**data) Path(path_html).write_text(html, encoding='utf-8') return if __name__ == '__main__': main()