Example #1
0
class AutosummaryRenderer:
    """A helper class for rendering."""
    def __init__(self, app: Sphinx) -> None:
        if isinstance(app, Builder):
            raise ValueError('Expected a Sphinx application object!')

        system_templates_path = [
            os.path.join(package_dir, 'ext', 'autosummary', 'templates')
        ]
        loader = SphinxTemplateLoader(app.srcdir, app.config.templates_path,
                                      system_templates_path)

        self.env = SandboxedEnvironment(loader=loader)
        self.env.filters['escape'] = rst.escape
        self.env.filters['e'] = rst.escape
        self.env.filters['underline'] = _underline

        if app.translator:
            self.env.add_extension("jinja2.ext.i18n")
            self.env.install_gettext_translations(app.translator)

    def render(self, template_name: str, context: Dict) -> str:
        """Render a template file."""
        try:
            template = self.env.get_template(template_name)
        except TemplateNotFound:
            try:
                # objtype is given as template_name
                template = self.env.get_template('autosummary/%s.rst' %
                                                 template_name)
            except TemplateNotFound:
                # fallback to base.rst
                template = self.env.get_template('autosummary/base.rst')

        return template.render(context)
Example #2
0
class AutosummaryRenderer:
    """A helper class for rendering."""

    def __init__(self, app: Union[Builder, Sphinx], template_dir: str = None) -> None:
        if isinstance(app, Builder):
            warnings.warn('The first argument for AutosummaryRenderer has been '
                          'changed to Sphinx object',
                          RemovedInSphinx50Warning, stacklevel=2)
        if template_dir:
            warnings.warn('template_dir argument for AutosummaryRenderer is deprecated.',
                          RemovedInSphinx50Warning)

        system_templates_path = [os.path.join(package_dir, 'ext', 'autosummary', 'templates')]
        loader = SphinxTemplateLoader(app.srcdir, app.config.templates_path,
                                      system_templates_path)

        self.env = SandboxedEnvironment(loader=loader)
        self.env.filters['escape'] = rst.escape
        self.env.filters['e'] = rst.escape
        self.env.filters['underline'] = _underline

        if isinstance(app, (Sphinx, DummyApplication)):
            if app.translator:
                self.env.add_extension("jinja2.ext.i18n")
                self.env.install_gettext_translations(app.translator)  # type: ignore
        elif isinstance(app, Builder):
            if app.app.translator:
                self.env.add_extension("jinja2.ext.i18n")
                self.env.install_gettext_translations(app.app.translator)  # type: ignore

    def exists(self, template_name: str) -> bool:
        """Check if template file exists."""
        warnings.warn('AutosummaryRenderer.exists() is deprecated.',
                      RemovedInSphinx50Warning, stacklevel=2)
        try:
            self.env.get_template(template_name)
            return True
        except TemplateNotFound:
            return False

    def render(self, objtype: str, context: Dict) -> str:
        """Render a template file."""
        if objtype.endswith('.rst'):
            # old styled: template_name is given
            warnings.warn('AutosummaryRenderer.render() takes an object type as an argument.',
                          RemovedInSphinx50Warning, stacklevel=2)
            return self.env.get_template(objtype).render(context)
        else:
            # objtype is given
            try:
                template = self.env.get_template('autosummary/%s.rst' % objtype)
            except TemplateNotFound:
                # fallback to base.rst
                template = self.env.get_template('autosummary/base.rst')

            return template.render(context)
Example #3
0
class AutosummaryRenderer:
    """A helper class for rendering."""
    def __init__(self, builder: Builder, template_dir: str) -> None:
        loader = None  # type: BaseLoader
        template_dirs = [
            os.path.join(package_dir, 'ext', 'autosummary', 'templates')
        ]
        if builder is None:
            if template_dir:
                template_dirs.insert(0, template_dir)
            loader = FileSystemLoader(template_dirs)
        else:
            # allow the user to override the templates
            loader = BuiltinTemplateLoader()
            loader.init(builder, dirs=template_dirs)

        self.env = SandboxedEnvironment(loader=loader)
        self.env.filters['escape'] = rst.escape
        self.env.filters['e'] = rst.escape
        self.env.filters['underline'] = _underline

        if builder:
            if builder.app.translator:
                self.env.add_extension("jinja2.ext.i18n")
                self.env.install_gettext_translations(
                    builder.app.translator)  # type: ignore

    def exists(self, template_name: str) -> bool:
        """Check if template file exists."""
        try:
            self.env.get_template(template_name)
            return True
        except TemplateNotFound:
            return False

    def render(self, template_name: str, context: Dict) -> str:
        """Render a template file."""
        return self.env.get_template(template_name).render(context)