Skip to content

Commit

Permalink
👌 From string fix (#1031)
Browse files Browse the repository at this point in the history
  • Loading branch information
danwos committed Sep 22, 2023
1 parent 5213b15 commit 0954a50
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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

0 comments on commit 0954a50

Please sign in to comment.