sphinxcontrib-screenshot 0.1.4__py3-none-any.whl → 0.2.0__py3-none-any.whl

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.

@@ -24,7 +24,7 @@ from urllib.parse import urlparse
24
24
 
25
25
  from docutils import nodes
26
26
  from docutils.parsers.rst import directives
27
- from docutils.statemachine import ViewList
27
+ from docutils.parsers.rst.directives.images import Figure
28
28
  from playwright._impl._helper import ColorScheme
29
29
  from playwright.sync_api import Browser, BrowserContext
30
30
  from playwright.sync_api import TimeoutError as PlaywrightTimeoutError
@@ -41,7 +41,7 @@ Meta = typing.TypedDict('Meta', {
41
41
  })
42
42
 
43
43
 
44
- class ScreenshotDirective(SphinxDirective):
44
+ class ScreenshotDirective(SphinxDirective, Figure):
45
45
  """Sphinx Screenshot Dirctive.
46
46
 
47
47
  This directive embeds a screenshot of a webpage.
@@ -54,20 +54,13 @@ class ScreenshotDirective(SphinxDirective):
54
54
  .. screenshot:: http://www.example.com
55
55
  ```
56
56
 
57
- You can also specify the screen size for the screenshot with `width` and
58
- `height` parameters in pixel.
57
+ You can also specify the screen size for the screenshot with
58
+ `viewport-width` and `viewport-height` parameters in pixel.
59
59
 
60
60
  ```rst
61
61
  .. screenshot:: http://www.example.com
62
- :width: 1280
63
- :height: 960
64
- ```
65
-
66
- You can include a caption for the screenshot's `figure` directive.
67
-
68
- ```rst
69
- .. screenshot:: http://www.example.com
70
- :caption: This is a screenshot for www.example.com
62
+ :viewport-width: 1280
63
+ :viewport-height: 960
71
64
  ```
72
65
 
73
66
  You can describe the interaction that you want to have with the webpage
@@ -79,13 +72,6 @@ class ScreenshotDirective(SphinxDirective):
79
72
  document.querySelector('button').click();
80
73
  ```
81
74
 
82
- Use `figclass` option if you want to specify a class name to the image.
83
-
84
- ```rst
85
- .. screenshot:: http://www.example.com
86
- :figclass: foo
87
- ```
88
-
89
75
  It also generates a PDF file when `pdf` option is given, which might be
90
76
  useful when you need scalable image assets.
91
77
 
@@ -96,13 +82,12 @@ class ScreenshotDirective(SphinxDirective):
96
82
  """
97
83
 
98
84
  required_arguments = 1 # URL
