diff --git a/docs/builders.rst b/docs/builders.rst index e284aa949..3410853a1 100644 --- a/docs/builders.rst +++ b/docs/builders.rst @@ -192,6 +192,7 @@ needs_lut The **needs_lut** (Needs Lookup Table) builder exports all found needs to a single json file, which only include list of key ``id`` and value of ``docname`` or ``external_url``. The build creates a file called **needs_lut.json** inside the given build-folder. + Usage +++++ @@ -203,32 +204,45 @@ Format ++++++ .. code-block:: python - - { - "extend_test_001": "directives/needextend", - "extend_test_002": "directives/needextend", - - "req_arch_001": "directives/needarch", - "req_arch_004": "directives/needarch", - - "spec_arch_001": "directives/needarch", - "test_arch_001": "directives/needarch", - "COMP_T_001": "directives/needarch", - "COMP_T_002": "directives/needarch", - "EX_REQ_1": "examples/index", - "EX_REQ_2": "examples/index", - - "R_F4722": "examples/index", - "OWN_ID_123": "examples/index", - "IMPL_01": "examples/index", - "T_C3893": "examples/index", - "R_2A9D0": "filter", - "R_22EB2": "filter", - "S_D70B0": "filter", - "S_01A67": "filter", - "T_5CCAA": "filter", - "R_17EB4": "filter", - "req_flow_001": "directives/needflow", - "spec_flow_001": "directives/needflow", + { + "created": "2023-09-25T17:11:03.162346", + "current_version": "1.3", + "project": "Sphinx-Needs", + "versions": { + "1.3": { + "created": "2023-09-25T17:11:03.162329", + "filters": {}, + "filters_amount": 0, + "needs": { + "extend_test_001": "directives/needextend", + "extend_test_002": "directives/needextend", + + "req_arch_001": "directives/needarch", + "req_arch_004": "directives/needarch", + + "spec_arch_001": "directives/needarch", + "test_arch_001": "directives/needarch", + "COMP_T_001": "directives/needarch", + "COMP_T_002": "directives/needarch", + + "EX_REQ_1": "examples/index", + "EX_REQ_2": "examples/index", + + "R_F4722": "examples/index", + "OWN_ID_123": "examples/index", + "IMPL_01": "examples/index", + "T_C3893": "examples/index", + "R_2A9D0": "filter", + "R_22EB2": "filter", + "S_D70B0": "filter", + "S_01A67": "filter", + "T_5CCAA": "filter", + "R_17EB4": "filter", + "req_flow_001": "directives/needflow", + "spec_flow_001": "directives/needflow", + } + } + } + } \ No newline at end of file diff --git a/docs/changelog.rst b/docs/changelog.rst index 7dfdd90aa..526d8b113 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -16,7 +16,6 @@ Released: under development ----- Released: under development * Improvement: Added Builder :ref:`needs_lut_builder` and config option :ref:`needs_build_lut_json` in `conf.py`. - * Improvement: Added Builder :ref:`needs_id_builder` added and config option :ref:`needs_build_json_per_id` in `conf.py`. * Improvement: Reduce document build time, by memoizing the inline parse in ``build_need`` (`#968 `_) @@ -25,6 +24,7 @@ Released: under development * Improvement: Suffix all warnings with ``[needs]``, and allow them to be suppressed (`#975 `_) * Improvement: :ref:`needextend` for single needs is much faster. +* Improvement: external_needs is using cached templates to save generation time. 1.3.0 ----- diff --git a/sphinx_needs/external_needs.py b/sphinx_needs/external_needs.py index 2c3af8c50..f46f7bd1f 100644 --- a/sphinx_needs/external_needs.py +++ b/sphinx_needs/external_needs.py @@ -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 @@ -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: @@ -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: diff --git a/sphinx_needs/templates/permalink.html b/sphinx_needs/templates/permalink.html index 8f324d6b2..b329e16cf 100644 --- a/sphinx_needs/templates/permalink.html +++ b/sphinx_needs/templates/permalink.html @@ -29,7 +29,7 @@ const needs_obj = version['needs']; const id = getParameterByName('id'); - var pathname = new URL(window.location.href).pathname; + var pathname = new URL(window.location.href).pathname; pathname = pathname.substring(0, pathname.lastIndexOf('{{ permalink_file }}'));