sphinxcontrib-screenshot 0.1.1__tar.gz → 0.1.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of sphinxcontrib-screenshot might be problematic. Click here for more details.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sphinxcontrib-screenshot
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: A Shpinx extension to embed webpage screenshots.
5
5
  Home-page: https://github.com/tushuhei/sphinxcontrib-screenshot/
6
6
  Author: Shuhei Iitsuka
@@ -32,7 +32,7 @@ Meta = typing.TypedDict('Meta', {
32
32
  'parallel_write_safe': bool
33
33
  })
34
34
 
35
- __version__ = '0.1.1'
35
+ __version__ = '0.1.3'
36
36
 
37
37
 
38
38
  class ScreenshotDirective(SphinxDirective):
@@ -72,6 +72,21 @@ class ScreenshotDirective(SphinxDirective):
72
72
 
73
73
  document.querySelector('button').click();
74
74
  ```
75
+
76
+ Use `figclass` option if you want to specify a class name to the image.
77
+
78
+ ```rst
79
+ .. screenshot:: http://www.example.com
80
+ :figclass: foo
81
+ ```
82
+
83
+ It also generates a PDF file when `pdf` option is given, which might be
84
+ useful when you need scalable image assets.
85
+
86
+ ```rst
87
+ .. screenshot:: http://www.example.com
88
+ :pdf:
89
+ ```
75
90
  """
76
91
 
77
92
  required_arguments = 1 # URL
@@ -80,12 +95,14 @@ class ScreenshotDirective(SphinxDirective):
80
95
  'height': directives.positive_int,
81
96
  'width': directives.positive_int,
82
97
  'caption': directives.unchanged,
98
+ 'figclass': directives.unchanged,
99
+ 'pdf': directives.flag,
83
100
  }
84
101
  pool = ThreadPoolExecutor()
85
102
 
86
103
  @staticmethod
87
104
  def take_screenshot(url: str, width: int, height: int, filepath: str,
88
- init_script: str, interactions: str):
105
+ init_script: str, interactions: str, generate_pdf: bool):
89
106
  """Takes a screenshot with Playwright's Chromium browser.
90
107
 
91
108
  Args:
@@ -98,6 +115,7 @@ class ScreenshotDirective(SphinxDirective):
98
115
  https://playwright.dev/python/docs/api/class-page#page-add-init-script
99
116
  interactions (str): JavaScript code to run before taking the screenshot
100
117
  after the page was loaded.
118
+ generate_pdf (bool): Generate a PDF file along with the screenshot.
101
119
  """
102
120
  with sync_playwright() as playwright:
103
121
  browser = playwright.chromium.launch()
@@ -118,6 +136,10 @@ class ScreenshotDirective(SphinxDirective):
118
136
  raise RuntimeError('Timeout error occured at %s in executing\n%s' %
119
137
  (url, interactions))
120
138
  page.screenshot(path=filepath)
139
+ if generate_pdf:
140
+ page.emulate_media(media='screen')
141
+ root, ext = os.path.splitext(filepath)
142
+ page.pdf(width=f'{width}px', height=f'{height}px', path=root + '.pdf')
121
143
  page.close()
122
144
  browser.close()
123
145
 
@@ -133,6 +155,8 @@ class ScreenshotDirective(SphinxDirective):
133
155
  height = self.options.get('height', 960)
134
156
  width = self.options.get('width', 1280)
135
157
  caption_text = self.options.get('caption', '')
158
+ figclass = self.options.get('figclass', '')
159
+ pdf = 'pdf' in self.options
136
160
  interactions = '\n'.join(self.content)
137
161
 
138
162
  if urlparse(url).scheme not in {'http', 'https'}:
@@ -148,7 +172,7 @@ class ScreenshotDirective(SphinxDirective):
148
172
  if not os.path.exists(filepath):
149
173
  fut = self.pool.submit(ScreenshotDirective.take_screenshot, url, width,
150
174
  height, filepath, screenshot_init_script,
151
- interactions)
175
+ interactions, pdf)
152
176
  fut.result()
153
177
 
154
178
  # Create image and figure nodes
@@ -158,6 +182,9 @@ class ScreenshotDirective(SphinxDirective):
158
182
  image_node = nodes.image(uri=rel_filepath)
159
183
  figure_node = nodes.figure('', image_node)
160
184
 
185
+ if figclass:
186
+ figure_node['classes'].append(figclass)
187
+
161
188
  if caption_text:
162
189
  parsed = nodes.Element()
163
190
  self.state.nested_parse(
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sphinxcontrib-screenshot
3
- Version: 0.1.1
3
+ Version: 0.1.3
4
4
  Summary: A Shpinx extension to embed webpage screenshots.
5
5
  Home-page: https://github.com/tushuhei/sphinxcontrib-screenshot/
6
6
  Author: Shuhei Iitsuka
@@ -12,6 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
+ import os
15
16
  from io import StringIO
16
17
 
17
18
  import pytest
@@ -29,7 +30,7 @@ def test_default(app: SphinxTestApp, status: StringIO,
29
30
 
30
31
  # Every screenshot directive should become an image.
31
32
  imgs = soup.find_all('img')
32
- assert len(list(imgs)) == 3
33
+ assert len(list(imgs)) == 5
33
34
 
34
35
  # The image size should be set as specified.
35
36
  img_obj = Image.open(app.outdir / imgs[0]['src'])
@@ -44,7 +45,7 @@ def test_default(app: SphinxTestApp, status: StringIO,
44
45
 
45
46
  # The images should be different after the specified user interaction.
46
47
  img_before_interaction = Image.open(app.outdir / imgs[0]['src'])
47
- img_after_interaction = Image.open(app.outdir / imgs[-1]['src'])
48
+ img_after_interaction = Image.open(app.outdir / imgs[2]['src'])
48
49
  assert list(img_before_interaction.getdata()) != list(
49
50
  img_after_interaction.getdata())
50
51
 
@@ -52,3 +53,18 @@ def test_default(app: SphinxTestApp, status: StringIO,
52
53
  img_with_caption_a = imgs[0]
53
54
  img_with_caption_b = imgs[1]
54
55
  assert img_with_caption_a['src'] == img_with_caption_b['src']
56
+
57
+ # The figure node should have the class name specified.
58
+ assert 'round' in soup.find_all('figure')[3]['class']
59
+
60
+ # Should generate a PDF file if specified.
61
+ img_with_pdf = imgs[4]['src']
62
+ root, ext = os.path.splitext(os.path.basename(img_with_pdf))
63
+ pdf_filepath = app.outdir / '_static' / 'screenshots' / f'{root}.pdf'
64
+ assert os.path.exists(pdf_filepath)
65
+
66
+ # Should not generate a PDF file if not specified.
67
+ img_without_pdf = imgs[2]['src']
68
+ root, ext = os.path.splitext(os.path.basename(img_without_pdf))
69
+ pdf_filepath = app.outdir / '_static' / 'screenshots' / f'{root}.pdf'
70
+ assert not os.path.exists(pdf_filepath)