iredmail-doc/convert.py

133 lines
3.9 KiB
Python

#!/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 = '&nbsp;&nbsp;//&nbsp;&nbsp;<a href="./index.html">Document Index</a>'
TEMPLATE = """<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>{title}</title>
<link rel="stylesheet" type="text/css" href="./css/markdown.css" />
</head>
<body>
<div id="navigation">
<a href="https://www.iredmail.org" target="_blank">
<img alt="iRedMail web site"
src="./images/logo-iredmail.png"
style="vertical-align: middle; height: 30px;"
/>&nbsp;
<span>iRedMail</span>
</a> {link_index}
</div>
{body}
<div class="footer">
<p style="text-align: center; color: grey;">All documents are available in <a href="https://github.com/iredmail/docs/">GitHub repository</a>, and published under <a href="http://creativecommons.org/licenses/by-nd/3.0/us/" target="_blank">Creative Commons</a> license. You can <a href="https://github.com/iredmail/docs/archive/master.zip">download the latest version</a> for offline reading. If you found something wrong, please do <a href="https://www.iredmail.org/contact.html">contact us</a> to fix it.</p>
</div>
</body>
</html>
"""
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()