Skip to content

Commit

Permalink
refix needs look up builder, test_case
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatnamnguyengtvthcm committed Sep 18, 2023
1 parent 27bd083 commit c77162c
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 61 deletions.
2 changes: 1 addition & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Released: under development
1.4.0
-----
Released: under development
* Improvement: Added Builder :ref:`needs_lut_builder` and config option :ref:`needs_lut_build_json` in `conf.py`.
* 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`.

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def custom_defined_func():
needs_build_json_per_id = False

# build needs_lut.json to make permalinks work
needs_lut_build_json = False
needs_build_lut_json = False

# Add any paths that contain templates here, relative to this directory.
templates_path = ["_templates"]
Expand Down
7 changes: 3 additions & 4 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1897,7 +1897,6 @@ an absolute path (on the web server) or an URL.
Default value: ``needs.json``
You can choose needs_lut_build_json ``needs_lut.json`` after setting :ref:`needs_lut_build_json`
.. _needs_constraints:
Expand Down Expand Up @@ -2335,9 +2334,9 @@ Default value: True
Can be overwritten for each single need by setting :ref:`need_collapse`.
.. __needs_lut_build_json:
.. _needs_build_lut_json:
needs_lut_build_json
needs_build_lut_json
~~~~~~~~~~~~~~~~~~~~
.. versionadded:: 1.4.0
Expand All @@ -2350,7 +2349,7 @@ Example:
.. code-block:: python
needs_lut_build_json = False
needs_build_lut_json = False
.. hint::
Expand Down
18 changes: 10 additions & 8 deletions sphinx_needs/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,22 +248,24 @@ def write_doc(self, docname: str, doctree: nodes.document) -> None:
def finish(self) -> None:
env = self.env
data = SphinxNeedsData(env)
needs = data.get_or_create_needs().values() # We need a list of needs for later filter checks
needs_dict = {}
needs_config = NeedsSphinxConfig(env.config)
filter_string = needs_config.builder_filter
from sphinx_needs.filter_common import filter_needs

filtered_needs = filter_needs(self.app, needs, filter_string)
version = getattr(env.config, "version", "unset")
needs_list = NeedsList(env.config, self.outdir, self.srcdir)
needs_list.wipe_version(version)
filtered_needs: List[NeedsInfoType] = filter_needs(self.app, data.get_or_create_needs().values(), filter_string)
for need in filtered_needs:
if need["is_external"]:
needs_dict[need["id"]] = need["external_url"]
needs_dict = {"id": need["id"], "docname": need["external_url"], "content": need["content"]}

else:
needs_dict[need["id"]] = need["docname"]
needs_dict = {"id": need["id"], "docname": need["docname"], "content": need["content"]}
need["docname"] = need["external_url"]

version = getattr(env.config, "version", "unset")
needs_list = NeedsList(env.config, self.outdir, self.srcdir)
needs_list.add_lut_need(version, needs_dict)
needs_list.add_need(version, needs_dict)
try:
needs_list.write_json("needs_lut.json")
except Exception as e:
Expand All @@ -290,7 +292,7 @@ def get_target_uri(self, _docname: str, _typ: Optional[str] = None) -> str:
def build_needs_look_up_json(app: Sphinx, _exception: Exception) -> None:
env = app.env

if not NeedsSphinxConfig(env.config).lut_build_json:
if not NeedsSphinxConfig(env.config).build_lut_json:
return

# Do not create an additional look up table json, if builder is already in use.
Expand Down
2 changes: 1 addition & 1 deletion sphinx_needs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def __setattr__(self, name: str, value: Any) -> None:
# add config for needs_id_builder
build_json_per_id: bool = field(default=False, metadata={"rebuild": "html", "types": (bool,)})
build_json_per_id_path: str = field(default="needs_id", metadata={"rebuild": "html", "types": (str,)})
lut_build_json: bool = field(default=False, metadata={"rebuild": "html", "types": (bool,)})
build_lut_json: bool = field(default=False, metadata={"rebuild": "html", "types": (bool,)})

@classmethod
def add_config_values(cls, app: Sphinx) -> None:
Expand Down
60 changes: 22 additions & 38 deletions sphinx_needs/templates/permalink.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,51 +23,35 @@
function main() {
loadJSON('{{ needs_file }}', function (response) {
const needs = JSON.parse(response);
if(needs.hasOwnProperty("current_version")) {
const needs = JSON.parse(response);
const current_version = needs['current_version'];
const versions = needs['versions'];
const version = versions[current_version];
const needs_obj = version['needs'];
const current_version = needs['current_version'];
const versions = needs['versions'];
const version = versions[current_version];
const needs_obj = version['needs'];

const id = getParameterByName('id');
var pathname = new URL(window.location.href).pathname;
pathname = pathname.substring(0, pathname.lastIndexOf('{{ permalink_file }}'));
const id = getParameterByName('id');
var pathname = new URL(window.location.href).pathname;

const keys = Object.keys(needs_obj);
pathname = pathname.substring(0, pathname.lastIndexOf('{{ permalink_file }}'));

var docname = 'index';
const keys = Object.keys(needs_obj);

keys.forEach((key, index) => {
if (key === id) {
const need = needs_obj[key];
docname = need['docname'];
return;
}
});
var docname = 'index';

window.location.replace(pathname + docname + '.html#' + id);
}
else {
const needs = JSON.parse(response);
const id = getParameterByName('id');
var pathname = new URL(window.location.href).pathname;
pathname = pathname.substring(0, pathname.lastIndexOf('permalink.html'));
const keys = Object.keys(needs);
var docname = 'index';
keys.forEach((key, index) => {
if (key === id) {
docname = needs[key];
return;
}
});

if (docname.includes("#" + id)) {
window.location.replace(pathname + docname);
} else {
window.location.replace(pathname + docname + '.html#' + id);
keys.forEach((key, index) => {
if (key === id) {
const need = needs_obj[key];
docname = need['docname'];
return;
}
});

if (docname.includes("#" + id)) {
window.location.replace(pathname + docname);
} else {
window.location.replace(pathname + docname + '.html#' + id);
}


});


Expand Down
32 changes: 32 additions & 0 deletions tests/__snapshots__/test_needs_look_up.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# serializer version: 1
# name: test_doc_needs_lut_builder[test_app0]
dict({
'current_version': '1.0',
'project': 'Python',
'versions': dict({
'1.0': dict({
'filters': dict({
}),
'filters_amount': 0,
'needs': dict({
'TC_001': dict({
'description': '',
'docname': 'index',
'id': 'TC_001',
}),
'TC_NEG_001': dict({
'description': '',
'docname': 'index',
'id': 'TC_NEG_001',
}),
'US_63252': dict({
'description': '',
'docname': 'index',
'id': 'US_63252',
}),
}),
'needs_amount': 3,
}),
}),
})
# ---
14 changes: 6 additions & 8 deletions tests/test_needs_look_up.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@
from pathlib import Path

import pytest
from syrupy.filters import props


@pytest.mark.parametrize(
"test_app", [{"buildername": "needs_lut", "srcdir": "doc_test/doc_needs_builder"}], indirect=True
)
def test_doc_needs_lut_builder(test_app):
def test_doc_needs_lut_builder(test_app, snapshot):
app = test_app
app.build()

needs_json = Path(app.outdir, "needs_lut.json")
with open(needs_json) as needs_file:
needs_file_content = needs_file.read()

needs_list = json.loads(needs_file_content)
assert needs_list["TC_NEG_001"]
needs_list = json.loads(Path(app.outdir, "needs_lut.json").read_text())
print(needs_list)
print(snapshot(exclude=props("created")))
assert needs_list == snapshot(exclude=props("created"))

0 comments on commit c77162c

Please sign in to comment.