@@ -84,6 +84,7 @@ def __init__(self, out=None, baseurl='', bodywidth=config.BODY_WIDTH):
84
84
self .a = []
85
85
self .astack = []
86
86
self .maybe_automatic_link = None
87
+ self .empty_link = False
87
88
self .absolute_url_matcher = re .compile (r'^[a-zA-Z+]+://' )
88
89
self .acount = 0
89
90
self .list = []
@@ -370,16 +371,21 @@ def handle_tag(self, tag, attrs, start):
370
371
attrs ['href' ].startswith ('#' )):
371
372
self .astack .append (attrs )
372
373
self .maybe_automatic_link = attrs ['href' ]
374
+ self .empty_link = True
373
375
if self .protect_links :
374
376
attrs ['href' ] = '<' + attrs ['href' ]+ '>'
375
377
else :
376
378
self .astack .append (None )
377
379
else :
378
380
if self .astack :
379
381
a = self .astack .pop ()
380
- if self .maybe_automatic_link :
382
+ if self .maybe_automatic_link and not self . empty_link :
381
383
self .maybe_automatic_link = None
382
384
elif a :
385
+ if self .empty_link :
386
+ self .o ("[" )
387
+ self .empty_link = False
388
+ self .maybe_automatic_link = None
383
389
if self .inline_links :
384
390
self .o ("](" + escape_md (a ['href' ]) + ")" )
385
391
else :
@@ -399,6 +405,19 @@ def handle_tag(self, tag, attrs, start):
399
405
attrs ['href' ] = attrs ['src' ]
400
406
alt = attrs .get ('alt' ) or ''
401
407
408
+ # If we have a link to create, output the start
409
+ if not self .maybe_automatic_link is None :
410
+ href = self .maybe_automatic_link
411
+ if self .images_to_alt and escape_md (alt ) == href and \
412
+ self .absolute_url_matcher .match (href ):
413
+ self .o ("<" + escape_md (alt ) + ">" )
414
+ self .empty_link = False
415
+ return
416
+ else :
417
+ self .o ("[" )
418
+ self .maybe_automatic_link = None
419
+ self .empty_link = False
420
+
402
421
# If we have images_to_alt, we discard the image itself,
403
422
# considering only the alt text.
404
423
if self .images_to_alt :
@@ -637,10 +656,12 @@ def handle_data(self, data):
637
656
href = self .maybe_automatic_link
638
657
if href == data and self .absolute_url_matcher .match (href ):
639
658
self .o ("<" + data + ">" )
659
+ self .empty_link = False
640
660
return
641
661
else :
642
662
self .o ("[" )
643
663
self .maybe_automatic_link = None
664
+ self .empty_link = False
644
665
645
666
if not self .code and not self .pre :
646
667
data = escape_md_section (data , snob = self .escape_snob )
0 commit comments