99
- has_content = True
100
85
  option_spec = {
86
+ **Figure.option_spec,
101
87
  'browser': str,
102
- 'height': directives.positive_int,
103
- 'width': directives.positive_int,
104
- 'caption': directives.unchanged,
105
- 'figclass': directives.unchanged,
88
+ 'viewport-height': directives.positive_int,
89
+ 'viewport-width': directives.positive_int,
90
+ 'interactions': str,
106
91
  'pdf': directives.flag,
107
92
  'color-scheme': str,
108
93
  'full-page': directives.flag,
@@ -113,8 +98,8 @@ class ScreenshotDirective(SphinxDirective):
113
98
 
114
99
  @staticmethod
115
100
  def take_screenshot(
116
- url: str, browser_name: str, width: int, height: int, filepath: str,
117
- init_script: str, interactions: str, generate_pdf: bool,
101
+ url: str, browser_name: str, viewport_width: int, viewport_height: int,
102
+ filepath: str, init_script: str, interactions: str, generate_pdf: bool,
118
103
  color_scheme: ColorScheme, full_page: bool,
119
104
  context_builder: typing.Optional[typing.Callable[[Browser, str, str],
120
105
  BrowserContext]],
@@ -123,8 +108,8 @@ class ScreenshotDirective(SphinxDirective):
123
108
 
124
109
  Args:
125
110
  url (str): The HTTP/HTTPS URL of the webpage to screenshot.
126
- width (int): The width of the screenshot in pixels.
127
- height (int): The height of the screenshot in pixels.
111
+ viewport_width (int): The width of the screenshot in pixels.
112
+ viewport_height (int): The height of the screenshot in pixels.
128
113
  filepath (str): The path to save the screenshot to.
129
114
  init_script (str): JavaScript code to be evaluated after the document
130
115
  was created but before any of its scripts were run. See more details at
@@ -150,7 +135,10 @@ class ScreenshotDirective(SphinxDirective):
150
135
 
151
136
  page = context.new_page()
152
137
  page.set_default_timeout(10000)
153
- page.set_viewport_size({'width': width, 'height': height})
138
+ page.set_viewport_size({
139
+ 'width': viewport_width,
140
+ 'height': viewport_height
141
+ })
154
142
 
155
143
  try:
156
144
  if init_script:
@@ -170,7 +158,10 @@ class ScreenshotDirective(SphinxDirective):
170
158
  if generate_pdf:
171
159
  page.emulate_media(media='screen')
172
160
  root, ext = os.path.splitext(filepath)
173
- page.pdf(width=f'{width}px', height=f'{height}px', path=root + '.pdf')
161
+ page.pdf(
162
+ width=f'{viewport_width}px',
163
+ height=f'{viewport_height}px',
164
+ path=root + '.pdf')
174
165
  page.close()
175
166
  browser.close()
176
167
 
@@ -180,7 +171,7 @@ class ScreenshotDirective(SphinxDirective):
180
171
  text = text.replace(f"|{key}|", value.astext())
181
172
  return text
182
173
 
183
- def run(self) -> typing.List[nodes.Node]:
174
+ def run(self) -> typing.Sequence[nodes.Node]:
184
175
  screenshot_init_script: str = self.env.config.screenshot_init_script or ''
185
176
 
186
177
  # Ensure the screenshots directory exists
@@ -190,20 +181,19 @@ class ScreenshotDirective(SphinxDirective):
190
181
  # Parse parameters
191
182
  raw_url = self.arguments[0]
192
183
  url = self.evaluate_substitutions(raw_url)
184
+ interactions = self.options.get('interactions', '')
193
185
  browser = self.options.get('browser',
194
186
  self.env.config.screenshot_default_browser)
195
- height = self.options.get('height',
196
- self.env.config.screenshot_default_height)
197
- width = self.options.get('width', self.env.config.screenshot_default_width)
187
+ viewport_height = self.options.get(
188
+ 'viewport-height', self.env.config.screenshot_default_viewport_height)
189
+ viewport_width = self.options.get(
190
+ 'viewport-width', self.env.config.screenshot_default_viewport_width)
198
191
  color_scheme = self.options.get(
199
192
  'color-scheme', self.env.config.screenshot_default_color_scheme)
200
- caption_text = self.options.get('caption', '')
201
- figclass = self.options.get('figclass', '')
202
193
  pdf = 'pdf' in self.options
203
194
  full_page = ('full-page' in self.options or
204
195
  self.env.config.screenshot_default_full_page)
205
196
  context = self.options.get('context', '')
206
- interactions = '\n'.join(self.content)
207
197
  headers = self.options.get('headers', '')
208
198
 
209
199
  request_headers = {**self.env.config.screenshot_default_headers}
@@ -219,8 +209,8 @@ class ScreenshotDirective(SphinxDirective):
219
209
  # Generate filename based on hash of parameters
220
210
  hash_input = "_".join([
221
211
  raw_url, browser,
222
- str(height),
223
- str(width), color_scheme, context, interactions,
212
+ str(viewport_height),
213
+ str(viewport_width), color_scheme, context, interactions,
224
214
  str(full_page)
225
215
  ])
226
216
  filename = hashlib.md5(hash_input.encode()).hexdigest() + '.png'
@@ -235,29 +225,19 @@ class ScreenshotDirective(SphinxDirective):
235
225
  # Check if the file already exists. If not, take a screenshot
236
226
  if not os.path.exists(filepath):
237
227
  fut = self.pool.submit(ScreenshotDirective.take_screenshot, url, browser,
238
- width, height, filepath, screenshot_init_script,
239
- interactions, pdf, color_scheme, full_page,
240
- context_builder, request_headers)
228
+ viewport_width, viewport_height, filepath,
229
+ screenshot_init_script, interactions, pdf,
230
+ color_scheme, full_page, context_builder,
231
+ request_headers)
241
232
  fut.result()
242
233
 
243
234
  # Create image and figure nodes
244
235
  docdir = os.path.dirname(self.env.doc2path(self.env.docname))
245
236
  rel_ss_dirpath = os.path.relpath(ss_dirpath, start=docdir)
246
237
  rel_filepath = os.path.join(rel_ss_dirpath, filename).replace(os.sep, '/')
247
- image_node = nodes.image(uri=rel_filepath)
248
- figure_node = nodes.figure('', image_node)
249
-
250
- if figclass:
251
- figure_node['classes'].append(figclass)
252
-
253
- if caption_text:
254
- parsed = nodes.Element()
255
- self.state.nested_parse(
256
- ViewList([caption_text], source=''), self.content_offset, parsed)
257
- figure_node += nodes.caption(parsed[0].source or '', '',
258
- *parsed[0].children)
259
238
 
260
- return [figure_node]
239
+ self.arguments[0] = rel_filepath
240
+ return super().run()
261
241
 
262
242
 
263
243
  app_threads = {}
@@ -298,12 +278,12 @@ def setup(app: Sphinx) -> Meta:
298
278
  app.add_directive('screenshot', ScreenshotDirective)
299
279
  app.add_config_value('screenshot_init_script', '', 'env')
300
280
  app.add_config_value(
301
- 'screenshot_default_width',
281
+ 'screenshot_default_viewport_width',
302
282
  1280,
303
283
  'env',
304
284
  description="The default width for screenshots")
305
285
  app.add_config_value(
306
- 'screenshot_default_height',
286
+ 'screenshot_default_viewport_height',
307
287
  960,
308
288
  'env',
309
289
  description="The default height for screenshots")
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.2
2
2
  Name: sphinxcontrib-screenshot
3
- Version: 0.1.4
3
+ Version: 0.2.0
4
4
  Summary: A Sphinx extension to embed webpage screenshots.
5
5
  Author-email: Shuhei Iitsuka <tushuhei@gmail.com>
6
6
  License: Apache-2.0
@@ -53,8 +53,8 @@ A Sphinx extension to embed website screenshots.
53
53
  ```rst
54
54
  .. screenshot:: http://www.example.com
55
55
  :browser: chromium
56
- :width: 1280
57
- :height: 960
56
+ :viewport-width: 1280
57
+ :viewport-height: 960
58
58
  :color-scheme: dark
59
59
  ```
60
60
 
@@ -0,0 +1,6 @@
1
+ sphinxcontrib/screenshot.py,sha256=03cVsF1d9zuWMsVBwtZU_EQ_GMMsYyPa2a3vBmzC4f8,11342
2
+ sphinxcontrib_screenshot-0.2.0.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
3
+ sphinxcontrib_screenshot-0.2.0.dist-info/METADATA,sha256=pwaSBQlzvflzW2BSv7ByJ8GvbwDrYF0xn8bftw3dPC0,2868
4
+ sphinxcontrib_screenshot-0.2.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
5
+ sphinxcontrib_screenshot-0.2.0.dist-info/top_level.txt,sha256=VJrV3_vaiKQVgVpR0I1iecxoO0drzGu-M0j40PVP2QQ,14
6
+ sphinxcontrib_screenshot-0.2.0.dist-info/RECORD,,
@@ -1,6 +0,0 @@
1
- sphinxcontrib/screenshot.py,sha256=eJEF1qfGGo1uwyTovYHXx4Ewz2fXmrxDSsQSdLJ0O-0,11863
2
- sphinxcontrib_screenshot-0.1.4.dist-info/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
3
- sphinxcontrib_screenshot-0.1.4.dist-info/METADATA,sha256=XpzmVDGTDfL-AxKUv7jIV3wQ_phEGc-OLxWU-wG7-o0,2850
4
- sphinxcontrib_screenshot-0.1.4.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
5
- sphinxcontrib_screenshot-0.1.4.dist-info/top_level.txt,sha256=VJrV3_vaiKQVgVpR0I1iecxoO0drzGu-M0j40PVP2QQ,14
6
- sphinxcontrib_screenshot-0.1.4.dist-info/RECORD,,