Skip to content

Commit e925369

Browse files
committed
Document directives, fix brittle paths in gallery item directive
1 parent 6756514 commit e925369

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
# Create gallery dirs if it doesn't exist
6464
try:
6565
os.mkdir(gallery_dir)
66-
except FileExistsError:
66+
except OSError:
6767
pass
6868

6969
# Copy rst files from source dir to gallery dir

custom_directives.py

+61-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@
55
import os
66
import sphinx_gallery
77

8+
try:
9+
FileNotFoundError
10+
except NameError:
11+
FileNotFoundError = IOError
12+
13+
814
class IncludeDirective(Directive):
15+
"""Include source file without docstring at the top of file.
16+
17+
Implementation just replaces the first docstring found in file
18+
with '' once.
19+
20+
Example usage:
21+
22+
.. includenodoc:: /beginner/examples_tensor/two_layer_net_tensor.py
23+
24+
"""
925

1026
# defines the parameter the directive expects
1127
# directives.unchanged means you get the raw value from RST
@@ -35,6 +51,22 @@ def run(self):
3551

3652

3753
class GalleryItemDirective(Directive):
54+
"""
55+
Create a sphinx gallery thumbnail for insertion anywhere in docs.
56+
57+
Optionally, you can specify the custom figure and intro/tooltip for the
58+
thumbnail.
59+
60+
Example usage:
61+
62+
.. galleryitem:: intermediate/char_rnn_generation_tutorial.py
63+
:figure: _static/img/char_rnn_generation.png
64+
:intro: Put your custom intro here.
65+
66+
If figure is specified, a thumbnail will be made out of it and stored in
67+
_static/thumbs. Therefore, consider _static/thumbs as a 'built' directory.
68+
"""
69+
3870
required_arguments = 1
3971
optional_arguments = 0
4072
final_argument_whitespace = True
@@ -46,28 +78,34 @@ class GalleryItemDirective(Directive):
4678
def run(self):
4779
args = self.arguments
4880
fname = args[-1]
81+
82+
env = self.state.document.settings.env
83+
fname, abs_fname = env.relfn2path(fname)
4984
basename = os.path.basename(fname)
5085
dirname = os.path.dirname(fname)
5186

5287
try:
5388
if 'intro' in self.options:
5489
intro = self.options['intro'][:195] + '...'
5590
else:
56-
intro = sphinx_gallery.gen_rst.extract_intro(fname)
91+
intro = sphinx_gallery.gen_rst.extract_intro(abs_fname)
5792

58-
thumbnail_rst = sphinx_gallery.backreferences._thumbnail_div(dirname, basename, intro)
93+
thumbnail_rst = sphinx_gallery.backreferences._thumbnail_div(
94+
dirname, basename, intro)
5995

6096
if 'figure' in self.options:
61-
env = self.state.document.settings.env
6297
rel_figname, figname = env.relfn2path(self.options['figure'])
63-
save_figname = os.path.join('_static/thumbs/', os.path.basename(figname))
98+
save_figname = os.path.join('_static/thumbs/',
99+
os.path.basename(figname))
64100

65101
try:
66102
os.makedirs('_static/thumbs')
67-
except FileExistsError:
103+
except OSError:
68104
pass
69105

70-
sphinx_gallery.gen_rst.scale_image(figname, save_figname, 400, 280)
106+
sphinx_gallery.gen_rst.scale_image(figname, save_figname,
107+
400, 280)
108+
# replace figure in rst with simple regex
71109
thumbnail_rst = re.sub(r'..\sfigure::\s.*\.png',
72110
'.. figure:: /{}'.format(save_figname),
73111
thumbnail_rst)
@@ -82,7 +120,6 @@ def run(self):
82120
return []
83121

84122

85-
86123
GALLERY_TEMPLATE = """
87124
.. raw:: html
88125
@@ -99,7 +136,24 @@ def run(self):
99136
</div>
100137
"""
101138

139+
102140
class CustomGalleryItemDirective(Directive):
141+
"""Create a sphinx gallery style thumbnail.
142+
143+
tooltip and figure are self explanatory. Description could be a link to
144+
a document like in below example.
145+
146+
Example usage:
147+
148+
.. customgalleryitem::
149+
:tooltip: I am writing this tutorial to focus specifically on NLP for people who have never written code in any deep learning framework
150+
:figure: /_static/img/thumbnails/babel.jpg
151+
:description: :doc:`/beginner/deep_learning_nlp_tutorial`
152+
153+
If figure is specified, a thumbnail will be made out of it and stored in
154+
_static/thumbs. Therefore, consider _static/thumbs as a 'built' directory.
155+
"""
156+
103157
required_arguments = 0
104158
optional_arguments = 0
105159
final_argument_whitespace = True

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ sphinx-gallery
44
numpy
55
matplotlib
66
torchvision
7+
torch

0 commit comments

Comments
 (0)