aboutsummaryrefslogtreecommitdiff
#!/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
})