5
5
import os
6
6
import sphinx_gallery
7
7
8
+ try :
9
+ FileNotFoundError
10
+ except NameError :
11
+ FileNotFoundError = IOError
12
+
13
+
8
14
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
+ """
9
25
10
26
# defines the parameter the directive expects
11
27
# directives.unchanged means you get the raw value from RST
@@ -35,6 +51,22 @@ def run(self):
35
51
36
52
37
53
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
+
38
70
required_arguments = 1
39
71
optional_arguments = 0
40
72
final_argument_whitespace = True
@@ -46,28 +78,34 @@ class GalleryItemDirective(Directive):
46
78
def run (self ):
47
79
args = self .arguments
48
80
fname = args [- 1 ]
81
+
82
+ env = self .state .document .settings .env
83
+ fname , abs_fname = env .relfn2path (fname )
49
84
basename = os .path .basename (fname )
50
85
dirname = os .path .dirname (fname )
51
86
52
87
try :
53
88
if 'intro' in self .options :
54
89
intro = self .options ['intro' ][:195 ] + '...'
55
90
else :
56
- intro = sphinx_gallery .gen_rst .extract_intro (fname )
91
+ intro = sphinx_gallery .gen_rst .extract_intro (abs_fname )
57
92
58
- thumbnail_rst = sphinx_gallery .backreferences ._thumbnail_div (dirname , basename , intro )
93
+ thumbnail_rst = sphinx_gallery .backreferences ._thumbnail_div (
94
+ dirname , basename , intro )
59
95
60
96
if 'figure' in self .options :
61
- env = self .state .document .settings .env
62
97
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 ))
64
100
65
101
try :
66
102
os .makedirs ('_static/thumbs' )
67
- except FileExistsError :
103
+ except OSError :
68
104
pass
69
105
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
71
109
thumbnail_rst = re .sub (r'..\sfigure::\s.*\.png' ,
72
110
'.. figure:: /{}' .format (save_figname ),
73
111
thumbnail_rst )
@@ -82,7 +120,6 @@ def run(self):
82
120
return []
83
121
84
122
85
-
86
123
GALLERY_TEMPLATE = """
87
124
.. raw:: html
88
125
@@ -99,7 +136,24 @@ def run(self):
99
136
</div>
100
137
"""
101
138
139
+
102
140
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
+
103
157
required_arguments = 0
104
158
optional_arguments = 0
105
159
final_argument_whitespace = True
0 commit comments