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

🐛 FIX: Image layout function #1135

Merged
merged 3 commits into from
Feb 28, 2024
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
22 changes: 12 additions & 10 deletions sphinx_needs/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ def image(
"""
See https://docutils.sourceforge.io/docs/ref/rst/directives.html#images

If url starts with ``icon:`` the following string is taken is icon-name and the related icon is shown.
If **url** starts with ``icon:`` the following string is taken as icon-name and the related icon is shown.
Example: ``icon:activity`` will show:

.. image:: _static/activity.png
Expand All @@ -857,7 +857,17 @@ def image(
'<<image("icon:bell", height="20px", align="center")>>'
'<<image("field:url", height="60px", align="center")>>' # Get url from need['url']

:param url:
If **url** starts with ``:field`` the URL value is taken from the defined field of the current need
object.

.. hint:: Relative URLs

If a relative path for the URL parameter is given, it must be relative to the documentation
root folder and not relative to the current need location, for which it gets executed.

Example: ``<<image("_static/picture.png")>>``,

:param url: Relative path to the project folder or an absolute path
:param height:
:param width:
:param align:
Expand Down Expand Up @@ -927,14 +937,6 @@ def image(

url = value

if (
not is_external
and not os.path.isabs(url)
and (docname := self.need["docname"])
):
subfolder_amount = docname.count("/")
url = "../" * subfolder_amount + url

if is_external:
url_parsed = urlparse(url)
filename = os.path.basename(url_parsed.path) + ".png"
Expand Down
11 changes: 11 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from sphinx.application import Sphinx
from sphinx.testing.path import path
from sphinx.testing.util import SphinxTestApp
from sphinx.util.console import strip_colors
from syrupy.extensions.single_file import SingleFileSnapshotExtension, WriteMode
from xprocess import ProcessStarter

Expand Down Expand Up @@ -283,6 +284,16 @@ def test_app(make_app, sphinx_test_tempdir, request):
docutilsconf=builder_params.get("docutilsconf"),
parallel=builder_params.get("parallel", 0),
)
# Add the Sphinx warning as list to the app
# Somehow "app._warning" seems to be just a boolean, if the builder is "latex" or "singlehtml".
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh thats weird 🤔

# In this case we don't catch the warnings.
if builder_params.get("buildername", "html") == "html":
app.warning_list = strip_colors(
app._warning.getvalue().replace(str(app.srcdir), "srcdir")
).splitlines()
else:
app.warning_list = None

# Add the spec_pattern as an attribute to the Sphinx app object
app.spec_pattern = builder_params.get("spec_pattern", "*.cy.js")
# Add the ``test_js`` function as an attribute to the Sphinx app object
Expand Down
Binary file added tests/doc_test/doc_layout/_images/smile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 15 additions & 2 deletions tests/doc_test/doc_layout/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
]

needs_extra_options = ["author"]
needs_extra_options = ["author", "author_image"]

needs_layouts = {
"example": {
Expand All @@ -41,8 +41,21 @@
"meta": [
'**status**: <<meta("status")>>',
'**author**: <<meta("author")>>',
'**author_image**: <<meta("author_image")>>',
],
"side": ['<<image("_images/{{author}}.png", align="center")>>'],
"side": ['<<image("_images/smile.png", align="center")>>'],
},
},
"image_author": {
"grid": "simple_side_right_partial",
"layout": {
"head": ['**<<meta("title")>>**'],
"meta": [
'**status**: <<meta("status")>>',
r'<<meta("author", prefix="\*\*author\*\*: ")>>',
r'<<meta("author_image", prefix="\*\*author_image\*\*: ")>>',
],
"side": ['<<image("field:author_image", align="center")>>'],
},
},
"optional_author": {
Expand Down
12 changes: 12 additions & 0 deletions tests/doc_test/doc_layout/index.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
TEST DOCUMENT
=============

.. toctree::

subfolder_1/index
subfolder_2/index

.. spec:: title_clean_layout
:layout: clean

Expand All @@ -12,10 +17,17 @@ TEST DOCUMENT

.. spec:: title_example_layout
:layout: example
:author: some author

.. spec:: title_layout_optional_author
:layout: optional_author
:author: some author

.. spec:: title_layout_image_author
:layout: image_author
:author: some author
:author_image: _images/smile.png

.. spec:: title_layout_footer_grid
:layout: footer_grid

8 changes: 8 additions & 0 deletions tests/doc_test/doc_layout/subfolder_1/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Subfolder 1
===========

Image-layout test

.. spec:: title_layout_image_author_sub_1
:layout: image_author
:author_image: _images/smile.png
8 changes: 8 additions & 0 deletions tests/doc_test/doc_layout/subfolder_2/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Subfolder 2
===========

Image-layout test with local image file

.. spec:: title_layout_image_author_sub_2
:layout: image_author
:author_image: subfolder_2/subfolder_smile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 30 additions & 2 deletions tests/test_layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@

@pytest.mark.parametrize(
"test_app",
[{"buildername": "html", "srcdir": "doc_test/doc_layout"}],
[{"buildername": "html", "srcdir": "doc_test/doc_layout", "no_plantuml": True}],
indirect=True,
)
def test_doc_build_html(test_app):
app = test_app
app.build()

assert len(app.warning_list) == 0

html = (app.outdir / "index.html").read_text()
assert "title_clean_layout" in html
assert "title_complete_layout" in html
assert "title_focus_layout" not in html
assert "title_example_layout" in html

needs = extract_needs_from_html(html)
assert len(needs) == 6
assert len(needs) == 7

assert (
'<span class="needs_label"><strong>author</strong>: </span><span class="needs_data">some author</span>'
Expand All @@ -28,3 +31,28 @@ def test_doc_build_html(test_app):

# check simple_footer grid layout
assert "custom footer for" in html

# Check image is correctly referenced
assert (
'<img alt="_images/smile.png" class="needs_image align-center" src="_images/smile.png" />'
in html
)

# Check a "root"-image is correctly referenced in subfolders
html_subfolder_1 = (app.outdir / "subfolder_1/index.html").read_text()
assert (
'<img alt="../_images/smile.png" class="needs_image align-center" src="../_images/smile.png" />'
in html_subfolder_1
)
assert '<span class="needs_data">_images/smile.png</span>' in html_subfolder_1

# Check a "subfolder"-image is correctly referenced in subfolders
html_subfolder_2 = (app.outdir / "subfolder_2/index.html").read_text()
assert (
'<img alt="../_images/subfolder_smile.png" class="needs_image align-center" src="../_images/subfolder_smile.png" />'
in html_subfolder_2
)
assert (
'<span class="needs_data">subfolder_2/subfolder_smile.png</span>'
in html_subfolder_2
)
Loading