def format_image(image, doc): # type: (Image, Doc) -> Element """ originally adapted from: `pandoc-fignos <https://github.com/tomduck/pandoc-fignos/>`_ """ if not isinstance(image, pf.Image): return None span = None if (isinstance(image.parent, pf.Span) and LABELLED_IMAGE_CLASS in image.parent.classes): span = image.parent if span is not None: identifier = span.identifier attributes = span.attributes # classes = span.classes else: identifier = image.identifier attributes = image.attributes # classes = image.classes if doc.format in ("tex", "latex"): new_doc = Doc(pf.Para(*image.content)) new_doc.api_version = doc.api_version if image.content: caption = pf.run_pandoc(json.dumps(new_doc.to_json()), args=["-f", "json", "-t", "latex"]).strip() else: caption = "" options = attributes.get("placement", "") size = "" # max width set as 0.9\linewidth if "width" in attributes: width = convert_units(attributes["width"], "fraction") size = "width={0}\\linewidth".format(width) elif "height" in attributes: height = convert_units(attributes["height"], "fraction") size = "height={0}\\paperheight".format(height) if identifier: latex = LATEX_FIG_LABELLED.format( label=identifier, options=options, path=image.url, caption=caption, size=size, ) else: latex = LATEX_FIG_UNLABELLED.format(options=options, path=image.url, caption=caption, size=size) return pf.RawInline(latex, format="tex") elif doc.format in ("rst", ): if not image.content.list: # If the container is empty, then pandoc will assign an iterative # reference identifier to it (image0, image1). # However, this iterator restarts for each markdown cell, # which can lead to reference clashes. # Therefore we specifically assign the identifier here, as its url # TODO does this identifier need to be sanitized? # (it works fine in the tests) identifier = image.url image.content = pf.ListContainer(pf.Str(str(identifier))) return image # TODO formatting and span identifier (convert width/height to %) elif doc.format in ("html", "html5"): if identifier: return _wrap_in_anchor(image, identifier) else: return image # TODO formatting, name by count else: return None
def format_image(image, doc): # type: (Image, Doc) -> Element """ originally adapted from: `pandoc-fignos <https://github.com/tomduck/pandoc-fignos/>`_ """ if not isinstance(image, pf.Image): return None span = None if (isinstance(image.parent, pf.Span) and LABELLED_IMAGE_CLASS in image.parent.classes): span = image.parent if span is not None: identifier = span.identifier attributes = span.attributes # classes = span.classes else: identifier = image.identifier attributes = image.attributes # classes = image.classes if doc.format in ("tex", "latex"): new_doc = Doc(pf.Para(*image.content)) new_doc.api_version = doc.api_version if image.content: caption = pf.run_pandoc(json.dumps(new_doc.to_json()), args=["-f", "json", "-t", "latex"]).strip() else: caption = "" options = attributes.get("placement", "") size = '' # max width set as 0.9\linewidth if "width" in attributes: width = convert_units(attributes['width'], "fraction") size = 'width={0}\\linewidth'.format(width) elif "height" in attributes: height = convert_units(attributes['height'], "fraction") size = 'height={0}\\paperheight'.format(height) if identifier: latex = LATEX_FIG_LABELLED.format(label=identifier, options=options, path=image.url, caption=caption, size=size) else: latex = LATEX_FIG_UNLABELLED.format(options=options, path=image.url, caption=caption, size=size) return pf.RawInline(latex, format="tex") elif doc.format in ("rst", ): return image # TODO formatting and span identifier (convert width/height to %) elif doc.format in ("html", "html5"): if identifier: return _wrap_in_anchor(image, identifier) else: return image # TODO formatting, name by count else: return None