Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👌 From string fix #1031

Merged
merged 2 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Released: under development
* Improvement: Suffix all warnings with ``[needs]``, and allow them to be suppressed
(`#975 <https://github.com/useblocks/sphinx-needs/pull/975>`_)
* Improvement: :ref:`needextend` for single needs is much faster.
* Improvement: external_needs is using cached templates to save generation time.

1.3.0
-----
Expand Down
15 changes: 13 additions & 2 deletions sphinx_needs/external_needs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import os
from functools import lru_cache

import requests
from jinja2 import Environment
from jinja2 import Environment, Template
from requests_file import FileAdapter
from sphinx.application import Sphinx
from sphinx.environment import BuildEnvironment
Expand All @@ -17,6 +18,16 @@
log = get_logger(__name__)


@lru_cache(maxsize=20)
def get_target_template(target_url: str) -> Template:
"""
Provides template for target_link style
Can be cached, as the template is always the same for a given target_url
"""
mem_template = Environment().from_string(target_url)
return mem_template


def load_external_needs(app: Sphinx, env: BuildEnvironment, _docname: str) -> None:
needs_config = NeedsSphinxConfig(app.config)
for source in needs_config.external_needs:
Expand Down Expand Up @@ -95,7 +106,7 @@ def load_external_needs(app: Sphinx, env: BuildEnvironment, _docname: str) -> No

if target_url:
# render jinja content
mem_template = Environment().from_string(target_url)
mem_template = get_target_template(target_url)
cal_target_url = mem_template.render(**{"need": need})
need_params["external_url"] = f'{source["base_url"]}/{cal_target_url}'
else:
Expand Down