Skip to content
Permalink
Browse files

propagate tag download for images in sphinx_rst_builder

  • Loading branch information...
sdpython committed Sep 22, 2018
1 parent 14b7d09 commit c89c480a98c6b4a2b9d1877891d0d012fdf11752
@@ -24,6 +24,7 @@
from src.pyquickhelper.helpgen import rst2html
from src.pyquickhelper.sphinxext import CmdRef
from src.pyquickhelper.sphinxext.sphinx_cmdref_extension import cmdref_node, visit_cmdref_node, depart_cmdref_node
from src.pyquickhelper.sphinxext.sphinximages.sphinxtrib.images import ImageDirective


if sys.version_info[0] == 2:
@@ -361,7 +362,6 @@ def test_rst_reference2(self):
f.write(text)

def test_rst_image(self):

temp = get_temp_folder(__file__, "temp_rst_image")
root = os.path.abspath(os.path.dirname(__file__))
img1 = os.path.join(root, "data", "image", "im.png")
@@ -370,25 +370,31 @@ def test_rst_image(self):
.. image:: {0}
:width: 200
:alt: alternative1
:download: True
* .. image:: {1}
:width: 200
:alt: alternative2
:download: True
""".replace(" ", "").format(img1, img2).replace("\\", "/")
if sys.version_info[0] >= 3:
content = content.replace('u"', '"')

tives = [("image", ImageDirective)]

try:
text = rst2html(content, # fLOG=fLOG,
writer="rst", keep_warnings=False, layout='sphinx',
extlinks={'issue': ('http://%s', '_issue_')},
rst_image_dest=temp)
directives=tives, rst_image_dest=temp)
except Exception as e:
raise Exception(
"Issue with '{0}' and '{1}'".format(img1, img2)) from e

text = text.replace("\r", "")
self.assertNotIn('unknown option: "download"', text)
self.assertIn(':: 5cf2985161e8ba56d893.png', text)
self.assertIn(' :download: True', text)
self.assertIn(' :alt: alternative1', text)
self.assertIn(' :alt: alternative2', text)
self.assertIn(' :width: 200', text)
@@ -404,7 +410,32 @@ def test_rst_image_target(self):
img1 = os.path.join(root, "data", "image", "im.png")
content = """
.. image:: {0}
:target: https://github.com/sdpython
:target: https://github.com/sdpython.png
:width: 200
:alt: alternative1
""".replace(" ", "").format(img1).replace("\\", "/")
if sys.version_info[0] >= 3:
content = content.replace('u"', '"')

text = rst2html(content, # fLOG=fLOG,
writer="rst", keep_warnings=False, layout='sphinx',
extlinks={'issue': ('http://%s', '_issue_')})

text = text.replace("\r", "")
self.assertIn('data/image/im.png', text)
self.assertIn(' :alt: alternative1', text)
self.assertIn(' :width: 200', text)
with open(os.path.join(temp, "out_image.rst"), "w", encoding="utf8") as f:
f.write(text)

def test_rst_image_target2(self):

temp = get_temp_folder(__file__, "temp_rst_image_target2")
root = os.path.abspath(os.path.dirname(__file__))
img1 = os.path.join(root, "data", "image", "im.png")
content = """
.. image:: {0}
:target: https://github.com/sdpython.png
:width: 200
:alt: alternative1
""".replace(" ", "").format(img1).replace("\\", "/")
@@ -417,10 +448,9 @@ def test_rst_image_target(self):
rst_image_dest=temp)

text = text.replace("\r", "")
self.assertIn(':: 5cf2985161e8ba56d893.png', text)
self.assertNotIn('data/image/im.png', text)
self.assertIn(' :alt: alternative1', text)
self.assertIn(' :width: 200', text)
self.assertExists(os.path.join(temp, '5cf2985161e8ba56d893.png'))
with open(os.path.join(temp, "out_image.rst"), "w", encoding="utf8") as f:
f.write(text)

@@ -77,17 +77,21 @@ def base_visit_image(self, node, image_dest=None):
srcdir = None
if image_dest is None:
outdir = builder.outdir
if srcdir is None:
current = os.path.dirname(builder.current_docname)
if builder.current_docname and builder.current_docname != "<<string>>":
if srcdir is None:
current = os.path.dirname(builder.current_docname)
else:
current = os.path.dirname(os.path.join(
srcdir, builder.current_docname))
if current is None or not os.path.exists(current):
raise FileNotFoundError(
"Unable to find document '{0}' current_docname='{1}'".format(current, builder.current_docname))
dest = os.path.dirname(os.path.join(
outdir, builder.current_docname))
else:
current = os.path.dirname(os.path.join(
srcdir, builder.current_docname))
if current is None or not os.path.exists(current):
raise FileNotFoundError(
"Unable to find document '{0}'".format(current))
current_dest = os.path.dirname(
os.path.join(outdir, builder.current_docname))
fold = current_dest
# current_docname is None which means
# no file should be created
fold = None
else:
fold = image_dest

@@ -112,32 +116,42 @@ def base_visit_image(self, node, image_dest=None):
name = self.hash_md5_readfile(full) + ext
remote = False

if not os.path.exists(fold):
if fold is not None and not os.path.exists(fold):
os.makedirs(fold)

dest = os.path.join(fold, name)
if not os.path.exists(dest):
if remote:
if atts.get('download', False):
# Downloads the image
try:
get_url_content_timeout(
atts['src'], output=dest, encoding=None, timeout=20)
full = atts['src']
except InternetException as e:
logger = logging.getLogger("image")
logger.warning(
"[image] unable to get content for url '{0}' due to '{1}'".format(atts['src'], e))
this = os.path.abspath(os.path.dirname(__file__))
full = os.path.join(
this, "sphinximages", "sphinxtrib", "missing.png")
shutil.copy(full, dest)
dest = os.path.join(fold, name) if fold else None

if dest is not None:
if not os.path.exists(dest):
if remote:
if atts.get('download', False):
# Downloads the image
try:
get_url_content_timeout(
atts['src'], output=dest, encoding=None, timeout=20)
full = atts['src']
except InternetException as e:
logger = logging.getLogger("image")
logger.warning(
"[image] unable to get content for url '{0}' due to '{1}'".format(atts['src'], e))
this = os.path.abspath(
os.path.dirname(__file__))
full = os.path.join(
this, "sphinximages", "sphinxtrib", "missing.png")
shutil.copy(full, dest)
else:
name = atts['src']
full = name
dest = name
else:
name = atts['src']
full = name
dest = name
shutil.copy(full, dest)
full = dest
else:
shutil.copy(full, dest)
full = dest
else:
name = atts['src']
full = name
dest = name

atts['src'] = name
atts['full'] = full
@@ -151,6 +165,8 @@ def base_visit_image(self, node, image_dest=None):
atts['width'] = node['width']
if 'height' in node:
atts['height'] = node['height']
if 'download' in node:
atts['download'] = node['download']
if 'scale' in node:
import PIL
if 'width' not in node or 'height' not in node:
@@ -197,7 +197,7 @@ def __init__(self, builder):
LaTeXWriter.__init__(self, builder)

def translate(self):
visitor = self.builder.create_translator( self.builder, self.document)
visitor = self.builder.create_translator(self.builder, self.document)
self.document.walkabout(visitor)
self.output = visitor.astext()

@@ -523,7 +523,7 @@ def visit_image(self, node):
self.new_state(0)
atts = self.base_visit_image(node, self.builder.rst_image_dest)
self.add_text('.. image:: {0}'.format(atts['src']))
for att_name in 'width', 'height', 'alt':
for att_name in 'width', 'height', 'alt', 'download':
if att_name in node.attributes and node.get(att_name) != 'auto':
self.new_state(4)
self.add_text(":{0}: {1}".format(att_name, node[att_name]))

0 comments on commit c89c480

Please sign in to comment.
You can’t perform that action at this time.