diff --git a/README.rst b/README.rst index 1e245c1..e4004ea 100644 --- a/README.rst +++ b/README.rst @@ -62,7 +62,11 @@ convert autolinks A boolean indicating whether the "automatic link" style should be used when - a ``a`` tag's contents match its href. Defaults to ``True`` + a ``a`` tag's contents match its href. Defaults to ``True``. + +default_title + A boolean to enable setting the title of a link to its href, if no title is + given. Defaults to ``False``. heading_style Defines how headings should be converted. Accepted values are ``ATX``, diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 0b849fb..013b1b4 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -69,6 +69,7 @@ class MarkdownConverter(object): strip = None convert = None autolinks = True + default_title = False heading_style = UNDERLINED bullets = '*+-' # An iterable of bullet types. strong_em_symbol = ASTERISK @@ -198,9 +199,14 @@ class MarkdownConverter(object): href = el.get('href') title = el.get('title') # For the replacement see #29: text nodes underscores are escaped - if self.options['autolinks'] and text.replace(r'\_', '_') == href and not title: + if (self.options['autolinks'] + and text.replace(r'\_', '_') == href + and not title + and not self.options['default_title']): # Shortcut syntax return '<%s>' % href + if self.options['default_title'] and not title: + title = href title_part = ' "%s"' % title.replace('"', r'\"') if title else '' return '%s[%s](%s%s)%s' % (prefix, text, href, title_part, suffix) if href else text diff --git a/tests/test_conversions.py b/tests/test_conversions.py index 354212b..5c544ed 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -173,7 +173,6 @@ def test_chomp(): def test_a(): assert md('Google') == '[Google](https://google.com)' - assert md('https://google.com', autolinks=False) == '[https://google.com](https://google.com)' assert md('https://google.com') == '' assert md('https://community.kde.org/Get_Involved') == '' assert md('https://community.kde.org/Get_Involved', autolinks=False) == '[https://community.kde.org/Get\\_Involved](https://community.kde.org/Get_Involved)' @@ -189,6 +188,7 @@ def test_a_spaces(): def test_a_with_title(): text = md('Google') assert text == r'[Google](http://google.com "The \"Goog\"")' + assert md('https://google.com', default_title=True) == '[https://google.com](https://google.com "https://google.com")' def test_a_shortcut(): @@ -197,8 +197,7 @@ def test_a_shortcut(): def test_a_no_autolinks(): - text = md('http://google.com', autolinks=False) - assert text == '[http://google.com](http://google.com)' + assert md('https://google.com', autolinks=False) == '[https://google.com](https://google.com)' def test_b():