Skip to content

Commit fe23c8c

Browse files
authored
Merge pull request #951 from pytorch/jlin27_tutorials_refresh
Add Build files for new tutorials cards and merge in updates to master index.rst
2 parents 0a45dcc + 8040da8 commit fe23c8c

File tree

3 files changed

+143
-4
lines changed

3 files changed

+143
-4
lines changed

conf.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import torch
3535
import glob
3636
import shutil
37-
from custom_directives import IncludeDirective, GalleryItemDirective, CustomGalleryItemDirective
37+
from custom_directives import IncludeDirective, GalleryItemDirective, CustomGalleryItemDirective, CustomCalloutItemDirective, CustomCardItemDirective
3838

3939

4040
try:
@@ -237,3 +237,5 @@ def setup(app):
237237
app.add_directive('includenodoc', IncludeDirective)
238238
app.add_directive('galleryitem', GalleryItemDirective)
239239
app.add_directive('customgalleryitem', CustomGalleryItemDirective)
240+
app.add_directive('customcarditem', CustomCardItemDirective)
241+
app.add_directive('customcalloutitem', CustomCalloutItemDirective)

custom_directives.py

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from docutils.parsers.rst import Directive, directives
2-
from docutils.statemachine import StringList
2+
from docutils.statemachine import StringList
33
from docutils import nodes
44
import re
55
import os
@@ -142,7 +142,7 @@ class CustomGalleryItemDirective(Directive):
142142
"""Create a sphinx gallery style thumbnail.
143143
144144
tooltip and figure are self explanatory. Description could be a link to
145-
a document like in below example.
145+
a document like in below example.
146146
147147
Example usage:
148148
@@ -206,3 +206,140 @@ def run(self):
206206
thumb = nodes.paragraph()
207207
self.state.nested_parse(thumbnail, self.content_offset, thumb)
208208
return [thumb]
209+
210+
211+
class CustomCardItemDirective(Directive):
212+
option_spec = {'header': directives.unchanged,
213+
'image': directives.unchanged,
214+
'link': directives.unchanged,
215+
'card_description': directives.unchanged,
216+
'tags': directives.unchanged}
217+
218+
def run(self):
219+
try:
220+
if 'header' in self.options:
221+
header = self.options['header']
222+
else:
223+
raise ValueError('header not doc found')
224+
225+
if 'image' in self.options:
226+
image = "<img src='" + self.options['image'] + "'>"
227+
else:
228+
image = '_static/img/thumbnails/default.png'
229+
230+
if 'link' in self.options:
231+
link = self.options['link']
232+
else:
233+
link = ''
234+
235+
if 'card_description' in self.options:
236+
card_description = self.options['card_description']
237+
else:
238+
card_description = ''
239+
240+
if 'tags' in self.options:
241+
tags = self.options['tags']
242+
else:
243+
tags = ''
244+
245+
except FileNotFoundError as e:
246+
print(e)
247+
return []
248+
except ValueError as e:
249+
print(e)
250+
raise
251+
return []
252+
253+
card_rst = CARD_TEMPLATE.format(header=header,
254+
image=image,
255+
link=link,
256+
card_description=card_description,
257+
tags=tags)
258+
card_list = StringList(card_rst.split('\n'))
259+
card = nodes.paragraph()
260+
self.state.nested_parse(card_list, self.content_offset, card)
261+
return [card]
262+
263+
264+
CARD_TEMPLATE = """
265+
.. raw:: html
266+
267+
<div class="col-md-12 tutorials-card-container" data-tags={tags}>
268+
269+
<div class="card tutorials-card" link={link}>
270+
271+
<div class="card-body">
272+
273+
<div class="card-title-container">
274+
<h4>{header}</h4>
275+
</div>
276+
277+
<p class="card-summary">{card_description}</p>
278+
279+
<p class="tags">{tags}</p>
280+
281+
<div class="tutorials-image">{image}</div>
282+
283+
</div>
284+
285+
</div>
286+
287+
</div>
288+
"""
289+
290+
class CustomCalloutItemDirective(Directive):
291+
option_spec = {'header': directives.unchanged,
292+
'description': directives.unchanged,
293+
'button_link': directives.unchanged,
294+
'button_text': directives.unchanged}
295+
296+
def run(self):
297+
try:
298+
if 'description' in self.options:
299+
description = self.options['description']
300+
else:
301+
description = ''
302+
303+
if 'header' in self.options:
304+
header = self.options['header']
305+
else:
306+
raise ValueError('header not doc found')
307+
308+
if 'button_link' in self.options:
309+
button_link = self.options['button_link']
310+
else:
311+
button_link = ''
312+
313+
if 'button_text' in self.options:
314+
button_text = self.options['button_text']
315+
else:
316+
button_text = ''
317+
318+
except FileNotFoundError as e:
319+
print(e)
320+
return []
321+
except ValueError as e:
322+
print(e)
323+
raise
324+
return []
325+
326+
callout_rst = CALLOUT_TEMPLATE.format(description=description,
327+
header=header,
328+
button_link=button_link,
329+
button_text=button_text)
330+
callout_list = StringList(callout_rst.split('\n'))
331+
callout = nodes.paragraph()
332+
self.state.nested_parse(callout_list, self.content_offset, callout)
333+
return [callout]
334+
335+
CALLOUT_TEMPLATE = """
336+
.. raw:: html
337+
338+
<div class="col-md-6">
339+
<div class="text-container">
340+
<h3>{header}</h3>
341+
<p class="body-paragraph">{description}</p>
342+
<a class="btn with-right-arrow callout-button" href="{button_link}">{button_text}</a>
343+
</div>
344+
</div>
345+
"""

index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,4 @@ Parallel and Distributed Training
371371
intermediate/dist_tuto
372372
intermediate/rpc_tutorial
373373
beginner/aws_distributed_training_tutorial
374-
intermediate/rpc_param_server_tutorial
374+
intermediate/rpc_param_server_tutorial

0 commit comments

Comments
 (0)