aboutsummaryrefslogtreecommitdiff
path: root/setup.py
blob: 5f4e532b83e4b4479e8287cf119853c048417a10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
# SPDX-License-Identifier: CC0-1.0

# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
#
# Available under the terms of Creative Commons Zero v1.0 Universal.

import setuptools

from setuptools.command.build_py import build_py
from setuptools.command.sdist import sdist
from setuptools import Command

from pathlib import Path

here = Path(__file__).resolve().parent

class CustomBuildCommand(build_py):
    """The build command but runs babel before build."""
    def run(self, *args, **kwargs):
        """Wrapper around build_py's original run() method."""
        self.run_command('compile_catalog')

        super().run(*args, **kwargs)

class BuildDocCommand(Command):
    """
    Command to create an `htmldoc/` catalog with Haketilo documentation inside
    as standalone .html files.
    """
    user_options = []

    def run (self, *args, **kwargs):
        """Generate the .html files"""
        import jinja2
        import shutil
        import sys

        htmldoc_dir = here / 'htmldoc'
        if htmldoc_dir.exists():
            shutil.rmtree(htmldoc_dir)

        proxy_doc_dir = htmldoc_dir / 'haketilo'

        sys.path.insert(0, str(here / 'src'))

        from hydrilla.proxy import self_doc
        from hydrilla import common_jinja_templates

        loader = common_jinja_templates.combine_with_loaders([self_doc.loader])
        jinja_env = jinja2.Environment(
            loader        = loader,
            autoescape    = jinja2.select_autoescape(['html.jinja']),
            lstrip_blocks = True,
            extensions    = ['jinja2.ext.do']
        )

        for locale in self_doc.available_locales:
            doc_dir = proxy_doc_dir / locale
            doc_dir.mkdir(parents=True)

            for page_name in self_doc.page_names:
                file_name = f'{locale}/{page_name}.html.jinja'
                template = jinja_env.get_template(file_name)
                html_text = template.render(doc_output='html')

                out_name = 'index' if page_name == 'doc_index' else page_name

                (doc_dir / f'{out_name}.html').write_text(html_text)

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

setuptools.setup(cmdclass = {
    'build_py':      CustomBuildCommand,
    'build_htmldoc': BuildDocCommand
})