# build out an image directive like # .. image:: somefile.png # :width 4in # # from an input like # savefig somefile.png width=4in
(self, decorator)
| 404 | sys.stdout = stdout |
| 405 | |
| 406 | def process_image(self, decorator): |
| 407 | """ |
| 408 | # build out an image directive like |
| 409 | # .. image:: somefile.png |
| 410 | # :width 4in |
| 411 | # |
| 412 | # from an input like |
| 413 | # savefig somefile.png width=4in |
| 414 | """ |
| 415 | savefig_dir = self.savefig_dir |
| 416 | source_dir = self.source_dir |
| 417 | saveargs = decorator.split(' ') |
| 418 | filename = saveargs[1] |
| 419 | # insert relative path to image file in source |
| 420 | # as absolute path for Sphinx |
| 421 | # sphinx expects a posix path, even on Windows |
| 422 | path = pathlib.Path(savefig_dir, filename) |
| 423 | outfile = '/' + path.relative_to(source_dir).as_posix() |
| 424 | |
| 425 | imagerows = ['.. image:: %s' % outfile] |
| 426 | |
| 427 | for kwarg in saveargs[2:]: |
| 428 | arg, val = kwarg.split('=') |
| 429 | arg = arg.strip() |
| 430 | val = val.strip() |
| 431 | imagerows.append(' :%s: %s'%(arg, val)) |
| 432 | |
| 433 | image_file = os.path.basename(outfile) # only return file name |
| 434 | image_directive = '\n'.join(imagerows) |
| 435 | return image_file, image_directive |
| 436 | |
| 437 | # Callbacks for each type of token |
| 438 | def process_input(self, data, input_prompt, lineno): |