|
1 | 1 | from docutils.parsers.rst import Directive, directives |
2 | | -from docutils.statemachine import StringList |
| 2 | +from docutils.statemachine import StringList |
3 | 3 | from docutils import nodes |
4 | 4 | import re |
5 | 5 | import os |
@@ -142,7 +142,7 @@ class CustomGalleryItemDirective(Directive): |
142 | 142 | """Create a sphinx gallery style thumbnail. |
143 | 143 |
|
144 | 144 | 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. |
146 | 146 |
|
147 | 147 | Example usage: |
148 | 148 |
|
@@ -206,3 +206,140 @@ def run(self): |
206 | 206 | thumb = nodes.paragraph() |
207 | 207 | self.state.nested_parse(thumbnail, self.content_offset, thumb) |
208 | 208 | 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 | +""" |
0 commit comments