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

Add figclass option #18

Merged
merged 6 commits into from
Oct 23, 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
5 changes: 5 additions & 0 deletions sphinxcontrib/screenshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class ScreenshotDirective(SphinxDirective):
'height': directives.positive_int,
'width': directives.positive_int,
'caption': directives.unchanged,
'figclass': directives.unchanged,
}
pool = ThreadPoolExecutor()

Expand Down Expand Up @@ -133,6 +134,7 @@ def run(self) -> typing.List[nodes.Node]:
height = self.options.get('height', 960)
width = self.options.get('width', 1280)
caption_text = self.options.get('caption', '')
figclass = self.options.get('figclass', '')
interactions = '\n'.join(self.content)

if urlparse(url).scheme not in {'http', 'https'}:
Expand All @@ -158,6 +160,9 @@ def run(self) -> typing.List[nodes.Node]:
image_node = nodes.image(uri=rel_filepath)
figure_node = nodes.figure('', image_node)

if figclass:
figure_node['classes'].append(figclass)

if caption_text:
parsed = nodes.Element()
self.state.nested_parse(
Expand Down
5 changes: 5 additions & 0 deletions tests/test-root/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@
:caption: Changing the background.

document.body.style.background = 'red';

.. screenshot:: http://www.example.com
:width: 480
:height: 320
:figclass: round
7 changes: 5 additions & 2 deletions tests/test_it.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_default(app: SphinxTestApp, status: StringIO,

# Every screenshot directive should become an image.
imgs = soup.find_all('img')
assert len(list(imgs)) == 3
assert len(list(imgs)) == 4

# The image size should be set as specified.
img_obj = Image.open(app.outdir / imgs[0]['src'])
Expand All @@ -44,11 +44,14 @@ def test_default(app: SphinxTestApp, status: StringIO,

# The images should be different after the specified user interaction.
img_before_interaction = Image.open(app.outdir / imgs[0]['src'])
img_after_interaction = Image.open(app.outdir / imgs[-1]['src'])
img_after_interaction = Image.open(app.outdir / imgs[2]['src'])
assert list(img_before_interaction.getdata()) != list(
img_after_interaction.getdata())

# The images should be the same if the difference is only the caption.
img_with_caption_a = imgs[0]
img_with_caption_b = imgs[1]
assert img_with_caption_a['src'] == img_with_caption_b['src']

# The figure node should have the class name specified.
assert 'round' in soup.find_all('figure')[-1]['class']